1

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; } 
1
  • Worked when I compiled under c++ express.. (although I stupidly put "f" not "F" .. so didnt print the first time.) Commented Jul 23, 2012 at 11:58

5 Answers 5

5

From what I can see it only skips the sex part - which is very sad to be honest :-)).

it immediately then skips displays both the height AND the sex

Input name: Jack Input height in cm: 180 sex(M/F): Computer $

You can try this:

scanf(" %c", &sex); ^ 

The space causes scanf to eat blanks before reading a character.

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

6 Comments

Wouldn't the value still be the same? Sorry if I'm wrong, I would have though the compiler would fill in the blanks.
@TheBlueCat scanf is very particular about its input. Some specifiers consume the space, some don't.
To elaborate on the reason, it's because when trying to read the sex as a single character, there is already the newline in the buffer and the scanf sets the sex to the newline. Adding a space in front of the format code tells scanf to skip whitespace (i.e. space, tabs and newlines).
Okay, I just tried your solution and it worked on GCC/Linux 3.4. Why does this happen, I don't understand why there needs to be a space?
@JoachimPileborg I intentionally used fflush(stdin); to renew the input buffer. Shouldn't that have sufficed for this issue?
|
0

fflush(stdin) is a very bad idea (undefined behavior). You should replace this call with an other function call, for example this one :

static void clean_stdin(void) { int c; do { c = getchar(); } while (c != '\n' && c != EOF); } 

With it, it seems working.

2 Comments

Yeah, I've been told multiple times from compilers and friends to avoid using scanf and gets, but I fear I've gotten accustomed to it after being taught to use these functions.
@Danny, why not just use fgets?
0

You're missing an ampersand on line 9:

scanf("%s", name); 

shoul be:

scanf("%s", &name); 

maybe this helps

3 Comments

scanf("%s", name); is enough. No need for &
@TheBlueCat I am told that because the string in C is already a character array, which is a pointer value, adding & on top of that would be repetitive.
@TheBlueCat It's not technically wrong, since taking the address of an array is the same as using the array itself. I.e. name == &name. However, it only works for arrays, not for pointer, so if you have e.g. char *nameptr = name;, then using &nameptr will actually return the address of the pointer, not what it points to.
0

You can also try this

printf("sex(M/F): "); scanf("%s", &sex); 

Comments

0

To read a single character from stdin you have to use getchar() instead of scanf("%c", &c) Or change the variable type of sex to char[2] and use scanf("%s", sex)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.