189k views
4 votes
C++: The program Telephone Digits outputs only telephone digits that correspond to uppercase letters. Rewrite the program so that it processes both uppercase and lowercase letters and outputs the corresponding telephone digit. If the input is something other than an uppercase or lowercase letter, the program must output an appropriate error message.

1 Answer

5 votes

Answer:

Following are the program written in the C++ Programming Language:

#include <iostream> //header file

using namespace std; //using namespace

int main() //main function

{

char letters;

cout << "To stop the program enter 0" << endl;

cout << "Enter letter: " ;

cin >> letters;

//set while loop

while (letters != '0' && letters >= 'A' && letters <= 'z') {

cout << "The letter you entered: " << letters << endl;

cout << "corresponding telephone digits is: ";

if (letters > 'Z') { //if statement

letters = (int)letters-32; // letters covertion

}

switch (letters) { //switch statement

case'A':

case'B':

case'C':

cout << "2" << "\\""\\"; //print message

break; //break the statement

case'D':

case'E':

case'F':

cout << "3" << "\\""\\"; //print message

break; //break the statement

case'G':

case'H':

case'I':

cout << "4" << "\\""\\"; //print message

break; //break the statement

case'J':

case'K':

case'L':

cout << "5" <<"\\""\\"; //print message

break; //break the statement

case'M':

case'N':

case'O':

cout << "6" <<"\\""\\"; //print message

break; //break the statement

case'P':

case'Q':

case'R':

case'S':

cout << "7" << "\\""\\"; //print message

break; //break the statement

case'T':

case'U':

case'V':

cout << "8" << "\\""\\"; //print message

break; //break the statement

case'W':

case'X':

case'Y':

case'Z':

cout << "9" << "\\""\\"; //print message

break; //break the statement

default:

break; //break the statement

}

//print and return letters

cout << "Enter another letter to find out the number: ";

cin >> letters;

}

return 0;

}

Output:

To stop the program enter 0

Enter letter: a

The letter you entered: a

corresponding telephone digits is: 2

Enter another letter to find out the number: S

The letter you entered: S

corresponding telephone digits is: 7

Enter another letter to find out the number: 0

Step-by-step explanation:

Here, we define header file "<iostream>" and namespace "std" then, we define main() function.

  • Then, inside the main function we set character data type variable "letters" and then print message and get input from the user in the variable "letters".
  • Then, we set the while loop and pass the condition then, we set the if statement and print two message.
  • Then, we set the switch statement inside the while loop and pass the letter variable in it's parameter then, we set the following cases of the switch statement.
  • Finally, we print the message and print the value of the variable "letters" then, return 0 and close the main() function.
User Chris Lombardi
by
5.1k points