Telephone digits

I am writting a program for telephone digits
which is ok i did it

now I want to change the program to prompt a used to enter a telephone number that expresses in letters and outputs the corresponding telephone number in digits. No more than 7 letters

example GET LONA = 438 - 5626

andi have no idea how to do it
help help


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

#include <iostream>

using namespace std;

int main()
{
    char letter;                                  

    cout << "Program to convert uppercase "
         << "letters to their corresponding "   
         << "telephone digits." << endl;       
  
    cout << "To stop the program enter #." 
         << endl;                                 

    cout << "Enter a letter: ";                   
    cin >> letter;                                
    cout << endl;                                 

    while (letter != '#')                         
    {
        cout << "The letter you entered is: "
             << letter << endl;                   
        cout << "The corresponding telephone "
             << "digit is: ";                     

        if (letter >= 'A' && letter <= 'z')       
            switch (letter)                       
            {
            case 'A': 
            case 'B': 
            case 'C': 
            case 'a': 
            case 'b': 
            case 'c':
                cout << "2" <<endl;               
                break;                            
            case 'D': 
            case 'E': 
            case 'F':
            case 'd': 
            case 'e': 
            case 'f':
                cout << "3" << endl;             
                break;                         
            case 'G': 
            case 'H': 
            case 'I':
            case 'g': 
            case 'h': 
            case 'i':
                cout << "4" << endl;              
                break;                            
            case 'J': 
            case 'K': 
            case 'L':			
            case 'j': 
            case 'k': 
            case 'l': 
                cout << "5" << endl;           
                break;                          
            case 'M': 
            case 'N': 
            case 'O':
            case 'm': 
            case 'n': 
            case 'o':
                cout << "6" << endl;            
                break;                           
            case 'P': 
            case 'Q': 
            case 'R': 
            case 'S': 
            case 'p': 
            case 'q': 
            case 'r': 
            case 's': 
                cout << "7" << endl;              
                break;                            
            case 'T': 
            case 'U': 
            case 'V':
            case 't': 
            case 'u': 
            case 'v':
                cout << "8" << endl;              
                break;                          
            case 'W': 
            case 'X': 
            case 'Y':
            case 'Z': 
            case 'w': 
            case 'x': 
            case 'y': 
            case 'z': 
                cout << "9" << endl;              
            }
        else                                     
            cout << "Invalid input." << endl;   

        cout << "\nEnter another uppercase "
             << "letter to find its " 
             << "corresponding telephone digit."
             << endl;                            
        cout << "To stop the program enter #."
             << endl;                             

        cout << "Enter a letter: ";               
        cin >> letter;                            
        cout << endl;                             
    }//end while

    return 0;
}

Last edited on
Looks like you've done pretty good. (But try entering '[' for the letter. Google "ASCII table".) I'd get rid of line 28 and move 99..100 into the default case of the switch.

So far, your program works fine for single letters. But now you want to read more than one letter.

Doing something more than once suggests a loop.
I also suggest you move all the stuff that changes a letter into a digit to a function.

Keep in mind, the user can enter the following characters as part of a valid telephone number:
0..9 A..Z a..z ( ) -
Digits you don't need to convert (but you should still print them).
Letters you already know how to convert.
And ( ) and - can be simply ignored.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.