### Telephone letters into digits

Hi there,

I have the following code that displays up to a char level. I need to write a program where the user will enter letters as a phone number and it will convert the letters into digits. FYI - I have tried few times with the string, but no luck. Please guide me as to how I should proceed with the strings, and get character by character. Thanks in advance!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111`` ``````#include #include using namespace std; int main () { char letter; cout<<"This program converts letters to their corresponding telephone digits" <>letter; cout<= 'A' && letter <= 'Z')|| (letter >= 'a' && letter <= 'z')) switch (letter) { case 'A': case 'B': case 'C': case 'a': case 'b': case 'c': cout<< 2 <>letter; cout<
`char letter;`

Only allows you one char. You really need to do an array of char and then do a loop to go through each spot in the array to change each letter one at a time. If you turn your switch into a function which takes in one char and returns one number then you can pass each char to the function one at a time.

 `` `` ``char word[11]; // Max size of phone number is 10 the 11th char will be '\0' which indicates the end of the array ``

Thanks for the response Ryan!
I have been trying since then on what you said.Currently I am completely lost. Can you please provide me with an example of when you say - " You really need to do an array of char and then do a loop to go through each spot in the array to change each letter one at a time. If you turn your switch into a function which takes in one char and returns one number then you can pass each char to the function one at a time."

FYI - when i build the program now, i get the following error message.

'char [11]' differs in levels of indirection from 'int'
I took out the loop and exit option but you shouldn't have to hard of a time working it back in.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110`` ``````#include #include using namespace std; int Letter2Number(char); int main () { char PhoneNumber[11]; cout<<"This program converts letters to their corresponding telephone digits" <
Last edited on
Ryan, I sincerely appreciate your efforts on this. I will take this code to the next level, and will update you. you ROCK!!!
Well, the loop seems to be working. I am trying to put hyphen between numbers, but am not able to do so. I did try using if statement, as you can see in the code below. the hyphen shows at the beginning of the next loop, but not after the third letter in the phone number. For example, 222-2222 Can you please let me know where it suppose to go. Thanks!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118`` ``````#include #include using namespace std; int Letter2Number(char); int main () { char PhoneNumber[11]; while (PhoneNumber[11] != '#') { cout<<"\nThis program converts letters to their corresponding telephone digits" <
How about just displaying it with the hyphens.
 ``12`` ``````cout << PhoneNumber[0] << PhoneNumber[1] << PhoneNumber[2] << '-' << PhoneNumber[3] << PhoneNumber[4] << PhoneNumber[5] << '-' << PhoneNumber[6] << PhoneNumber[7] << PhoneNumber[8] << PhoneNumber[9] << endl; ``````
Topic archived. No new replies allowed.