0

I have a problem with a small C program. It outputs a question (see code below) of which I can put input into (y and n) but then nothing else happens, even though it is meant to print something according to the input entered (y or n). However, nothing is outputted after my question, and the program just exits. Here is the code:

#include <stdio.h>

int main()
{
        char string [80];
        static char y;
        static char n;
        printf( "ARE YOU SHAQIRI? [y/n]: " );
        scanf( "%s", string );
        if ("%s" == "y")
                printf("That's impossible. YOU CANNOT BE SHAQIRI YOU IDIOT");
        else if ("%s" == "n")
              printf("I thought not.");
        fflush ( stdin );
        return 0;
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
islandmonkey
  • 15
  • 1
  • 1
  • 7
  • 1
    Why waste the 80 bytes for a string when a single char is all that's required for a y/n response? Just `getchar()` instead of `scanf()` – Mike Oct 01 '12 at 17:33
  • 1
    Never call `fflush` on `stdin` - it results in Undefined Behaviour. Also, why are you making your local variables `static` ? – Paul R Oct 01 '12 at 17:34
  • Here's the fundamental error: *"%s"* is not magical. It's just the literal string with a percent sign and an s. So *"%s" == "y"* will always be false, as will *"%s" == "n"*. – dyoo Oct 01 '12 at 18:16
  • See also [Using `fflush(stdin)`](http://stackoverflow.com/questions/2979209/using-fflushstdin). – Jonathan Leffler Sep 15 '16 at 05:33

4 Answers4

6

You have two problems in your comparison: if ("%s" == "y"):

  • "%s" is your scanf format string. If scanf succeeds in reading the input, then the result is in your variable: string.
  • Don't use == to compare strings. You should use strcmp.

Because you use a comparison of this form in both if tests, neither branch executes, and you see no output.

Also don't call fflush on stdin. You may intend fflush(stdout) there.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
pb2q
  • 58,613
  • 19
  • 146
  • 147
  • OK, so after modifying the program from this and also using Mike's example, the linker throws out a mental error - http://pastebin.com/QA3eFHiR – islandmonkey Oct 01 '12 at 19:02
  • OK, so my big error has turned into a small one (I needed to include stdlib.h and string.h:) `shaqiri.c:10:13: error: expected ‘)’ before string constant shaqiri.c:12:18: error: expected ‘)’ before string constant ` – islandmonkey Oct 02 '12 at 15:09
  • @islandmonkey look at your `strcmp` call: `(strcmp "input", n)`. You have problems there: 1) not a valid function call; 2) the bare `n` isn't a variable or a string. Don't you want `"n"` there? – pb2q Oct 02 '12 at 17:19
0

That's not how you compare strings in C, you need to use strcmp. Also, you need to compare the variable string, not "%s".

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

scanf() stores the returned values in the second parameter. See here. Second you're compairing the strings wrong... use strcmp

I'm just going to throw this out there as a simplification to your program:

  1. using a single char to store the single char input
  2. you get to keep your conditionals the way you wanted them now "=="
  3. removed the fflush of stdin, you shouldn't be doing that anyway.
  4. added a "catch all" case for non 'y'/'n' input values.

Here's the modified code:

#include <stdio.h>  
int main() {
    char input;
    printf( "ARE YOU SHAQIRI? [y/n]: " );
    input = getchar();
    if (input == 'y')
        printf("That's impossible. YOU CANNOT BE SHAQIRI YOU IDIOT");
    else if (input == 'n')
        printf("I thought not.");
    else
        printf("Not valid input...");
    return 0; 
} 
Mike
  • 47,263
  • 29
  • 113
  • 177
0

C doesn't have built in operators for comparing char arrays, therefore you call strcmp, like this:

if(strcmp(string, "yes") == ) 
{
    /* User said yes */
}

Make sure to #include <string.h>

Ryan
  • 14,392
  • 8
  • 62
  • 102