I need to make a C program to read name and marital status of a girl and print her name with Miss or Mrs.
It works fine with this code:
#include <stdio.h>
#include <string.h>
int main()
{
// Declare a char buffer to take input for name
char name[30]={0};
// Declare a char buffer to take input for answer
char YesNo[10]={0};
//input name
printf("Enter the name of a girl : ");
gets(name);
//input marital status
printf("Is the girl married (Y-Yes, N-No) : ");
gets(YesNo);
if((!strcmp(YesNo,"yes")) || (!strcmp(YesNo,"Y")))
printf("Her full name is : Mrs. %s",name);
else if((!strcmp(YesNo,"no")) || (!strcmp(YesNo,"N")))
printf("Her full name is : Miss %s",name);
else
printf("Marital status is wrong");
return 0;
}
But I want to know what is problem in this code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char name[100],mstatus=[30];
printf("Enter the name of the girl!\n");
scanf("%c",&name);
printf("whether the girl is married (Enter 'Y' for Yes and 'N' for No)!\n");
scanf("%c",&mstatus);
if(mstatus=='Y')
{
printf("Full name of girl is Mrs %c:",name);
}
else
{
printf("Full name of girl is Miss %c:",name);
}
return 0;
}
Why we only have to use gets
and not scanf
, and what is the use of strcmp
?