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?
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 != '#');
#include <iostream>
usingnamespace 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