Hi, I have to write a program in C to enter basic information about a customer into several string variables. This is the criteria I have to follow:
This is what I have:Quote:
Write a program in C to enter basic information about a customer into several string variables. Use printf() for screen output and the fixgets() function to input each field. Combine all of the individual strings to one string for output as illustrated. This will require the use of strcat() and strcpy() as demonstrated in class. The field length is listed on the sample screen. Remember that the variable is declared 1 larger to accomodate the NULL. The combined field should be 133+1 characters. The input locations should be in the same column, as illustrated. Also, the char array for each field is one greater than the field length to accommodate the NULL.
Basic Input Screen (space) Field Length (do not display on screen)
Customer Number A12345 (space) 6
First Name James (space) 15
Middle Initial E (space) 1
Last Name Jones (space) 25
Address 234 S. Maple Street (space) 30
City San Fran (space) 15
State Ca (space) 2
Zip Code 12345 (space) 10
Phone 345-123-4567 (space) 12
----------Output--------
A12345 / Jones, James E / 234 S. Maple Street / San Fran, Ca 12345 / 345-123-4567
The problem I have is that in the output section all it lists is the phone number, not "A12345 / Jones, James E / 234 S. Maple Street / San Fran, Ca 12345 / 345-123-4567". Not sure what I'm doing wrong. Any help is appreciated.Code:/* basicinput.c by Aron, 11/21/05 */
/* This program shows basic information about a customer using strings
* The strings are copied inton on string at the end */
#include <stdio.h>
#include <string.h>
int fixgets(char *, int ,FILE *);
int main ()
{
/* Declarations */
char cusnum[7], fname[16], mi[2], lname[26], address[31], city[16], st[3], zcode[11], phone[13];
char combo[134];
/* Input Section */
printf("Customer Number ");
fixgets(cusnum, 7, stdin);
printf("\nFirst Name ");
fixgets(fname, 16, stdin);
printf("\nMiddle Initial ");
fixgets(mi, 2, stdin);
printf("\nLast Name ");
fixgets(lname, 26, stdin);
printf("\nAddress ");
fixgets(address, 31, stdin);
printf("\nCity ");
fixgets(city, 16, stdin);
printf("\nState ");
fixgets(st, 3, stdin);
printf("\nZip Code ");
fixgets(zcode, 11, stdin);
printf("\nPhone ");
fixgets(phone, 13, stdin);
/* Processing */
strcpy(combo, cusnum);
strcat(combo, " / ");
strcpy(combo, lname);
strcat(combo, ", ");
strcpy(combo, fname);
strcpy(combo, mi);
strcat(combo, " / ");
strcpy(combo, address);
strcat(combo, " / ");
strcpy(combo, city);
strcat(combo, ", ");
strcpy(combo, st);
strcat(combo, " ");
strcpy(combo, zcode);
strcat(combo, " / ");
strcpy(combo, phone);
/* Output */
printf("\n%s\n", combo);
return 0;
}
/* This routine fixes fgets incorporation of line feeds and other
* ASCII values less than 32 into an input string. Written by H. Brown.
*/
int fixgets(char input_array[], int length, FILE *file_name)
{
register int position=0;
if (fgets(input_array,length,file_name) != NULL)
{
while(input_array[position] != 0)
{ if (input_array[position] < 32) input_array[position]=0;
else ++position;
}
}
/* The length of the string is returned. */
fflush(stdin);
return position;
}
Thanks
