Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases DeMYSTiFieD

Authors: Andy Oppel

2nd Edition

0071747990, 978-0071747998

More Books

Students also viewed these Databases questions

Question

How to find if any no. is divisble by 4 or not ?

Answered: 1 week ago

Question

Explain the Pascals Law ?

Answered: 1 week ago

Question

What are the objectives of performance appraisal ?

Answered: 1 week ago

Question

How many Tables Will Base HCMSs typically have? Why?

Answered: 1 week ago

Question

What is the process of normalization?

Answered: 1 week ago