Problem with string handling using scanf() and gets() function. [message #1698736] |
Wed, 17 June 2015 14:31 |
|
Here is my code,
#include <stdio.h>
#include <stdlib.h>
char membershipstatus();
char checkmembership(char check);
void enterdetails(int status);
int main(void)
{
setvbuf(stdout, NULL, _IONBF, 0);
int status;
char check;
check=membershipstatus();
status=checkmembership(check);
enterdetails(status);
return 0;
}
char membershipstatus()
{
char a;
printf("Enter your membership status: (Y/N)");
scanf("%c",&a);
printf("a=%c\n",a);
return a;
}
char checkmembership(char check)
{
if (check=='Y')
return 0;
else if(check=='N')
{
printf("\nPlease get your membership");
return 1;
}
else
{
printf("\nInvalid input by user");
return 3;
}
}
void enterdetails(int status)
{
setvbuf(stdout, NULL, _IONBF, 0);
char name[30],address[200];
int custid;
if(status==0)
{
printf("\nEnter Customer ID: ");
scanf("%d",&custid);
printf("\nEnter Address: ");
scanf("%s",address); OR gets(address);
printf("\nEnter name: ");
scanf("%s",name); OR gets(name);
printf("\n\nYour Name: %s\nAddress: %s\nCustomer ID: %d",name,address,custid);
}
}
The outputs are as follows:
Output when scanf() function is used:-
Enter your membership status: (Y/N)Y
a=Y
Enter Customer ID: 2892482
Enter Address: COLONEL HOUSE
Enter name:
Your Name: HOUSE
Address: COLONEL
Customer ID: 2892482
White space is considered as a delimiter. Any formatting than will consider white space as a character under scanf() function?
Output when gets() is used:-
Enter your membership status: (Y/N)Y
a=Y
Enter Customer ID: 28724777
Enter Address:
Enter name: SHIBAM DEBBARMA
Your Name: SHIBAM DEBBARMA
Address:
Customer ID: 28724777
Input for address gets automatically skipped. What is the reason? If I swap the input sequence for the strings then input for name gets skipped. Whichever comes 1st gets skipped.
|
|
|
Powered by
FUDForum. Page generated in 0.02697 seconds