Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

To make telephone numbers easier to remember, some companies use letters to show their telephone number. For example, using letters, the telephone number 4 3

To make telephone numbers easier to remember, some companies use letters to show their telephone number. For example, using letters, the telephone number 438-5626 can be shown as GET LOAN.
In some cases, to make a telephone number meaningful, companies might use more than seven letters. For example, 225-5466 can be displayed as CALL HOME, which uses eight letters.
Instructions
Write a program that prompts the user to enter a telephone number expressed in letters and outputs the corresponding telephone number in digits.
If the user enters more than seven letters, then process only the first seven letters.
Also output the -(hyphen) after the third digit.
Allow the user to use both uppercase and lowercase letters as well as spaces between words.
Moreover, your program should process as many telephone numbers as the user wants.
Use the dialpad below for reference: phoneDialpad.jpg
The program should accept input and produce output similar to the example program execution below.
Enter Y/y to convert a telephone number from
letters to digits.
Enter any other letter to terminate the program.
Y
Enter a telephone number using letters: Hello world
The corresponding telephone number is:
435-5696
To process another telephone number, enter Y/y
Enter any other letter to terminate the program.
z
Task #01: Correct output for single entry
Task #02: Correct output for multiple numbers
I get past the first task but the second task keeps failing on me and I am stumped as to why. here is my current code if it helps you. please help me figure this out!
#include
#include
#include
#include
using namespace std;
// Function to convert a character to its corresponding phone digit
char letterToDigit(char letter){
letter = toupper(letter); // Convert to uppercase for uniformity
if (letter >='A' && letter <='C') return '2';
if (letter >='D' && letter <='F') return '3';
if (letter >='G' && letter <='I') return '4';
if (letter >='J' && letter <='L') return '5';
if (letter >='M' && letter <='O') return '6';
if (letter >='P' && letter <='S') return '7';
if (letter >='T' && letter <='V') return '8';
if (letter >='W' && letter <='Z') return '9';
return ''; // Default case for non-letter characters (e.g., spaces)
}
int main(){
char continueConversion;
do {
cout << "Enter Y/y to convert a telephone number from letters to digits.
";
cout << "Enter any other letter to terminate the program.
";
cin >> continueConversion;
cin.ignore(); // Clear the newline character from the input buffer
if (continueConversion =='Y'|| continueConversion =='y'){
string input, numericPhoneNumber;
cout << "Enter a telephone number using letters: ";
getline(cin, input);
int count =0;
for (char c : input){
if (isalpha(c) && count <7){
numericPhoneNumber += letterToDigit(c);
count++;
}
}
// Insert hyphen after the third digit if length is at least 4
if (numericPhoneNumber.length()>3){
numericPhoneNumber.insert(3,"-");
}
cout <<"
The corresponding telephone number is:
";
cout << numericPhoneNumber << endl;
}
cout <<"To process another telephone number, enter Y/y
";
cout << "Enter any other letter to terminate the program.
";
cin >> continueConversion;
cin.ignore(); // Clear the newline character from the input buffer
} while (continueConversion =='Y'|| continueConversion =='y');
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions