In: Computer Science
C++
Summary
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:
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
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;
int main()
{
int done = 0;
int flag;
int count = 1;
char word, letter;
int Phone_Num;
while (done != 1)
{
flag = 0;
count = 1;
cout << "Enter a telephone number using letters:";
while (flag == 0)
{
cin >> word;
// converting word to uppercase letters
word = toupper(word);
// swtich case to get the number for equalant char
switch (word)
{
case 'A':
case 'B':
case 'C':
Phone_Num = 2;
break;
case 'D':
case 'E':
case 'F':
Phone_Num = 3;
break;
case 'G':
case 'H':
case 'I':
Phone_Num = 4;
break;
case 'J':
case 'K':
case 'L':
Phone_Num = 5;
break;
case 'M':
case 'N':
case 'O':
Phone_Num = 6;
break;
case 'P':
case 'Q':
case 'R':
case 'S':
Phone_Num = 7;
break;
case 'T':
case 'U':
case 'V':
Phone_Num = 8;
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
Phone_Num = 9;
break;
}
// if count reaches 4 than print -
if(count == 4)
cout << "-" << Phone_Num;
else
cout << Phone_Num;
if(count >= 7)
{
cout << endl;
while (cin.get() != '\n' );
flag = 1;
}
count++;
}
cout << "Do you want to make another number? enter Y or N " << endl;
cin >> letter;
if (letter == 'n' || letter == 'N')
done = 1;
}
return 0;
}