omer_dude
omer_dude

Reputation: 11

Using arrays with structures

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();
}

Upvotes: 0

Views: 36

Answers (1)

nullptr
nullptr

Reputation: 11058

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.

Upvotes: 3

Related Questions