You are viewing our Forum Archives. To view or take place in current topics click here.
[Coding][C] Help me figure out the right way to do this
Posted:

[Coding][C] Help me figure out the right way to do thisPosted:

Suis
  • Ladder Climber
Status: Offline
Joined: Oct 15, 201013Year Member
Posts: 303
Reputation Power: 10
Status: Offline
Joined: Oct 15, 201013Year Member
Posts: 303
Reputation Power: 10
Instructions

i finished it now


I know how to do this if it was just like 1 line of the scores. but how do i do multiple lines of the scores. like my original plan was to do something like this


finished


Any help would be great!

Thanks.


Last edited by Suis ; edited 1 time in total
#2. Posted:
tortuga
  • Wizard
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
You could take your original plan, and just wrap it inside of while loop. Since scanf returns the number of input items matched, and returns an EOF (a macro for the end-of-file condition whose value is just -1), we could read input until an EOF occurs.

#include <stdio.h>

int main( void ) {

    int id, a1, a2, a3, a4, b1, b2, b3, b4;

    while ( scanf("%d %d %d %d %d %d %d %d %d",
            &id, &a1, &a2, &a3, &a4, &b1, &b2, &b3, &b4) != EOF ) {

        // output your ints however your prof wants them

        printf("%d %d %d %d %d %d %d %d %d\n",
            id, a1, a2, a3, a4, b1, b2, b3, b4);

    }

    return 0;

}


Notice how I have the assignment in the loop condition. This practice is very idiomatic in C, and you'll see it in a lot of further texts if you dive deeper into C. If you don't feel comfortable having the assignment there, you can rewrite it in the form of a do-while.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.