Question

Write an interactive program exam1 that loops Loop and asks the user at the end of...

Write an interactive program exam1 that loops
Loop and asks the user at the end of the loop to continue or not
Ask the user to enter an alphanumeric US phone number
Read the line of text from the screen
Check if the entered string is a valid phone number
using isValid(Line)
if it is valid number
{
call toNumeric(line) to convert it to digits
print Line after conversion
}

bool isValid(char line[])
{
//st must have 10 digits and letters
//ignore non-alpha like () . -
//first 3 should be digits like:
//800.got.milk
//use int size = strlen(line);
int alnumCount = 0;
return true or false
}

void toNumeric(char line[])
{
//use switvh statement
for size_t i=0; i<strlen(line); i++)
{
char ch = line[i];
ch = tolower(ch);
switch(ch)
{
case 'a':
case 'b':
case 'c':
line[i] = '2';
break;
.....
}
}
}

int exam1main(void)
{
Loop
read line
check if line is valid using isValid(line)
if valid call toNumber(line) to convert to digitis
printf line
Ask if you want to continue
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the program to convert to numeric phone number and necessary comments in the program.

Program:

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>

#define SIZE 30

bool isValid(char line[])
{
  
//st must have 10 digits and letters
//ignore non-alpha like () . -
//first 3 should be digits like:
//800.got.milk
//use int size = strlen(line);
int alnumCount = 0;
int size = strlen(line);
int start =0;
  
//return false if the size is less than 10
if(size < 10)
{
return false;
}
  
//return false if the line does not have numbers for first three digits
if (!((line[0] >= '0' && line[0] <= '9') && (line[1] >= '0' && line[1] <= '9') &&
(line[2] >= '0' && line[2] <= '9')))
{
return false;
}
  
//count how many alphanumeric characters are present
while(start < size)
{
if (!(line[start] == '(' || line[start] == ')' || line[start] == '.'
|| line[start] == '-'))
{
  
alnumCount++;
}
start++;
}
  
//if it has ten characters, return true else false
//printf("%d", alnumCount);
if (alnumCount==10)
{
return true;
}
  
return false;
}

void toNumeric(char line[])
{
//use switvh statement
size_t i =0;
for (i=0; i<strlen(line); i++)
{
char ch = line[i];
ch = tolower(ch);
//convert only the characters to numbers
switch(ch)
{
case 'a':
case 'b':
case 'c':
line[i] = '2';
break;
case 'd':
case 'e':
case 'f':
line[i] = '3';
break;
case 'g':
case 'h':
case 'i':
line[i] = '4';
break;
case 'j':
case 'k':
case 'l':
line[i] = '5';
break;
case 'm':
case 'n':
case 'o':
line[i] = '6';
break;
case 'p':
case 'q':
case 'r':
case 's':
line[i] = '7';
break;
case 't':
case 'u':
case 'v':
line[i] = '8';
break;
case 'w':
case 'x':
case 'y':
case 'z':
line[i] = '9';
break;
default:
break;
}
}
}

int exam1main(void)
{
int continu=1, value;
char line[SIZE]={'\0',};
char answer[5]={'\0', };
  
while (continu)
{
printf("Enter Phone Number:\n");
  
//read the user input
scanf("%s", line);
/* if (fgets(line, SIZE, stdin) == NULL)
{
printf("Error in reading phoneNumber.\n");
return -1;
}
*/
  
//check if its a valid phone number
if(isValid(line))
{
//convert to numeric format
toNumeric(line);
printf("%s\n", line);
}
else
{
printf("Invalid phone Number.\n");
  
}
  
//do we need to continue check here
printf("Would you like to continue? (y/n): ");
scanf("%s", answer);
  
/* Ask user if they want to add another employee */
if ((value = toupper(answer[0])) != 'Y')
{
continu=0;
}
else
{
continu=1;
}
}
  
return 0;
}

//invoke main from here
int main()
{
exam1main();
return 0;
}

Output:

Enter Phone Number:
800-got-milk
800-468-6455
Would you like to continue? (y/n): y
Enter Phone Number:
1800COMPUTE
Invalid phone Number.
Would you like to continue? (y/n): n

Screen Shot:

Output ScreenShot:

Add a comment
Know the answer?
Add Answer to:
Write an interactive program exam1 that loops Loop and asks the user at the end of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • Can someone please edit my current code to accept a user input for the text file...

    Can someone please edit my current code to accept a user input for the text file name in lieu of how it currently will read data1.txt. Also if the user enters a data file that isn't ready to be used, cout a statement "cout << "Could not open file " << userInputforData.txt << endl; Another condition that I need to add is if there is a special character anywhere in the word it cannot be an acceptable variable name. could...

  • Write a C program that takes two sets of characters entered by the user and merge...

    Write a C program that takes two sets of characters entered by the user and merge them character by character. Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle Your program should include the following function: void merge(char *s3, char *s1, char *s2); The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer...

  • Keeping this code written as close to this as possible and only using these headers. How...

    Keeping this code written as close to this as possible and only using these headers. How do I turn this in to a code that can read from 3 files of passwords then sort them into two files. One for valid passwords and the other for invalid passwords and then send a message to the user that says how many are valid and how many are invalid. Only using std::ifstream fin; fin.open("data.txt"); fin.close(); std::ofstream fout; fout.open("data.txt"); fout.close(); #include <iostream> #include...

  • Digit to WordsWrite a C program that asks the user for a two-digit number, then prints...

    Digit to WordsWrite a C program that asks the user for a two-digit number, then prints the English wordfor the number. Your program should loop until the user wants to quit.Example:Enter a two-digit number: 45You entered the number forty-five.Hint: Break the number into two digits. Use one switch statement to print the word for the firstdigit (“twenty,” “thirty,” and so forth). Use a second switch statement to print the word for thesecond digit. Don’t forget that the numbers between 11...

  • Need help with this C program? I cannot get it to compile. I have to use...

    Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...

  • build a phone number from digits entered by your user. Your program will loop until 10...

    build a phone number from digits entered by your user. Your program will loop until 10 valid digits have been entered. It will then use those digits to display the phone number entered using the format: XXXXXXXXXX (or (XXX) XXX – XXXX for extra credit). The program will ask for a digit, it will check to see if the digit is actually between 0 and 9 inclusively. If so, the digit will be stored as the next number in the...

  • Need help with implementing a While, Do-while and for loops in a program. UML: - scnr...

    Need help with implementing a While, Do-while and for loops in a program. UML: - scnr : Scanner - rows : int - MAX_ASCII : int ------------------------------------------------------------------------------- + CharacterTables() : + CharacterTables(rows : int) : - getStartingValue() : int -displayTable(startValue : int) : void - userContinue() : boolean + main(args : String[]) : void In Section 5.10 we saw that there is a close relationship between char and int. Variables of type char store numbers. (Actually, variables of all types...

  • (1) Prompt the user to enter a string of their choosing. Store the text in a...

    (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue! You entered: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT