How can I read multiple datas from outside text file that have the same data type by declaring only one variable?
For example:
numbers below are scores and they are double, the negative number is used a "break" jump to the next record

The problem is that each record has different number of score and I can't find a way to get each score.
Note that I used "in" instead of "cin" cuz I read outside data file and then view it on the screen....
Any advice? ....

11-04-2007

Daved

You still need two loops. The outer loop reads a line at a time. The inner loop reads until it finds a negative number.

Your current code has a typo (a missing >>). Otherwise it will read in the values and do nothing with them for a single line. You need to put all that code in another loop to read each line, and you need to do something with each score.

11-04-2007

CornedBee

It will do absolutely nothing at all. You initialize score to 0, then run a loop while it is greater than 0. Obviously, the loop won't ever be entered.

You could do this instead:

Code:

while((in >> score) && (score > 0)) {
// do something with score
}

11-04-2007

Molokai

The example loop I gave you in the other one should have worked just fine, as long as you took the "c"s out of the "cin"s. But they're all correct. You need two loops.

Edit: And again, you can't have more than one value for a single variable unless you plan on printing it immediately after it is set. The way you're trying to set it up, you need to have an array, not a single variable.