Invalid Entry message was placed before checking the validity of the postal code. This caused the message
Question:
"Invalid Entry" message was placed before checking the validity of the postal code. This caused the message to be printed even if the postal code was valid in my output. I need some help for my code to work as corrected output
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
struct Customer {
char first_name[50];
char last_name[50];
char street_address[100];
char city[50];
char province[3];
char postal_code[8];
};
int isValidPostalCode(char postal_code[7]);
struct Customer getCustomerInfo(void) {
struct Customer customer;
printf("Enter your first name: ");
fgets(customer.first_name, sizeof(customer.first_name), stdin);
customer.first_name[strcspn(customer.first_name, "\n")] = '\0';
while (1) {
printf("Enter your last name: ");
fgets(customer.last_name, sizeof(customer.last_name), stdin);
customer.last_name[strcspn(customer.last_name, "\n")] = '\0';
if (strlen(customer.last_name) > 0) {
break;
} else {
printf("Invalid Entry: ");
}
}
printf("Enter your street address: ");
fgets(customer.street_address, sizeof(customer.street_address), stdin);
customer.street_address[strcspn(customer.street_address, "\n")] = '\0';
printf("Enter your city: ");
fgets(customer.city, sizeof(customer.city), stdin);
customer.city[strcspn(customer.city, "\n")] = '\0';
printf("Enter your province: ");
fgets(customer.province, sizeof(customer.province), stdin);
customer.province[strcspn(customer.province, "\n")] = '\0';
while (1) {
printf("Enter your postal code: ");
fgets(customer.postal_code, sizeof(customer.postal_code), stdin);
customer.postal_code[strcspn(customer.postal_code, "\n")] = '\0';
if (isValidPostalCode(customer.postal_code)) {
break;
} else {
printf("Invalid Entry: ");
}
}
return customer;
}
int isValidPostalCode(char postal_code[8]) {
int i;
for (i = 0; i < 6; i++) {
postal_code[i] = toupper(postal_code[i]);
}
if (isalpha(postal_code[0]) && isdigit(postal_code[1]) && isalpha(postal_code[2]) &&
postal_code[3] == ' ' &&
isdigit(postal_code[4]) && isalpha(postal_code[5]) &&
strlen(postal_code) == 7) {
return 1;
} else {
return 0;
}
}
int main(void) {
struct Customer customer = getCustomerInfo();
printf("\nYou entered:\n%s %s\n%s,\n%s, %s,\n%s\n",
customer.first_name, customer.last_name,
customer.street_address,
customer.city, customer.province,
customer.postal_code);
return 0;
}
My Output
Enter your first name: John
Enter your last name:
Invalid Entry: Enter your last name: Smith
Enter your street address: 25 Elm St.
Enter your city: Toronto
Enter your province: ON
Enter your postal code: Invalid Entry: Enter your postal code: m2e44x
Invalid Entry: Enter your postal code: m2e 4x4
You entered:
John Smith
25 Elm St.,
Toronto, ON,
M2E 4X4
Program ended with exit code: 0
Corrected Output
Enter your first name: John
Enter your last name:
Invalid Entry: Enter your last name: Smith
Enter your street address: 25 Elm St.
Enter your city: Toronto
Enter your province: ON
Enter your postal code: m2e44x
Invalid Entry: Enter your postal code: m2e 4x4
You entered:
John Smith
25 Elm St.,
Toronto, ON,
M2E 4X4
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill