I am new to C and having trouble with the program below. It asks people if they are well or not and displays a message accordingly. So far I have tried using !strcmp
, strcmp
and strncmp
and none returns a positive value for if
, all skip to the else
statement. Can any one point to me where I am going wrong as the syntax seems fine to me.
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
int main()
{
char well[3];
printf("Hello, are you well today? (Yes/No) ");
scanf_s ("%s",well);
if (!strcmp(well,"Yes")){
printf("Glad to hear it, so am I\n");
}
else{
printf("Sorry to hear that, I hope your day gets better\n");
}
system("PAUSE");
return 0;
}
Many thanks to all for all the answers unfortunately none of them seem to work. Assigning a 4 to take account of the null makes no difference. Invoking scanf rather then scanf_s results in an access violation (which is odd as the rest of the program uses plain scanf. Adding a '4' parameter to scanf_s also makes no difference. Really tearing my hair out here I'm happy to accommodate the null at the end of the line but the program won't seem to recognise it.