1

Possible Duplicate:
How do I properly compare strings in C?

Although I've been sitting on this topic for months, I'm relatively new to C. I'm trying to write a simple question/response program. I know it has something to do with the if else conditions (everything else works), but I've searched and can't seem to find the problem. There's also the recursion at the end that repeats the program. The function call that I put inside of it might be wrong.

#include <stdio.h>

main()
{
    char line[100];
    char decision[100];
    printf("Are you gonna throw it?\n");
    printf("Type yes or no.\n");

    scanf("%s", line);
    printf("%s \n", line);

    if (line == "yes") {
        printf("Thanks.\n");
    } else if (line == "no") {
        printf("Why not?\n");
    }

    printf("Do you want to do this again?\n");
    scanf("%s", decision);
    if (decision == "yes") {
        main();
    };
}
Community
  • 1
  • 1
kizlink
  • 165
  • 1
  • 6

5 Answers5

2

Comparisons like line == "yes" doesn't work. You need to compare your string using strcmp, e.g.

if (strcmp(line, "yes") == 0) {
    printf("Thanks.\n");
} else if (strcmp(line, "no") == 0) {
    printf("Why not?\n");
}
Mark
  • 6,033
  • 1
  • 19
  • 14
1

To compare strings, you have to use strcmp or strncmp function from string.h

Other problem is:

main should return int.

int main()

OR

int main(int argc, char *argv[])

Or equivalent is the proper signature for main in C.

P.P
  • 117,907
  • 20
  • 175
  • 238
0

Recursion is not necessary in this case and builds up data on the stack unnecessarily for each iteration of the program. Enclose your entire code in a do {...} while (strcmp(decision, "yes") == 0) loop instead. Also change line == "yes" and line == "no" to strcmp(line, "yes") == 0 and strcmp(line, "no") == 0.

1''
  • 26,823
  • 32
  • 143
  • 200
0

First thing to know: string literals like "yes" and "no" are array expressions; the expression "yes" has type "4-element array of char" (1 extra for the 0 terminator).

Second thing to know: in most circumstances, an expression of type "N-element array of T" will be converted to an expression of type "pointer to T", and its value will be the address of the first element of the array.

When you write if (line == "yes"), both of the expressions line and "yes" are converted from type "array of char" to "pointer to char", and their values are the addresses of their first element, which will be different (even if the contents of line are "yes", the string literal "yes" lives at a different address from line). Thus the comparison always fails, regardless of the contents of line.

In order to compare the contents of the two array expressions, you will need to use the standard library function strcmp:

if (strcmp(line, "yes") == 0) { ... }

strcmp will return 0 if the two strings are equivalent, < 0 if line is lexicographically less than "yes", and > 0 if line is lexicographically greater than "yes".

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

1) remove the semicolon here:

if (decision == "yes")
{
     main();
 }; //<-- if you want to keep this code at all

2) you can't compaire strings using ==

strcmp(decision, "yes"); //returns 0 if they are equil

3) why recursivly call main()? Why not just put the whole thing in a loop:

main() { 
  char line[100];
  char decision[100] = "yes";
  while(!strcmp(decision, "yes")){
    printf("Are you gonna throw it?\n"); 
    printf("Type yes or no.\n");
    scanf("%s", line);  
    printf("%s \n", line);  
    if (!strcmp(line, "yes"))
      printf("Thanks.\n");   
    else if (!strcmp(line, "no"))     
      printf("Why not?\n");   

    printf("Do you want to do this again?\n");
    scanf("%s", decision); 
  } //end while
} //end main
Mike
  • 47,263
  • 29
  • 113
  • 177