I'm working on an assignment that takes some inputs for an address book. However, I have problems getting the input to read properly. Here's the section so far:
Code:
char addcontact ()
{
auto char input1[100], input2[100], input3[100], input4[6], input5[10];
char c;
int cnt = 0;
fflush (stdin);
printf("First Name: ");
fgets(input1, 100, stdin);
strcpy (InfoList[listcnt].firstname, input1);
printf("Last Name: ");
fgets(input2, 100, stdin);
strncpy (InfoList[listcnt].lastname, input2, cnt);
printf("Address: ");
while ((c = getchar()) != '\n')
fgets(input3, 100, stdin);
strncpy (InfoList[listcnt].address, input3, cnt);
printf("Postal Code: ");
fgets(input4, 7, stdin);
strcpy (InfoList[listcnt].pcode, input4);
printf("Phone Number: ");
fgets(input5, 11, stdin);
strcpy (InfoList[listcnt].phone, input5);
listcnt++;
menu();
}
The input:
Code:
First Name: John
Last Name: Doe
Address: 123 Street
Postal Code: A1B 2C3
Phone Number:
And the output:
Code:
Contact 1
First Name: John
Last Name:
Address:
Postal Code: A1B- 2C
Phone Number: (3
) -
I have to press return twice to get past the address section, and it won't even let me enter a phone number. After that, it ignores the address and for some reason overflows the postal code into the phone number. Any idea what's wrong here?