Quote:
char a;
printf("Enter the Name\n");
scanf("%c",&a);
change to
char a[256];
printf("Enter the name\n");
fgets(a, 256, stdin);
Quote:
if(a=='xyz')
{
printf("You enter xyz");
}
if(a=='abc')
{
printf("You enter abc");
}
change to:
if (strcmp(a, "xyz") == 0)
{
printf("You entered xyz\n");
}
else if (strcmp(a, "abc")==0)
{
printf("You entered abc\n");
}
You may also simply write:
printf("You entered %s\n", a);
Note you must include
string.h
in order to use
strcmp
.
[UPDATE]
You are right, I forgot the
"annoying newline NOT discarded by fgets". Try:
#include <stdio.h>
#include <string.h>
int main() {
char a[256]={0};
int len;
printf("Enter the Name\n");
if ( fgets(a,256,stdin) )
len = strlen(a);
if (len>0 && a[len-1]=='\n')
a[len-1]='\0';
if (strcmp(a, "xyz") == 0)
{
printf("You entered xyz\n");
}
else if (strcmp(a, "abc")==0)
{
printf("You entered abc\n");
}
printf("'%s'\n", a);
return 0;
}
[/UPDATE]