Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » Problem with string handling using scanf() and gets() function.(How to manipulate strings with white space while taken as input under user defined functions?)
Problem with string handling using scanf() and gets() function. [message #1698736] Wed, 17 June 2015 14:31
SHIBAM DEBBARMA is currently offline SHIBAM DEBBARMAFriend
Messages: 1
Registered: June 2015
Location: INDIA
Junior Member
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.
Previous Topic:Add some parts of CDT as dependency
Next Topic:How to customize C/C++ Code Template
Goto Forum:
  


Current Time: Tue Apr 16 20:17:23 GMT 2024

Powered by FUDForum. Page generated in 1.58048 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top