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!

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
#include<iostream>
#include<string>

using namespace std;

int main ()
{
	char letter;
	
	cout<<"This program converts 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')|| (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 letter to find its corresponding telephone digit." <<endl;
		cout<<"To stop the pgoram enter #." <<endl;

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

return 0;
}

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.

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
#include<iostream>
#include<string>

using namespace std;

int Letter2Number(char);

int main ()
{
	char PhoneNumber[11];
	
	cout<<"This program converts letters to their corresponding telephone digits" <<endl;

	//cout<<"To stop the program enter #." <<endl;

	cout<<"Enter a phone number: ";
	cin.getline( PhoneNumber, 11, '\n' );

	cout<<"\nPhone Number before convert: ";
	cout << PhoneNumber;

	for(int i = 0; i < 11; i++)
		{
		PhoneNumber[i] = Letter2Number(PhoneNumber[i]);
		}

	cout<<"\nPhone Number after convert: ";
	cout << PhoneNumber << endl;

return 0;
}

int Letter2Number(char letter)
	{
	switch (letter)
		{
			case 'A':
			case 'B':
			case 'C':
			case 'a':
			case 'b':
			case 'c':
				return '2';
				break;
			case 'D':
			case 'E':
			case 'F':
			case 'd':
			case 'e':
			case 'f':
				return '3';
				break;
			case 'G':
			case 'H':
			case 'I':
			case 'g':
			case 'h':
			case 'i':
				return '4';
				break;
			case 'J':
			case 'K':
			case 'L':
			case 'j':
			case 'k':
			case 'l':
				return '5';
				break;
			case 'M':
			case 'N':
			case 'O':
			case 'm':
			case 'n':
			case 'o':
				return '6';
				break;
			case 'P':
			case 'Q':
			case 'R':
			case 'S':
			case 'p':
			case 'q':
			case 'r':
			case 's':
				return '7';
				break;
			case 'T':
			case 'U':
			case 'V':
			case 't':
			case 'u':
			case 'v':
				return '8';
				break;
			case 'W':
			case 'X':
			case 'Y':
			case 'Z':
			case 'w':
			case 'x':
			case 'y':
			case 'z':
				return '9';
				break;
			default:
				return letter; // in case it is not a letter or is allready a number
				break;
		}

	}
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!

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
116
117
118
#include<iostream>
#include<string>

using namespace std;

int Letter2Number(char);

int main ()
{
	char PhoneNumber[11];
	while (PhoneNumber[11] != '#')
	{
	cout<<"\nThis program converts letters to their corresponding telephone digits" <<endl;

	//cout<<"To stop the program enter #." <<endl;

	cout<<"Enter a phone number: ";
	cin.getline( PhoneNumber, 11, '\n' );

	cout<<"\nPhone Number before convert: ";
	cout << PhoneNumber;

	for(int i = 0; i < 11; i++)
		{
		PhoneNumber[i] = Letter2Number(PhoneNumber[i]);
		}

	cout<<"\nPhone Number after convert: ";
	cout << PhoneNumber << endl;

	if(PhoneNumber[3])
		cout<<"-";
	}
system("Pause");
return 0;
}

int Letter2Number(char letter)
	{
		
	switch (letter)
		{
			case 'A':
			case 'B':
			case 'C':
			case 'a':
			case 'b':
			case 'c':
				return '2';
				break;
			case 'D':
			case 'E':
			case 'F':
			case 'd':
			case 'e':
			case 'f':
				return '3';
				break;
			case 'G':
			case 'H':
			case 'I':
			case 'g':
			case 'h':
			case 'i':
				return '4';
				break;
			case 'J':
			case 'K':
			case 'L':
			case 'j':
			case 'k':
			case 'l':
				return '5';
				break;
			case 'M':
			case 'N':
			case 'O':
			case 'm':
			case 'n':
			case 'o':
				return '6';
				break;
			case 'P':
			case 'Q':
			case 'R':
			case 'S':
			case 'p':
			case 'q':
			case 'r':
			case 's':
				return '7';
				break;
			case 'T':
			case 'U':
			case 'V':
			case 't':
			case 'u':
			case 'v':
				return '8';
				break;
			case 'W':
			case 'X':
			case 'Y':
			case 'Z':
			case 'w':
			case 'x':
			case 'y':
			case 'z':
				return '9';
				break;
			default:
				return letter; // in case it is not a letter or is already a number
				break;
				
				
		}

	}
How about just displaying it with the hyphens.
1
2
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.