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.
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.
****************************************/
#include <iostream>
#include <string>
using namespace std;
int main()
{
string phrase, number;
char letter;
int i,j;
// Get input
cout << "Program to convert a phrase into a telephone number" << endl;
cout << "To stop the program enter #." << endl;
cout << "Enter a phrase that contains only letters and is at least seven characters long: ";
cin >> phrase;
cout << endl;
// Processing
for ( i=0; i<7; i++)
{
cin.get(letter);
letter = cin.get();
while (letter != '#')
{
if (letter >= 'A' && letter <= 'Z')
switch (letter)
{
case 'A':
case 'B':
case 'C':
j = 2;
break;
case 'D':
case 'E':
case 'F':
j = 3;
break;
case 'G':
case 'H':
case 'I':
j = 4;
break;
case 'J':
case 'K':
case 'L':
j = 5;
break;
case 'M':
case 'N':
case 'O':
j = 6;
break;
case 'P':
case 'Q':
case 'R':
case 'S':
j = 7;
break;
case 'T':
case 'U':
case 'V':
j = 7;
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
j = 7;
}
else (letter >= 'a' && letter <= 'z');
switch (letter)
{
case 'a':
case 'b':
case 'c':
j = 2;
break;
case 'd':
case 'e':
case 'f':
j = 3;
break;
case 'g':
case 'h':
case 'i':
j = 4;
break;
case 'j':
case 'k':
case 'l':
j = 5;
break;
case 'm':
case 'n':
case 'o':
j = 6;
break;
case 'p':
case 'q':
case 'r':
case 's':
j = 7;
break;
case 't':
case 'u':
case 'v':
j = 8;
break;
case 'w':
case 'x':
case 'y':
case 'z':
j = 9;
}
}
number[i] = j;
}
cout << number;
The first thing you need to do is taken in an array of chars. Something like:
1 2
char letters[ 8 ];
cin >> letters;
Then once you have gotten the letters, you'd loop through the array of chars using the switch statement you have already coded to convert letters to numbers.