F
FakeAddress
I have two problems with a program. One is related to fgets reading
in the new line and the other is reading in a number that begins with
a zero.
===============================================
The dialog with the user should be as follows:
===============================================
Enter name: Minnie Mouse
Enter street address: 100 Disney Drive
Enter city: Orlando
Enter state: FL
Enter zip code: 99990
Enter age: 25
Enter gender (M or F): F
Enter name: Big Bird
Enter street address: 10 Sesame Street
Enter city: Funtown
Enter state: MA
Enter zip code: 01222
Enter age: 20
Enter gender (M or F): M
The information you entered is:
Minnie Mouse
100 Disney Drive
Orlando, FL 99990
She is 25 years old.
Big Bird
10 Sesame Street
Funtown, MA 01222
He is 20 years old.
==================================================
When I input the information, I get the following results:
==================================================
The information you entered is:
Minnie Mouse
100 Disney Drive
Orlando
, FL 01222
She is 25 years old.
Big Bird
10 Sesame Street
Funtown
, MA 01222
He is 20 years old.
There are two problems. First, I don’t want the new line after the
city. I know it is because the fgets function is reading in the
newline, but I don't know how to delete it from the string. Also,
when I enter a zip code with a leading zero, it takes up both spots
(the first zip code should be 99990 not 01222). Here are the
suggestions regarding the input and output of the zip code given by my
instructor:
* If you use %li (instead of %ld) as the format specifier for the "zip
code" and if you enter your zip code starting with the number 0
(zero), C will interpret that number as "octal" and which will cause
erroneous results. The "%i" format specifier says to interpret any
number starting with a zero as octal. For this reason, I recommend
that you use %ld in the scanf statement when prompting the user for
the zip code.
* The above hint handles the zip code as it is being "input" using a
scanf statement. You have a different problem when you want to
"output" the zip code using a printf statement. C does not store
leading zeros. So, if the user in fact enters a zip code starting with
a zero, you need to do some extra formatting when you output the zip
code to display leading zeros if any. We have not yet covered this, so
I will give you the solution. You have 2 choices: Either: "%.5ld" or
"%05ld". Both of those format specifiers indicate that you want C to
reserve 5 spaces for the output, and if the integer value is less than
5 characters, to pad the integer with leading zeros.
========================
Here is my code so far:
========================
#include <stdio.h>
#include <string.h>
struct info
{
char name[30];
char address[30];
char city[20];
char state[3];
long int zip;
int age;
char gender;
};
int main (void)
{
int i;
char M = 'M';
struct info people[2];
for (i = 0; i < 2; i++)
{
fflush (stdin);
printf ("Enter Name: ");
fgets(people.name, sizeof(people.name), stdin);
fflush (stdin);
printf ("Enter street address: ");
fgets(people.address, sizeof(people.address),
stdin);
fflush (stdin);
printf ("Enter city: ");
fgets(people.city, sizeof(people.city), stdin);
fflush (stdin);
printf ("Enter state: ");
fgets(people.state, sizeof(people.state),
stdin);
fflush (stdin);
printf ("Enter zip code: ");
scanf ("%ld", &people.zip);
fflush (stdin);
printf ("Enter age: ");
scanf ("%i", &people.age);
fflush(stdin);
printf ("Enter gender (M or F): ");
scanf ("%c", &people.gender);
fflush(stdin);
printf ("\n");
} /* End for loop */
printf ("The information you entered is:\n\n");
for (i = 0; i < 2; i++)
{
printf ("%s",people.name);
printf ("%s",people.address);
printf ("%s, ",people.city);
printf ("%s",people.state);
printf (" %.5ld\n", people[1].zip);
if (people.gender == 'M')
{
printf ("He is %i years old.\n",
people.age);
}
else
{
printf ("She is %i years old.\n",
people.age);
}
printf ("\n");
} /* End for loop */
return 0;
} /* End main */
in the new line and the other is reading in a number that begins with
a zero.
===============================================
The dialog with the user should be as follows:
===============================================
Enter name: Minnie Mouse
Enter street address: 100 Disney Drive
Enter city: Orlando
Enter state: FL
Enter zip code: 99990
Enter age: 25
Enter gender (M or F): F
Enter name: Big Bird
Enter street address: 10 Sesame Street
Enter city: Funtown
Enter state: MA
Enter zip code: 01222
Enter age: 20
Enter gender (M or F): M
The information you entered is:
Minnie Mouse
100 Disney Drive
Orlando, FL 99990
She is 25 years old.
Big Bird
10 Sesame Street
Funtown, MA 01222
He is 20 years old.
==================================================
When I input the information, I get the following results:
==================================================
The information you entered is:
Minnie Mouse
100 Disney Drive
Orlando
, FL 01222
She is 25 years old.
Big Bird
10 Sesame Street
Funtown
, MA 01222
He is 20 years old.
There are two problems. First, I don’t want the new line after the
city. I know it is because the fgets function is reading in the
newline, but I don't know how to delete it from the string. Also,
when I enter a zip code with a leading zero, it takes up both spots
(the first zip code should be 99990 not 01222). Here are the
suggestions regarding the input and output of the zip code given by my
instructor:
* If you use %li (instead of %ld) as the format specifier for the "zip
code" and if you enter your zip code starting with the number 0
(zero), C will interpret that number as "octal" and which will cause
erroneous results. The "%i" format specifier says to interpret any
number starting with a zero as octal. For this reason, I recommend
that you use %ld in the scanf statement when prompting the user for
the zip code.
* The above hint handles the zip code as it is being "input" using a
scanf statement. You have a different problem when you want to
"output" the zip code using a printf statement. C does not store
leading zeros. So, if the user in fact enters a zip code starting with
a zero, you need to do some extra formatting when you output the zip
code to display leading zeros if any. We have not yet covered this, so
I will give you the solution. You have 2 choices: Either: "%.5ld" or
"%05ld". Both of those format specifiers indicate that you want C to
reserve 5 spaces for the output, and if the integer value is less than
5 characters, to pad the integer with leading zeros.
========================
Here is my code so far:
========================
#include <stdio.h>
#include <string.h>
struct info
{
char name[30];
char address[30];
char city[20];
char state[3];
long int zip;
int age;
char gender;
};
int main (void)
{
int i;
char M = 'M';
struct info people[2];
for (i = 0; i < 2; i++)
{
fflush (stdin);
printf ("Enter Name: ");
fgets(people.name, sizeof(people.name), stdin);
fflush (stdin);
printf ("Enter street address: ");
fgets(people.address, sizeof(people.address),
stdin);
fflush (stdin);
printf ("Enter city: ");
fgets(people.city, sizeof(people.city), stdin);
fflush (stdin);
printf ("Enter state: ");
fgets(people.state, sizeof(people.state),
stdin);
fflush (stdin);
printf ("Enter zip code: ");
scanf ("%ld", &people.zip);
fflush (stdin);
printf ("Enter age: ");
scanf ("%i", &people.age);
fflush(stdin);
printf ("Enter gender (M or F): ");
scanf ("%c", &people.gender);
fflush(stdin);
printf ("\n");
} /* End for loop */
printf ("The information you entered is:\n\n");
for (i = 0; i < 2; i++)
{
printf ("%s",people.name);
printf ("%s",people.address);
printf ("%s, ",people.city);
printf ("%s",people.state);
printf (" %.5ld\n", people[1].zip);
if (people.gender == 'M')
{
printf ("He is %i years old.\n",
people.age);
}
else
{
printf ("She is %i years old.\n",
people.age);
}
printf ("\n");
} /* End for loop */
return 0;
} /* End main */