Question
Is there a function that could reduce repetition? Possible with the yesNo ...? void getAddress(struct Address *address) { char yesNo; do { printf(Please enter the
Is there a function that could reduce repetition? Possible with the yesNo ...?
void getAddress(struct Address *address)
{
char yesNo;
do {
printf("Please enter the contact's street number: ");
scanf("%d%*c", &address->streetNumber);
} while (address->streetNumber < 1);
printf("Please enter the contact's street name: ");
scanf("%40[^ ]%*c", address->street);
printf("Do you want to enter an apartment number? (y or n): ");
scanf("%c%*c", &yesNo);
if (yesNo == 'y' || yesNo == 'Y') { // change to single quotes
do {
printf("Please enter the contact's apartment number: ");
scanf("%d%*c", &address->apartmentNumber);
} while (address->apartmentNumber < 1);
}
printf("Please enter the contact's postal code: ");
scanf("%7[^ ]%*c", address->postalCode);
printf("Please enter the contact's city: ");
scanf("%40[^ ]%*c", address->city);
}
void getNumbers(struct Numbers *numbers)
{
char yesNo;
printf("Do you want to enter a cell phone number? (y or n): ");
scanf("%c%*c", &yesNo);
if (yesNo == 'y' || yesNo == 'Y') {
printf("Please enter the contact's cell phone number: ");
scanf("%10[^ ]%*c", numbers->cell);
}
printf("Do you want to enter a home phone number? (y or n): ");
scanf("%c%*c", &yesNo);
if (yesNo == 'y' || yesNo == 'Y') { // change && to ||
printf("Please enter the contact's home phone number: ");
scanf("%10[^ ]%*c", numbers->home);
}
printf("Do you want to enter a business phone number? (y or n): ");
scanf("%c%*c", &yesNo);
if (yesNo == 'y' || yesNo == 'Y') {
printf("Please enter the contact's business phone number: ");
scanf("%10[^ ]%*c", numbers->business);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started