February 20th, 2013, 02:36 PM
If loop conditions ignored
I am trying to use the response to a yes/no question to print one of two strings:
However, no matter what letter I put in, I get the "Error" result. I'm not sure what I'm doing wrong, so I'm seeing if anyone here can figure it out?
printf ("Does this program work? Y/N:");
if ((input == 'y') || (input == 'Y'))
February 20th, 2013, 02:47 PM
It's pretty amazing that it doesn't crash.
First of all, you forgot to use &input in your scanf call.
$ gcc -W -Wall -Wextra foo.c
foo.c: In function ‘main’:
foo.c:8:5: warning: format ‘%i’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat]
foo.c:17:1: warning: control reaches end of non-void function [-Wreturn-type]
foo.c:8:10: warning: ‘input’ is used uninitialized in this function [-Wuninitialized]
Then you need to realise that %i isn't for typing in letters.
You could type in 89 I suppose, but that's a bit counter-intuitive.