Search code examples
cvisual-studiogets

'If' Condition Not Working As Expected In My C Code


I am fully aware that this is due to some error overlooked by me while writing my text-based calculator project in C, but I have only started learning C less than a week ago, so please help me out!

Since the entire code is 119 lines, I'll just post the necessary snippet where the real issue lies: (There are no errors during compiling, so there is no error beyond these lines)

char choice[15];
    printf("Type 'CALCULATE' or 'FACTORISE' or 'AVERAGE' to choose function :\n");
    gets(choice);
    if (choice == "CALCULATE")

The bug is that even after perfectly entering CALCULATE or FACTORISE or AVERAGE, I still get the error message that I programmed in case of invalid input (i.e, if none of these 3 inputs are entered). It SHOULD be going on to ask me the first number I wish to operate on, as written for the CALCULATE input.

The code runs fine, no errors in VS 2013, and so I'm sure its not a syntax error but rather something stupid I've done in these few lines.


Solution

  • If you use == you are comparing the addresses of 2 arrays, not the contents of the arrays.

    Instead, you need to do:

    if (strcmp(choice, "CALCULATE") == 0)