Converting Letters to numbers (Phone Key Pad)

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) :

 ``123456789101112131415161718192021222324`` ``````#include 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
what exactly are you having trouble with? are stuck or just getting compiler errors?
In fact you already wrote the program except that instead of switch( digit ) you shall use switch ( letter ). For example

 ``1234567891011121314151617181920212223242526272829303132`` ``````#include #include 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
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?
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
Topic archived. No new replies allowed.