| View previous topic :: View next topic |
| Author |
Message |
Angie Guest
|
Posted: Wed Jul 30, 2003 5:49 pm Post subject: while loops |
|
|
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 filen" ;
return 1;
}
infile >> SIZE;
infile >> name >> difficulty;
total_score = 0;
while(infile)
{
cout << name << setw( << 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
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Wed Jul 30, 2003 6:11 pm Post subject: Re: while loops |
|
|
"Angie" <ahurlbut (AT) NOSPAM (DOT) umich.edu> wrote...
| Quote: | 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
|
|
| Back to top |
|
 |
jwtroll05 Guest
|
Posted: Wed Jul 30, 2003 7:25 pm Post subject: Re: while loops |
|
|
| Quote: | 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
|
|
| Back to top |
|
 |
jwtroll05 Guest
|
Posted: Thu Jul 31, 2003 4:31 pm Post subject: Re: while loops |
|
|
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
|
|
| Back to top |
|
 |
Angie Guest
|
Posted: Fri Aug 01, 2003 1:45 pm Post subject: Re: while loops |
|
|
Angie wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
|