473,394 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 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
4 3255
"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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marco | last post by:
We can read this comment at php.net >jon >05-Nov-2003 10:06 >According to my tests, the "do...while" control structure actually seems to be ~40% faster than the "for" control structure. At...
24
by: Andrew Koenig | last post by:
PEP 315 suggests that a statement such as do: x = foo() while x != 0: bar(x) be equivalent to while True:
3
by: RobG | last post by:
A little while ago I opined that do/while loops are harder to read than for loops, and therefore I preferred using for loops. However, it was pointed out that do/while has significant...
36
by: invni | last post by:
I have a nested while. How do I go from the inner while to the beginning of the outer while? Can this be done without using goto? while_1() { some codes here while_2() { if true go to the...
147
by: Michael B Allen | last post by:
Should there be any preference between the following logically equivalent statements? while (1) { vs. for ( ;; ) { I suspect the answer is "no" but I'd like to know what the consensus is
8
by: jvb | last post by:
Hey all, I figure it's Wednesday, why not put a question up for debate. Beyond personal preference, is there any benefit (performance or otherwise) to using one loop over the other? For example,...
6
by: kydavis77 | last post by:
i was wondering if anyone could point me to some good reading about the for and while loops i am trying to write some programs "Exercise 1 Write a program that continually reads in numbers...
2
by: Sorin Schwimmer | last post by:
I am thinking on something in the following form: <code> import time import thread delay=True def fn() global delay
3
by: monomaniac21 | last post by:
hi all i have a script that retrieves rows from a single table, rows are related to eachother and are retrieved by doing a series of while loops within while loops. bcos each row contains a text...
5
by: Alex | last post by:
Hi I just want to clear something up in my head with while loops and exceptions. I'm sure this will probably be a no brainer for most. Check this simple pseudo-code out (vb.net): ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.