Question
Would you please write an answer to this program using c++/ Branching/ Switch statement . I have already posted this question but the answer had
Would you please write an answer to this program using c++/ Branching/ Switch statement . I have already posted this question but the answer had errors. I will post that answer too.
Topics
Branching
switch statement
Description
Write a program that translates a letter used in a telephone number into an equivalent numeric digit. The program asks the user to enter a letter, translates the letter into the corresponding digit and displays it. The user may enter its selection either as an upper or lower case letter.
The program translates the letter into a digit according to the table below:
Letters Digit
a b c 2
d e f 3
g h i 4
j k l 5
m n o 6
p q r s 7
t u v 8
w x y z 9
Testing
Input Test 1
Enter a letter: p
Output Test 1
Letter: p
Digit: 7
Input Test 2
Enter a letter: P
Output Test 1
Letter: P
Digit: 7
Sample Code 1
/*
The code below determines the character entered by user.
*/
/*
C++ code:
*/
char letter;
cout << Enter a single alphabet: << endl;
cin >> letter;
/*
Java code:
*/
/*
The code snippet below does the following:
It asks the user to enter a single alphabetical character
It saves the entered character in a string named in.
It extracts the first character from the String in.
It saves the extracted character in a char variable named letter.
*/
String in;
char letter;
int digit;
in = JOptionPane.showInputDialog(Enter a single alphabet);
letter = in.charAt(0);
Sample Code 2
/*
The code snippet below attempts to determine the digit corresponding to the letter.
Only a partially written Switch statement is shown. Add the additional cases.
*/
switch (letter)
{
case a:
case b:
case c:
case A:
case B:
case C:
digit = 2;
break;
case d:
case e:
case f:
case D:
case E:
case F:
digit = 3;
break;
}
previous Answer:
#include
using namespace std;
int main() {
char letter;
cout << "Enter a single alphabet:" << endl;
cin >> letter; switch (letter)
{
case 'a':
case 'b':
case 'c':
case 'A':
case B:
case C:
digit = 2;
break;
case d:
case e:
case f:
case D:
case E:
case F:
digit = 3;
break;
case g:
case h:
case i:
case G:
case H:
case I:
digit = 4;
break;
case J:
case K:
case L:
case j:
case k:
case l:
digit = 5;
break;
case m:
case n:
case o:
case M:
case N:
case O:
digit = 6;
break;
case p:
case q:
case r:
case s:
case P:
case Q:
case 'R':
case 'S':
digit = 7;
break;
case t:
case u:
case v:
case T:
case U:
case V:
digit = 8;
break; case W:
case X:
case Y:
case Z:
case w:
case x:
case 'y':
case "z": digit = 9;
break;
} cout<< "digit is:"<
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started