1
votes

Trying to get some values to a struct. When I run the code, everything works perfectly except for the Gender values. For some reason that whole scanf just gets skipped over. In the command prompt it looks like this.

Please provide the student's first name: (user enters "John" and presses enter)

Please provide the student's first name: (user enters "Doe" and presses enter)

Please provide the student's gender (M/F): (can't input anything, no line skip) Please provide the student's age: (input)

etc.

I don't know if this could be a problem with another portion of the program, but I can edit in the whole thing if the problem isn't within this chunk of code.

if (option == 2){
    i=i+1;
    printf("Please provide the student's first name: ");
    scanf("%s", roster[i].firstname);
    printf("Please provide the student's last name: ");
    scanf("%s", roster[i].lastname);
    printf("Please provide the student's gender (M/F): ");
    scanf("%c", &roster[i].gender);
    printf("Please provide the student's age: ");
    scanf("%i", &roster[i].age);
    printf("Please provide the student's weight (In pounds): ");
    scanf("%i", &roster[i].weight);
    printf("Please provide the student's height (In inches): ");
    scanf("%i", &roster[i].height);
}
1
You get the newline after the last name in place of the gender; use " %c" to skip white space before reading a non-space character.Jonathan Leffler
I figured it had to be something simple. Thank you so much :)JustAGuy
Please don't use scanf for parsing user input. Just no. Use fgets(), strchr(), strtok(), etc.user529758
scanf() is a fiendishly difficult function to use completely correctly.Jonathan Leffler

1 Answers

1
votes

The problem here is that the code that reads the last name reads up to but not including the newline at the end of the input, and the %c conversion specification does not skip leading white space, so the gender input reads a newline into the gender and continues on to the next prompt (age).

Use " %c" to skip white space (which includes blanks, tabs and newlines) before reading a non-space character.

I note in passing that you should be checking each of your scanf() calls to ensure it recognized the input.

printf("Please provide the student's first name: ");
if (scanf("%s", roster[i].firstname) != 1)
    ...handle error...
printf("Please provide the student's last name: ");
if (scanf("%s", roster[i].lastname) != 1)
    ...handle error...
printf("Please provide the student's gender (M/F): ");
if (scanf("%c", &roster[i].gender) != 1)
    ...handle error...
printf("Please provide the student's age: ");
if (scanf("%i", &roster[i].age) != 1)
    ...handle error...
printf("Please provide the student's weight (In pounds): ");
if (scanf("%i", &roster[i].weight) != 1)
    ...handle error...
printf("Please provide the student's height (In inches): ");
if (scanf("%i", &roster[i].height) != 1)
    ...handle error...

Also, pity the student with spaces in the surname, or even forename.