Reading from file with different amount of digits in lines

Oke, so I have data txt looking like this:

8
9 5 7 -5 13 -4 11
7 5 -3 12 -5 17 -3
25 7 12 -3 5 -5 7 -5 3
14 5 12 -3 10 -7 8
5 1 -40
33 5 15 -5 9 -3 8
11 5 -12 8 -5 12 -3
13 5 3 -4 25 -5 3

8 is obviously number of lines, then from 2nd line
no1: jersey number of particular player
no2: number of digits remaining
no3,4,5...: if its >0, it indicates how many minutes he played, if its <0, it shows how many mins he was on the bench. He can go out several times (that's why there are several variating digits).

How do I write the code for this?

file>>n;
for(int i=1;i<=n;i++)

what's next?
I assume I can't write "file >> a >> b >> c" in this case ?
Last edited on
You know that there are 8 lines so you need to loop 8 times (you have that code already). Then extract the player's number and then the number signalling the amount of following numbers. Loop for that amount of times. You're basically using the same technique as you did for knowing that you need to loop 8 times:
1
2
3
4
5
6
- Get the amount of lines
- For each line
  - Get the player number
  - Get the amount of changes
  - For each change
    - Get the change
Last edited on
Theoretically I know all of this but I have no idea how I should write all of that...

1
2
3
4
5
6
7
8
file>>n;
for(int i=1;i<=n;i++){
    file>>nr>>changes;

for (int i=1; i<=changes;i++){
...   

 


Is this correct? If so, how do I read now remaining values? How do I tell compiler that they are in the same line?

Last edited on
The >> operator ignores white space, and a new line is white space.

If you aren't sure about what your program is reading, print the data out to the console:
1
2
3
4
5
6
7
file >> n;
// I know n should be 8, is it?
cout << n << endl;

// How do I get to the next line?  Can I just do it?
file >> n;
cout << n << endl;





Thanks, couldn't believe at first it will be so easy, lol.

What I need to do next is make it check if the first value of time is negative or not (easy enough) and then somehow somewhere store the positive ones |????|(starting lineup) so that later on I could sort them by jersey numbers, how it should be done? I assume it should be something with those A[i] (constructors or how do you call them in English...), but haven't got much clue...
Last edited on
Anyone have an idea how do I store them (together with info to which jersey it belongs) and then call for them so I could sort them by jersey numbers ?

8
9 5 7 -5 13 -4 11
7 5 -3 12 -5 17 -3
25 7 12 -3 5 -5 7 -5 3
14 5 12 -3 10 -7 8
5 1 -40
33 5 15 -5 9 -3 8
11 5 -12 8 -5 12 -3
13 5 3 -4 25 -5 3

Here is the values I will need.

and again

first no - jersey number
2nd - number of following values (times in minutes)
if 3rd value is negative, it means player started game on the bench.

When I try something like this:

1
2
3
4
5
6
7
int n, i, nr[n], laikai, l1, l2;
failas>>n; // number of lines/players
for(int i=1;i<=n;i++){
    failas>>nr[i]>>laikai; // nr - number of playing/bench time digits for each player, laikai - actual times. 

for (int i=1; i<=laikai;i++){
    failas >> l1 >> l2; // seperated time values to check if l1 is negative later on 


main.exe just hangs.
Last edited on
bump
Topic archived. No new replies allowed.