0

I want to check if the user input is equal to a certain value

    #include <stdio.h>
#include <windows.h>

int main()
{
    SetConsoleOutputCP(1252);
    SetConsoleCP(1252);

    char meningen[100];



    printf("Hello \n");
    printf("I want you to write 'Simon Anderson'.\n");

    scanf(" %[^\n]s", meningen);
        if (meningen == "Simon Anderson")
        {
            printf("Congratilation. You have won the game.\n");
            printf("Have a good day");

        }
        else {
            printf("You failed.\n");

        }
    getchar();
    getchar();
    return 0;
}

The problem is whatever I write I still get the "You failed" printed out. How can I make it so it returns true if the user wrote as the value in the if-statement?

Thank you

Simon
  • 81
  • 8

2 Answers2

1
if (meningen == "Simon Anderson")

should be

if (strcmp(meningen,"Simon Anderson") ==0)

I suggest you to use

fgets(meningen,sizeof(meningen),stdin);

instead of scanf() as fgets() takes care of buffer overflow and also make a note that fgets() comes with a newline character. You need to remove the newline character as shown below.

size_t n = strlen(meningen);
if(n>0 && a[n-1] == '\n')
a[n-1] = '\0';
Gopi
  • 19,784
  • 4
  • 24
  • 36
0

This isn't working because you are comparing a string with ==:

scanf(" %[^\n]s", meningen);
if (meningen == "Simon Anderson")

This is comparing the address of the meningen array, rather than the value(s) it contains. To check the contents of the array, you should use the strcmp() function instead:

scanf(" %[^\n]s", meningen);
if (strcmp(meningen, "Simon Anderson") == 0)
GoBusto
  • 4,632
  • 6
  • 28
  • 45