Converting Letters to numbers (Phone Key Pad)

Victor714 (22)
Im doing this homework assignment in which i need to convert letters to a number.(like on the telephone key pads, ABC = 2, DEF = 3, etc.

This is my code for ABC(i know it is incorrect,but i need help I dont know what else to do) :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
 
using namespace std;
 
int main()
 
{
       
        char letter;
        int digit;
       
 
    cout <<"Please enter a single letter in upper case,and I will tell you what the corresponding digit is on the telephone:";
   
        cin  >> letter;
        switch (digit)
        {
       
        case 'A':
        case 'B':
        case 'C':
        digit= 2;
       
        }



Last edited on
Need4Sleep (532)
what exactly are you having trouble with? are stuck or just getting compiler errors?
vlad from moscow (3112)
In fact you already wrote the program except that instead of switch( digit ) you shall use switch ( letter ). For example

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
#include<iostream>
#include <cctype>
 
using namespace std;
 
int main()
{
       
        char letter;
        int digit;
       
 
        cout <<"Please enter a single letter, and I will tell you what the corresponding digit is on the telephone:";
   
        cin  >> letter;
        letter = toupper( letter );

        switch ( letter )
        {
       
        case 'A': case 'B': case 'C':
        digit= 2;
        cout << digit << " corresponds to " << letter << endl;
        break;

        default:
        cout << letter << " is unknown\n";
        break;       
        }

        return ( 0 );
}
Last edited on
Victor714 (22)
As you may see im new (not that new to not know what cout and cin does but still new)to c++, what does "toupper" do or has to do in there?
vlad from moscow (3112)
toupper is a standard C function that converts a letter to upper case. This will allow a user to input either a low case letter or upper case letter. That is your program will be more smart and will convert a letter to upper case itself.
Last edited on
Registered users can post here. Sign in or register to post.