Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello! I need help debugging my C code for the below question: Here is what I have so far: include //C program that takes a

Hello! I need help debugging my C code for the below question:

image text in transcribed

Here is what I have so far:

include

//C program that takes a phone number as standard input from the user and forma$

int main(void){

int a, b, c, d, e, f, g, h, i, j, y;

char x;

printf("Input phone number: ");

y = scanf("%d %d %d %d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f, &g, &h, &i, $

//Error check phone number and return 1 if not 10 digits long

if(y != 10){

printf("Error: bad input! ");

return 1;

}

printf("Format Options: A.Local B. Domestic C.International D. Odd"$

printf(" Input Option: ");

scanf("%c", &x);

//Check if input is A or a

if(x == 'A' || x == 'a'){

//Print phone number with no area code

printf("%d%d%d-%d%d%d%d", d, e, f, g, h, i, j);

return 0;

}

//Check if input is B or b

else if(x == 'B' || x == 'b'){

printf("(%d%d%d) %d%d%d-%d%d%d%d", a, b, c, d, e, f, g, h, i, j);

return 0;

}

//Check if input is C or c

else if(x == 'C' || x == 'c'){

printf("+1-%d%d%d-%d%d%d-%d%d%d%d", a, b, c, d, e, f, g, h, i, j);

return 0;

}

//Check if input is D or d

else if(x == 'D' || x == 'd'){

printf("|%6.4d|%d%d%d", a, b, c);

printf("|%6.4d|%d%d%d", d, e, f);

printf("|%6.4d|%d%d%d%d", g, h, i, j);

return 0;

}

//Error check format option and return 2 if invalid

if(x != 'A' && x != 'a' && x != 'B' && x != 'b' && x != 'C' && x != 'c' && x !=$

printf("Error: invalid format option ");

return 2;

}

}

I have no errors but have 3 warnings for the final format option and the program exits prematurely because it thinks the code is invalid before I have even entered one. Thank you!!!!

Question 1 (23 Marks) Create a C program named phone.c that takes as input (over standard input) a 10 digit phone number and a single character. The format of the phone number will be DDDDDDDDDD, that is, 10 digits in a row with no spaces or other characters. The character will be a single uppercase or lowercase letter between A and D (e.g. A, b, c, D, d, etc) Your program will display a menu of options and ask the user for input as shown in the Example Input/Output below (at the end of this question) and format the number based on the menu option (character) input by the user. The following table describes how to format the phone number for each menu option: Phone Number Format Local format: DDD-DDDD omit the area code (first 3 numbers). For example, 5199145555 would become 914-5555. Domestic: (DDD) DDD-DDDD For example, 5199145555 would become (519) 914-5555. International: +1-DDD-DDD-DDDD assume country code is 1. For example, 5199145555 would become +1-519-914-5555. Odd: ODDD ODDD DDDD, use the minimum with specifier to output each part of the phone number with at least 6 spaces and use the precision specifier to output each number with at least 4 digits. Do not pad the numbers manually (allow the specifier and printf to do this for you). For example, 5199145555 would become .0519.09145555 (spaces added by the minimum width specifier shown as., red numbers added by precision specifier) Menu Option A or a B or b or C D or d Any other characterPrint an error message and exit with an exit status that indicates failure. You are required to do error checking to test for invalid input. You must check the return of scanf and print an error if it aborted early. You must also check that no part of the input number is negative and that the input option is valid. If an error is encountered print an error message and exit with a non-zero exit status (to denote failure), otherwise exit with an exit status of 0 (to denote success) Hint: You do not need to use strings, arrays, pointers or loops for this question. If you are, you are likely over thinking it

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

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

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions

Question

What do they do well?

Answered: 1 week ago