By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,654 Members | 936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,654 IT Pros & Developers. It's quick & easy.

while loops

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Angie" <ah******@NOSPAM.umich.edu> wrote...
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
Jul 19 '05 #2

P: n/a
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;
}
}

--
Posted via http://dbforums.com
Jul 19 '05 #3

P: n/a

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.

--
Posted via http://dbforums.com
Jul 19 '05 #4

P: n/a
Angie wrote:
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
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.