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.
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>
using namespace std;
int main()
{
char letter;
int counter = 0;
cout << "Program to convert letters to their corresponding telephone digits" << endl;
while (cin.get(letter) && counter < 7 ) {
if (letter != ' ' && letter >= 'A' && letter <= 'z') {
counter++;
if (letter > 'Z') {
letter = (int)letter - 32;
}
if (counter == 4) {
cout << "-";
using namespace std;
int main()
{
char letter;
int counter = 0;
cout << "Program to convert letters to their corresponding telephone digits" << endl;
while (cin.get(letter) && counter < 7 ) {
if (letter != ' ' && letter >= 'A' && letter <= 'z') {
counter++;
if (letter > 'Z') {
letter = (int)letter-32;
}
if (counter == 4) {
cout << "-";
}
switch (letter) {
case 'A':
case 'B':
case 'C':
cout << "2";
break;
case 'D':
case 'E':
case 'F':
cout << "3";
break;
case 'G':
case 'H':
case 'I':
cout << "4";
break;
case 'J':
case 'K':
case 'L':
cout << "5";
break;
case 'M':
case 'N':
case 'O':
cout << "6";
break;
case 'P':
case 'Q':
case 'R':
case 'S':
cout << "7";
break;
case 'T':
case 'U':
case 'V':
cout << "8";
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
cout << "9";
break;
default:
break;
}
}
}
return 0;
Print the hyphen when required
}
switch (letter) {
case 'A':
case 'B':
case 'C':
cout << "2";
break;
case 'D':
case 'E':
case 'F':
cout << "3";
break;
case 'G':
case 'H':
case 'I':
cout << "4";
break;
case 'J':
case 'K':
case 'L':
cout << "5";
break;
case 'M':
case 'N':
case 'O':
cout << "6";
break;
case 'P':
case 'Q':
case 'R':
case 'S':
cout << "7";
break;
case 'T':
case 'U':
case 'V':
cout << "8";
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
cout << "9";
break;
default:
break;
}
}
}
return 0;
Comments
Leave a comment