C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

while loops

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Angie
Guest





PostPosted: Wed Jul 30, 2003 5:49 pm    Post subject: while loops Reply with 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?

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(Cool << 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





PostPosted: Wed Jul 30, 2003 6:11 pm    Post subject: Re: while loops Reply with quote



"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





PostPosted: Wed Jul 30, 2003 7:25 pm    Post subject: Re: while loops Reply with quote




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





PostPosted: Thu Jul 31, 2003 4:31 pm    Post subject: Re: while loops Reply with quote


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





PostPosted: Fri Aug 01, 2003 1:45 pm    Post subject: Re: while loops Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.