accepting long strings with spaces
I am using the Visual C++ compiler, and running on WIN2000.
I am trying to accept input fields like address, city, and state.
States are usually 2 letter initials, but address and cities may invlove more than 2 characters and may have a space between each word.
How do I accept input from the user when the input involves long strings with spaces between each word? I can't seem to allow input from address and city both at the same time.
I found a way to accept the address, but when I use the same stratagy for the city, it doesn't work. Please help!
Check it out:
Code:
char strPatientAddress[BUFSIZ];
char strPatientCity[BUFSIZ];
char strPatientState[50];
char *p;
char *a;
char *fgets(char *s, int size, FILE *stream);
printf(" Please enter in data for the following:\n\n");
printf(" Address: ");
fgets( strPatientAddress, sizeof(strPatientAddress), stdin);
//checks to see if variable contains a value
if (fgets(strPatientAddress, sizeof(strPatientAddress), stdin) != NULL)
{
//test for and remove newline character
if ((p = strchr(strPatientAddress, '\n')) != NULL)
*p = '\0';
}
printf("\n");
printf(" City: ");
fgets( strPatientCity, sizeof(strPatientCity), stdin);
//checks to see if variable contains a value
if (fgets(strPatientCity, sizeof(strPatientCity), stdin) != NULL)
{
//test for and remove newline character
if ((a = strchr(strPatientCity, '\n')) != NULL)
*a = '\0';
}
printf("\n");
printf(" State Initials: ");
scanf(" %s", strPatientState);
printf("\n");
Mod edit: Please review your posts to make sure your tags are correct