I am creating a mailing label generator for school and am having an issue with a few problems. My program is to take the full name, address, city, state, and zip code for individuals from 0 to 10. When running my program I am having two major problems. The for loop skips the full name "safergets()" and moves on to the address safergets. I moved on to see if everything else works, but my verification for the zip code would not work correctly. I added a printf to see if the input was the same number and found it to be bogus. Also, I am getting an error code for my line attempting to capitalize the state output. I'm sure I am using toupper incorrectly. Attached below is my code, error code, and output.
#include <stdio.h>
#include <ctype.h>
/* Define structure */
struct information
{
char full_name[35], address[50], city[25], state[3];
long int zip_code;
};
/* Function safer_gets */
/* ------------------- */
void safer_gets (char array[], int max_chars)
{
/* Declare variables. */
/* ------------------ */
int i;
/* Read info from input buffer, character by character, */
/* up until the maximum number of possible characters. */
/* ------------------------------------------------------ */
for (i = 0; i < max_chars; i++)
{
array[i] = getchar();
/* If "this" character is the carriage return, exit loop */
/* ----------------------------------------------------- */
if (array[i] == '
')
break;
} /* end for */
/* If we have pulled out the most we can based on the size of array, */
/* and, if there are more chars in the input buffer, */
/* clear out the remaining chars in the buffer. */
/* ---------------------------------------------------------------- */
if (i == max_chars )
if (array[i] != '
')
while (getchar() != '
');
/* At this point, i is pointing to the element after the last character */
/* in the string. Terminate the string with the null terminator. */
/* -------------------------------------------------------------------- */
array[i] = '';
} /* end safer_gets */
/* Begin main */
int main()
{
/* Declare variables */
struct information person[10];
int x, i;
/* Issue greeting */
printf("Welcome to the mailing label generator program.
");
/* Prompt user for number of individuals between 0 - 10. If invalid, re-prompt */
do
{
printf("How many people do you want to generate labels for (0-10)? ");
scanf("%i", &x);
if(x<0 || x>10)
printf("Invalid number. Please re-enter number. Must be from 0 to 10.
");
}while(x<0 || x>10);
/* Begin loop for individual information */
for(i = 0; i < x; i++)
{
printf("
Enter name: ");
safer_gets(person[i].full_name, 35); /* This is the step being skipped */
printf("
Enter street address: ");
safer_gets(person[i].address, 50);
printf("
Enter city: ");
safer_gets(person[i].city, 25);
printf("
Enter state: ");
gets(person[i].state);
/* Begin loop to verify correct zipcode */
do
{
printf("
Enter zipcode: ");
scanf("%ld", person[i].zip_code); /* I get a bogus number here */
if(person[i].zip_code<00001 || person[i].zip_code>99999)
{
printf("
Invalid zipcode. Must be from 00001 to 99999.");
}
}while(person[i].zip_code<00001 || person[i].zip_code>99999);
/* end loop */
}/* end of loop */
/* Output individual information in mailing format, condition for 0 individuals */
if(x>0 && x<10)
{
printf("
Below are your mailing labels:
");
}
/* Begin loop for outputting individual(s) mailing labels */
for(i = 0; i < x; i++)
{
printf("%s
",person[i].full_name);
printf("%s
",person[i].address);
printf("%s
",person[i].city);
/* Output state in all uppercase */
printf("%s
", toupper(person[i].state)); /* This is where the error code is occurring */
printf("%.5ld
", person[i].zip_code);
} /* end of loop */
printf("Thank you for using the program.
");
}/*end of main */
Error code:142: warning: passing arg 1 of `toupper' makes integer from pointer without a cast.
Output:
Welcome to the mailing label generator program.
How many people do you want to generate labels for (0-10)? 1
Enter name:
Enter street address: 100 Needhelp Ave.
Enter city: Gardner
Enter state: NY
Enter zipcode: 01420
Invalid zipcode. Must be from 00001 to 99999.
Enter zipcode:
I have looked at several questions on here to try and understand where I am going wrong, but if feel it might be a couple of issues effecting my program. Also, the safergets function was given to my class by our professor to ensure that the user does not input more characters than the array can hold. Thank you for your help and tolerance in helping me understand my mistakes!
See Question&Answers more detail:os