Convert letter to phone number using switch statement

I am trying to convert letter to corresponding telephone digits using only switch statement, but I got an infinite loop. It works if using if statement but I can only use switch Would you please help me detect the code?

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
  #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 enter is: " 
			<< letter << endl;
		cout << "The corresponding telephone digit is: ";

		switch (letter)
		{
		case 'A':
		case 'B':
		case 'C':
			cout << 2 << endl;
			break;
		case 'D':
		case 'E':
		case 'F':
			cout << 3 << endl;
			break;
		case 'G':
		case 'H':
		case 'I':
			cout << 4 << endl;
			break;
		case 'J':
		case 'K':
		case 'L':
			cout << 5 << endl;
			break;
		case 'M':
		case 'N':
		case 'O':
			cout << 6 << endl;
			break;
		case 'P':
		case 'Q':
		case 'R':
		case 'S':
			cout << 7 << endl;
			break;
		case 'T':
		case 'U':
		case 'V':
			cout << 8 << endl;
			break;
		case 'W':
		case 'X':
		case 'Y':
		case 'Z':
			cout << 9 << endl;
			break;
		default:
			{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;}
		}
	}

	return 0;

}
Use a do-while loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cout << "Program to convert uppercase "
     << "letters to their corresponding "
     << "telephone digits." << endl;

do {
    cout << "To stop the program enter #.\n"

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

    cout << "The letter you enter is: " 
         << letter << endl
         << "The corresponding telephone digit is: ";

    switch() {...}

} while (letter != '#');
Last edited on
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
#include <iostream>

using namespace std;

int main()
{
	char letter;

	cout << "Program to convert uppercase letters to their corresponding "
		  << "telephone digits.\nTo stop the program enter #.\n" ;

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

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

		switch (letter)
		{
		    case 'A':
		    case 'B':
		    case 'C':
			    cout << 2 << endl;
			    break;

                    // case ...

		    default:
                            cout << "Invalid input." << endl;

                } // end switch
        
                // this part should be outside the switch, but within the while loop
                cout << "\nEnter another uppercase letter to find its "
                     << "corresponding telephone digit.\nTo stop the program enter #.\n" ;
                cin >> letter;

        } // end while

} // end main() 

Thank you for your help, I just got it. I suppose to put the curly bracket after default:, not after cout << endl. I was trying to use a do-while, thanks anyway
Topic archived. No new replies allowed.