0

I'm trying to make couple of arrays out of a structure. I'm having difficulties in getting the values for every array ( for example I want to get 6 different arrays from a structure but I only get one array and then the program stops to the final stage). Can someone tell me what's wrong with my code?

#define NUM_OF_PLAYERS 6
typedef struct player
{
char name[20];
float height;
float avr_points;
int tshirt_num;
 };



int main()
{
_flushall();
struct player players[NUM_OF_PLAYERS];
int i;
for (i=0 ; i<NUM_OF_PLAYERS ; i++);
{
    printf("\nenter the name of the player, height in cm, \navrage points            per game and number of his tshirt\n"); 
    scanf("%s", &players[i].name);
    scanf("%f", &players[i].height);
    scanf("%f", &players[i].avr_points);
    scanf("%d", &players[i].tshirt_num);
    _flushall();
}
2
  • first thing, the typedef either 1) needs a final name before the ';' or 2) remove the 'typedef' modifier Commented Feb 1, 2015 at 20:12
  • what are you expecting the calls to '_flushall();' to accomplish? the flush operation is only for output, not input streams. So it has no effect on stdin. Commented Feb 1, 2015 at 20:18

1 Answer 1

3

You have an extra ; which terminates your for-loop.

for (i=0 ; i<NUM_OF_PLAYERS ; i++);
                                  ^

This is essentially equal to

for (i=0 ; i<NUM_OF_PLAYERS ; i++) {}

So the for-loop has an empty body, and all scanf's are executed only once, outside the loop.

Sign up to request clarification or add additional context in comments.

2 Comments

........................................ MY BAD........................... Thank you VERY much! I was stuck on this for couple of hours.....
also note that the scanf's are placing data past the end of the definition of 'players' array as 'i' is already 7 when exiting the 'for' loop. I.E. undefined behaviour that can/will lead to a seg fault event.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.