Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

WHY DOES THIS CODE OUTPUT 3E3 INSTEAD OF 25 elm st. Enter your first name: john Enter your last name: smith Enter your street address:

WHY DOES THIS CODE OUTPUT 3E3 INSTEAD OF 25 elm st. Enter your first name: john Enter your last name: smith Enter your street address: 25 elm st. Enter your city: toronto Enter your province: on Enter your postal code: m1e 3e3 You entered: john smith 3E3 toronto on M1E 3E3


#include #include #include #define MAX_NAME_SIZE 50 #define MAX_ADDRESS_SIZE 100 #define MAX_CITY_SIZE 50 #define MAX_PROVINCE_SIZE 50 #define MAX_POSTAL_CODE_SIZE 7 struct customerInfo { char firstName[MAX_NAME_SIZE]; char lastName[MAX_NAME_SIZE]; char streetAddress[MAX_ADDRESS_SIZE]; char city[MAX_CITY_SIZE]; char province[MAX_PROVINCE_SIZE]; char postalCode[MAX_POSTAL_CODE_SIZE]; }; int isValidPostalCode(char* postalCode) { char formattedCode[MAX_POSTAL_CODE_SIZE]; int i, j = 0; // Convert the postal code to uppercase for (i = 0; postalCode[i]; i++) { postalCode[i] = toupper(postalCode[i]); formattedCode[j++] = postalCode[i]; } formattedCode[j] = '0'; // Check if the formatted code is valid with or without a space if ((strlen(formattedCode) == 7 && isalpha(formattedCode[0]) && isdigit(formattedCode[1]) && isalpha(formattedCode[2]) && formattedCode[3] == ' ' && isdigit(formattedCode[4]) && isalpha(formattedCode[5]) && isdigit(formattedCode[6])) || (strlen(formattedCode) == 6 && isalpha(formattedCode[0]) && isdigit(formattedCode[1]) && isalpha(formattedCode[2]) && isdigit(formattedCode[3]) && isalpha(formattedCode[4]) && isdigit(formattedCode[5]))) { // Copy the formatted code back to the original postalCode strcpy(postalCode, formattedCode); return 1; } else { return 0; } } void clearInputBuffer() { int c; while ((c = getchar()) != ' ' && c != EOF); } struct customerInfo getCustomerInfo(){ struct customerInfo customer; // Get first name printf("Enter your first name: "); scanf(" %[^ ]", customer.firstName); getchar(); do { // Get last name printf("Enter your last name: "); fgets(customer.lastName, sizeof(customer.lastName), stdin); customer.lastName[strcspn(customer.lastName, " ")] = '0'; if (customer.lastName[0] == '0') { printf("Invalid Entry: "); } } while (customer.lastName[0] == '0'); // Get street address printf("Enter your street address: "); scanf(" %[^ ]", customer.streetAddress); getchar(); // Get city printf("Enter your city: "); scanf(" %[^ ]", customer.city); getchar(); // Get province printf("Enter your province: "); scanf(" %[^ ]", customer.province); getchar(); do { printf("Enter your postal code: "); scanf(" %[^ ]", customer.postalCode); getchar(); if (!isValidPostalCode(customer.postalCode)) { printf("Invalid Entry: "); clearInputBuffer(); } } while (!isValidPostalCode(customer.postalCode)); return customer; clearInputBuffer(); } int main() { struct customerInfo customer = getCustomerInfo(); // Print the entered information printf("You entered: %s %s %s %s %s %s ", customer.firstName, customer.lastName, customer.streetAddress, customer.city, customer.province, customer.postalCode); return 0; } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

The code outputs 3E3 instead of 25 elm st because of an issue in the getCustomerInfo function After ... 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

Step: 3

blur-text-image

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

Income Tax Fundamentals 2019

Authors: Gerald E. Whittenburg, Steven Gill

37th Edition

1337703060, 1337673218, 9781337673211, 978-1337703062

More Books

Students also viewed these Algorithms questions

Question

Why do some individuals confess to a crime they did not commit?

Answered: 1 week ago