while loops

A

Angie

I have a program that needs to read information from a file. This
information contains a name, a difficulty level and 9 scores.

The problem I am having is that my while loop isn't looping. It's only
reading the first full line of the file. Any suggestions?

Thanks in advance!
Angie


int main()
{
// declare variables
int SIZE;
double difficulty, score, total_score;
string name;

ifstream infile;
infile.open("MP6dive.dat");
if (!infile)
{
cout << "Trouble opening data file\n" ;
return 1;
}

infile >> SIZE;

infile >> name >> difficulty;
total_score = 0;

while(infile)
{
cout << name << setw(8) << difficulty << setw(7);

int i;
for(i=0; i <= JUDGES; i++)
{
infile >> score;
cout << score << " ";
total_score = score + total_score;
}

double final_score = total_score * difficulty;

cout << final_score << endl;

infile >> name >> difficulty;
}

infile.close();

return 0;
} // end main
 
V

Victor Bazarov

Angie said:
I have a program that needs to read information from a file. This
information contains a name, a difficulty level and 9 scores.

The problem I am having is that my while loop isn't looping. It's only
reading the first full line of the file. Any suggestions?

Yes. Post the contents of the file along with the source.

Victor
 
J

jwtroll05

It's only reading the first full line of the file.

You only told it to read the first full line of the file.

Instead of if(!infile)... try this:

while(infile)
{
infile >> name >> difficulty;
cout << name << difficulty;

total_score = 0;
for(int i = 0; i < 9; i++)
{
infile >> score;
cout << score;
total_score += score;
}
}
 
J

jwtroll05

Yeah...sorry. I viewed the original message in an internet newsgroup
reader and it saw the < signs and the > signs as html tags or something
and a lot of the code was missing. Ignore my previous post.
 
A

Angie

Angie said:
The problem I am having is that my while loop isn't looping. It's only
reading the first full line of the file. Any suggestions?

I figured it out and it was a simple error on my part. Thanks for looking
over it for me!
(Loops and I have a very strained relationship...)

Angie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top