Code:char input = getchar(); printf("%c",&input);
how come i dont get the same output whenCode:char input = 0; scanf("%c",&input); printf("%c",&input);
i enter the input? im trying to get one 1 char.
i come from c++
Code:char input = getchar(); printf("%c",&input);
how come i dont get the same output whenCode:char input = 0; scanf("%c",&input); printf("%c",&input);
i enter the input? im trying to get one 1 char.
i come from c++
i enter one and it prints outCode:char input = 0; while(1) { printMap(Level1); input = getchar(); switch(input) { case '1': case 1: printf("You entered 1\n"); break; default: printf("No.\n"); break; } }
both 1 and default
whats the deal here, c++ made it simple with cin
Last edited by hit; 08-18-2012 at 10:42 PM.
This is another one of those "if I had a dollar for every time it was asked" questions. When you enter the number 1 and press enter, the getchar reads the 1 but leaves the newline behind. Your loop goes around and the getchar reads the newline the next time, leading to the default case.
And technically, getchar returns an int, not a char, but that's not the cause of your problem.
In your first post you shouldn't be using an ampersand in front of input in the printf function. That will pass the address of input to printf, which then tries to print that as a char.
The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss
thanks for your help.
how do i get it to stop reading the null character etc, next time around?Code:int main() { struct Map Level1 = MapInit(); struct Creature User = {100,20,2,"User",0,0}; char input = 0; while(1) { printMap(Level1); scanf("%c",&input); //or input = getchar(); move(input,&User,&Level1); } return 0; }
One solution would be to add one more "getchar()" after receiving the user input to eat up that newline floating in the input buffer.
You can also put a space before the %c in the "scanf()" call.
Why are you put and ampersand in front of your variable in the printf statement? Remove that, and your problems will be resolved.