# Help with creating a loop?

• (3 Pages)
• 1
• 2
• 3

## 35 Replies - 5309 Views - Last Post: 27 March 2012 - 12:46 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=272465&amp;s=5626c2eb234c510e89a73795182b175b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 TommehTGOD

Reputation: 0
• Posts: 31
• Joined: 25-March 12

# Help with creating a loop?

Posted 25 March 2012 - 03:27 PM

This is my input data file
```30713 4 3 3 4 3 3 2 3 4 3
21356 4 4 4 3 3 3 2 3 2 3
21265 4 1.5 3 3 3 0.5 2 3 3 3
19364 4 3 3 3 -1 -1 -1 -1 -1 -1
19335 0 3 0 4 2 3 3 1.5 -1 -1
18264 2 3 3 0.5 3 0.5 3 3 2 3
20135 4 1 3 4 3 1.5 0 3 -1 -1
22185 4 3 4 4 4 3 3 3 4 3
```

And this is the main code...I'm trying to read the grade and credit hours in each course as you can see. There are spots where there are no courses taken, so I put -1 in the place of them. What loop can I put inside my while loop so when the sentinel value of -1 is read, the inside loop IN the while loop stops and goes to the next line in the input data file?? Thanks

P.S. (Where i put in the IF statement, it may be wrong. I was still trying to work on it.

```#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <stdlib.h>
using namespace std;

//function prototypes go
//double findGPA(double points, double hours);
int main()
{
double gpa, points, hours, ID, grade, crhrs;

ofstream OutputFile;
OutputFile.open("Output.dat");

ifstream infile;
infile.open("Input.dat");

OutputFile<<setw(30)<<"Thomas Powe"<<endl;
OutputFile<<setw(29)<<"03-27-12"<<endl<<endl;
OutputFile<<setw(15)<<"ID"<<setw(18)<<"GPA"<<endl<<endl;
while (!infile.eof())
{
if (grade <= -1 || crhrs <= -1)
{
break;
}
//OutputFile<<setw(15)<<ID<<setw(18)<<"Comin Soon"<<endl;
hours=crhrs + crhrs; //+ crhrs +crhrs +crhrs;
gpa=points/hours;
OutputFile<<setw(15)<<ID<<setw(18)<<gpa<<endl;
}
OutputFile.close();
infile.close();
return 0;
}
//Function Definitions go here
//double findGPA(double points, double hours)
//{
//return gpa;
//}
//

```

This is supposed to be the output code
03-27-2012
ID GPA
30713 3.2
21356 3.1
21265 2.9
19364 3.5
19335 0.9
18264 2.4
20135 2.2
22185 3.8

Is This A Good Question/Topic? 0

## Replies To: Help with creating a loop?

### #2 #define

• Duke of Err

Reputation: 1635
• Posts: 5,755
• Joined: 19-February 09

## Re: Help with creating a loop?

Posted 25 March 2012 - 04:13 PM

Hi, your data file still has 5 sets of numbers when -1 is used, they will still have to be read to remove them from the file stream.

```19364 4 3 3 3 -1 -1 -1 -1 -1 -1

```

In the loop just read the ID, then use an inner loop to read the 5 sets of numbers. If the number is -1 don't update points or hours.

### #3 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: Help with creating a loop?

Posted 25 March 2012 - 04:17 PM

You don't have an "INSIDE" loop in a while loop. You just have a while loop.

It's not clear exactly at which point you want to stop -- maybe before storing any "-1"s, maybe after storing the "-1"s but before outputting -- but you can figure that out yourself. Obviously you'll have to test each input value (presumably with an if statement).

Whenever you decide that you want to discontinue that iteration, you can use
```continue;
```

continue is similar to break in that it immediately jumps out of the current iteration of the loop (it skips whatever is inside the loop after "continue") but unlike break, it doesn't jump to the code following the loop. Instead, it just goes on processing the next iteration of the loop.

### #4 TommehTGOD

Reputation: 0
• Posts: 31
• Joined: 25-March 12

## Re: Help with creating a loop?

Posted 25 March 2012 - 04:20 PM

I'm so confused...how will i do that..please guide me
my professor said the way my code is reading the file needs to be changed
and plus how would you read in each course...you see for example it's like

so like 1 grade and 1 crhrs represents 1 course
so how would i read that in correctly?

then which loop should i use to read the grade and crhrs in?

p.s. crhrs means "credit hours"

thanks

### #5 #define

• Duke of Err

Reputation: 1635
• Posts: 5,755
• Joined: 19-February 09

## Re: Help with creating a loop?

Posted 25 March 2012 - 04:49 PM

TommehTGOD, on 26 March 2012 - 01:20 AM, said:

I'm so confused...how will i do that..please guide me
my professor said the way my code is reading the file needs to be changed

I thought I described it quite well in my previous post.
How would you read the ID from the file into a variable?

TommehTGOD, on 26 March 2012 - 01:20 AM, said:

then which loop should i use to read the grade and crhrs in?

All loops are basically the same but a for loop seems fitting.
What do you think a for loop used to read the 5 courses would look like?

### #6 TommehTGOD

Reputation: 0
• Posts: 31
• Joined: 25-March 12

## Re: Help with creating a loop?

Posted 25 March 2012 - 04:59 PM

ok, so how would you break that up i mean reading only id in the main loop, then reading the grade and credit hours in the for loop (if i said that right)

### #7 #define

• Duke of Err

Reputation: 1635
• Posts: 5,755
• Joined: 19-February 09

## Re: Help with creating a loop?

Posted 25 March 2012 - 05:10 PM

To read the ID from the stream to the ID variable

```infile >> ID

```

It is useful to put the read into the while conditional expression. This is because the read operation flags up more information about the state of the stream than just infile.eof().

```while(infile >> ID)

```

### #8 TommehTGOD

Reputation: 0
• Posts: 31
• Joined: 25-March 12

## Re: Help with creating a loop?

Posted 25 March 2012 - 05:17 PM

http://faculty.winth...05f07f10s12.pdf

here's the instructions...i'm not asking you to do it for me, but here's a better interpretation of it than i can tell you of what's goin on.

but i kinda get what you're saying on infile>>ID..

### #9 #define

• Duke of Err

Reputation: 1635
• Posts: 5,755
• Joined: 19-February 09

## Re: Help with creating a loop?

Posted 25 March 2012 - 06:04 PM

The data file in the pdf doesn't have any -1's. Usually getline would be used to get a line of text, then a stringstream could be used extract each data value.

That would probably be the easiest way.

Does the chapter on loops give hints for a method to read a varying number of items in a record?

### #10 TommehTGOD

Reputation: 0
• Posts: 31
• Joined: 25-March 12

## Re: Help with creating a loop?

Posted 25 March 2012 - 06:22 PM

not at all really to be honest lol
it goes over these three loops: while loop, do while loop, and for loop
basically, in the data file you see
you have the ID number, grade and credit hours
my professor said that grade and credit hours should be read in a loop inside the main loop one set a time (1 course)
I put the -1's in there because there are some lines that don't have ALL of the courses. I also did this so that way when -1 is read, it terminates the loop and moves on to the next line down

I highly doubt we can use getline because we have not learn that yet

i bet the solution is simple, but i definitely don't know what it is.
like this

21265 4 1.5 3 3 3 0.5 2 3 3 3
19364 4 3 3 3 -1

ID 21265 has 5 courses (1 grade and 1 credit hour a piece) while ID 19364 has only 2 courses.
since ID 19364 has only 2 courses and not 5, i put a -1, so when -1 is read, the loop terminates and moves to the next line.

another explanation: for example, in ID 21265, the first number you see, (which is 4) is the grade, and the 2nd number you see (which is 1.5) is the credit hour, so both numbers togethers being read represents 1 course

I hope this explains things better

### #11 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: Help with creating a loop?

Posted 25 March 2012 - 06:41 PM

First, you have this hint in your assignment:

Quote

Review the chapter on loops for a method to read a varying
number of items in a record.

I have no idea what's in that chapter, but here's another hint:
every line in a text file (except maybe the last one) ends with a newline character '\n' (in Unix/Linux and OS X) or (in Windows) two characters '\r' '\n', so if you read the next input and see if it's one of those (or 'EOF'), you'll know you're at the end of a line.

Now you can have two loops: an outer loop that reads the first entry in each line (the ID), and an inner loop that reads the rest of each line.

### #12 TommehTGOD

Reputation: 0
• Posts: 31
• Joined: 25-March 12

## Re: Help with creating a loop?

Posted 25 March 2012 - 06:48 PM

ok this what i have so far for the outer and inner loop...
what needs to be changed and what needs to be added so the rest of the lines can be read
```while (!infile.eof())
{
while (infile>>ID)
{
OutputFile<<setw(15)<<ID<<setw(18)<<"Comin Soon"<<endl;
}
//OutputFile<<setw(15)<<ID<<setw(18)<<"Comin Soon"<<endl;
//hours=crhrs + crhrs; //+ crhrs +crhrs +crhrs;
//gpa=points/hours;
//OutputFile<<setw(15)<<ID<<setw(18)<<gpa<<endl;
}
OutputFile.close();
infile.close();
return 0;
}
```

### #13 #define

• Duke of Err

Reputation: 1635
• Posts: 5,755
• Joined: 19-February 09

## Re: Help with creating a loop?

Posted 25 March 2012 - 07:49 PM

You want to get the ID in the outer loop.

```while (!infile.eof())
{
infile>>ID;

```

You can check for the newline character in the inner loop as r.stiltskin suggests.

### #14 TommehTGOD

Reputation: 0
• Posts: 31
• Joined: 25-March 12

## Re: Help with creating a loop?

Posted 25 March 2012 - 08:20 PM

ok, i'm gettin what you guys are saying....alright i put the infile>>ID in the outer loop so in all its looking like this right now

while (!infile.eof())
{
infile>>ID;
while (infile>>ID)
{

}
}
etc
etc
What goes in the inner loop so it can read the rest of the lines and
how do i check for the newline character in the inner loop?

ok, i'm gettin what you guys are saying....alright i put the infile>>ID in the outer loop so in all its looking like this right now

while (!infile.eof())
{
infile>>ID;
while (infile>>ID)
{

}
}
etc
etc
What goes in the inner loop so it can read the rest of the lines and
how do i check for the newline character in the inner loop?

### #15 #define

• Duke of Err

Reputation: 1635
• Posts: 5,755
• Joined: 19-February 09

## Re: Help with creating a loop?

Posted 25 March 2012 - 08:40 PM

With the inner loop you want to get a grade and hours from the file stream.

You can get a character from the stream and test for a newline character if so you can break from the loop.

```char ch;

infile >> ch;

if(ch == '\n')
cout << "new line" << endl;

```