Question: My code has a problem where it validates the postal code before the user inputs data thus resulting in the error message triggering it should

My code has a problem where it validates the postal code before the user inputs data thus resulting in the error message triggering it should be just as the corrected output i need some help please. this is coded in C
#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, "
")]='\0';
while (1){
printf("Enter your last name: ");
fgets(customer.last_name, sizeof(customer.last_name), stdin);
customer.last_name[strcspn(customer.last_name, "
")]='\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, "
")]='\0';
printf("Enter your city: ");
fgets(customer.city, sizeof(customer.city), stdin);
customer.city[strcspn(customer.city, "
")]='\0';
printf("Enter your province: ");
fgets(customer.province, sizeof(customer.province), stdin);
customer.province[strcspn(customer.province, "
")]='\0';
while (1){
printf("Enter your postal code: ");
fgets(customer.postal_code, sizeof(customer.postal_code), stdin);
customer.postal_code[strcspn(customer.postal_code, "
")]='\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("
You entered:
%s %s
%s,
%s,%s,
%s
",
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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!