I'm fairly competent with if/else statements, and this is a really old assignment that I ended up turning in partially complete. But I still wanted to know exactly why my code wouldn't work.
I want my user to input name, height, and sex. My code will then display an entire sentence that says "Name is X cm tall and is a male" or "Name is X cm tall and is a female."
When I input name and enter, it immediately then skips displays both the height AND the sex. Regardless of what I input after that, it then ends the program.
Input name: Jack Input height in cm: 180 sex(M/F): Computer $
I've been playing around with this code for a while now, but I have been stuck for a while now. Any help would be greatly appreciated. Here is my code:
#include<stdio.h> int main() { char name[30]; char sex; float height; printf("Input name: "); scanf("%s", name); fflush(stdin); printf("Input height in cm: "); scanf("%f", &height); fflush(stdin); printf("sex(M/F): "); scanf("%c", &sex); if (sex == 'M') { printf("%s is %f cm tall and male", name, height); } else if (sex == 'F') { printf("%s is %f cm tall and female", name, height); } printf("\n"); return 0; }