Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > while loops

Reply
Thread Tools

while loops

 
 
Angie
Guest
Posts: n/a
 
      07-30-2003
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( << 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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-30-2003
"Angie" <> 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


 
Reply With Quote
 
 
 
 
jwtroll05
Guest
Posts: n/a
 
      07-30-2003

>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
 
Reply With Quote
 
jwtroll05
Guest
Posts: n/a
 
      07-31-2003

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
 
Reply With Quote
 
Angie
Guest
Posts: n/a
 
      08-01-2003
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it possible to run two "while 1:" loops in two threadings respectively? zxo102 Python 2 07-19-2007 03:34 AM
Variable declaration and while loops Zachary Turner C++ 10 06-18-2007 08:34 PM
Testing pointers in while-loops cman C Programming 6 03-03-2007 09:08 AM
for and while loops kydavis77@gmail.com Python 6 06-29-2006 03:40 AM
Loops with loops using html-template Me Perl Misc 2 01-12-2006 05:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57