# c++: input unknown amount of int from file

#### dubstar

##### format c:
i'm pulling information from a file -- in this order -- first name, last name, scores (different amount of scores for each line).

example:
john doe 44 100 80 33 -1
jane johnson 33 -4
hooka smoker -9
etc etc 0 44 99 -44

the negative score is supposed to tell the code to stop, read it, but stop that line and move to the next line.

problem code:
Code:
[SIZE=2]in >> score;[/SIZE]
[SIZE=2][COLOR=#0000ff]while[/COLOR][/SIZE][SIZE=2] (score >= 0)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]cout << score;[/SIZE]
[SIZE=2]}[/SIZE]

or

Code:
[SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] i=0;i<10;i++)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]in >> score[i + 0];[/SIZE]
[SIZE=2]cout << score[i + 0];[/SIZE]
[SIZE=2]}[/SIZE]

here is the whole function code:
Code:
[SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][SIZE=2] ReadRecord()[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]ifstream in; [/SIZE][SIZE=2][COLOR=#008000]// input file[/COLOR][/SIZE]
[SIZE=2]string fname; [/SIZE][SIZE=2][COLOR=#008000]// first name[/COLOR][/SIZE]
[SIZE=2]string lname; [/SIZE][SIZE=2][COLOR=#008000]// last name[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] score;[/SIZE]
[SIZE=2][COLOR=#008000]//OPEN FILES[/COLOR][/SIZE]
[SIZE=2]in.open([/SIZE][SIZE=2][COLOR=#a31515]"c:\\in_infoproc.txt"[/COLOR][/SIZE][SIZE=2], ios::in); [/SIZE][SIZE=2][COLOR=#008000]// opens in.txt file for input[/COLOR][/SIZE]

[SIZE=2][COLOR=#0000ff]while[/COLOR][/SIZE][SIZE=2] (in.eof() == [/SIZE][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]getline(in, fname, [/SIZE][SIZE=2][COLOR=#a31515]' '[/COLOR][/SIZE][SIZE=2]); [/SIZE][SIZE=2][COLOR=#008000]// first name[/COLOR][/SIZE]
[SIZE=2]getline(in, lname, [/SIZE][SIZE=2][COLOR=#a31515]' '[/COLOR][/SIZE][SIZE=2]); [/SIZE][SIZE=2][COLOR=#008000]// last name[/COLOR][/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]"\t\tYour name is:\t"[/COLOR][/SIZE][SIZE=2] << fname << [/SIZE][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][SIZE=2] << lname << endl;[/SIZE]
[SIZE=2]in >> score;[/SIZE]
[SIZE=2][COLOR=#0000ff]while[/COLOR][/SIZE][SIZE=2] (score >= 0)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]cout << score;[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] i=0;i<10;i++)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]in >> score[i + 0];[/SIZE]
[SIZE=2]cout << score[i + 0];[/SIZE]
[SIZE=2]}[/SIZE]

[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]"\n\t\t==============================================\n\n"[/COLOR][/SIZE][SIZE=2] << endl;[/SIZE]
[SIZE=2]}[/SIZE]

[SIZE=2][COLOR=#008000]//CLOSE FILES[/COLOR][/SIZE]
[SIZE=2]in.close();[/SIZE]

[SIZE=2][COLOR=#008000]// ----------------------------------------------------------------------------------------------------------- END READ RECORD FUNCTION[/COLOR][/SIZE]
[SIZE=2]}[/SIZE]

i've tried this tons of different ways but cant seem to find one that works properly. eventually i have to put this in to an array, having fname and lname the arrays.. but if i dont understand the first parts, i'm not ready to put it into an array.

Last edited:

#### LordOfLA

##### Godlike!
Well getline looks like its reading until it gets a \r\n or any other combination of those two characters.

Seems to me you just need to check for a negative score in your variable, either strip it or not and move on. I don't really see what your problem is...

#### dubstar

##### format c:
for the first record it spits out the first and last name, no score (its negative)
the second record becomes aligned improperly, and then the first number (positive) is repeated over and over.... never stops

edit: you helped. all it takes is waking up. thanks man.

problem was that i wasnt reintroducing the new variable. in << score in to the loop.

Last edited:

#### Geffy

another alternative to getting the line data might be to use a std::istringstream.

I know that wasn't the question but it might make the algorithm simpler and easier to debug

#### dubstar

##### format c:
thanks geffy, i'll look into that when i get the program fully functional.

### Members online

No members online now.

### Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Impressed you have kept this alive this long EP! So many sites have come and gone.

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.