Originally posted by Elite
Hi, I'm new here. I've been trying to make this program work but i'm not sure how to store text user input. This program seems to miss the first statement no matter what the input. Sorry for being such a noob
Code:
#include <stdio.h>
char c[6];
char kate[7] = "kate";
main()
{
printf("What is your name : ");
scanf("%c",c);
if(c == kate) printf("Thats a great name");
else printf("What a $$$$ name");
return 0;
}
Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
char ustr[30], cstr[] = "Kate", *p;
printf("What is your name: ");
fgets(ustr, sizeof(ustr), stdin);
if((p = strchr(ustr, '\n') != NULL)
*p = '\0'; /* strip newline if present */
if(strcmp(ustr, cstr) == 0)
printf("%s, that's a great name!", ustr);
else
printf("%s, isn't a bad name either", ustr);
return 0;
}
If you want not to worry about case in the user entry such as "kate" being the same as "Kate", "KaTe", or "KATE", then use stricmp