I have a programming problem, I am running a program that corresponds with the digits on a telephone keypad. When I enter a valid char, it should give the result and loop until an invalid char is entered. I cannot get it to loop to work properly.
#include <iostream>
usingnamespace std;
int main()
{
int digit;
char letter, end;
do{
cout << "Enter a single letter, and I will tell you what the corresponding digit is on the telephone keypad." << endl;
cin >> letter;
switch(letter)
{
case'A':
case'a':
case'B':
case'b':
case'C':
case'c':
digit = 2;
cout << "The digit " << digit << " corresponds to the letter " << letter << " on the keypad." << endl;
break;
case'D':
case'd':
case'E':
case'e':
case'F':
case'f':
digit = 3;
cout << "The digit " << digit << " corresponds to the letter " << letter << " on the keypad." << endl;
break;
case'G':
case'g':
case'H':
case'h':
case'I':
case'i':
digit = 4;
cout << "The digit " << digit << " corresponds to the letter " << letter << " on the keypad." << endl;
break;
case'J':
case'j':
case'K':
case'k':
case'L':
case'l':
digit = 5;
cout << "The digit " << digit << " corresponds to the letter " << letter << " on the keypad." << endl;
break;
case'M':
case'm':
case'N':
case'n':
case'O':
case'o':
digit = 6;
cout << "The digit " << digit << " corresponds to the letter " << letter << " on the keypad." << endl;
break;
case'P':
case'p':
case'R':
case'r':
case'S':
case's':
digit = 7;
cout << "The digit " << digit << " corresponds to the letter " << letter << " on the keypad." << endl;
break;
case'T':
case't':
case'U':
case'u':
case'V':
case'v':
digit = 8;
cout << "The digit " << digit << " corresponds to the letter " << letter << " on the keypad." << endl;
break;
case'W':
case'w':
case'X':
case'x':
case'Y':
case'y':
digit = 9;
cout << "The digit " << digit << " corresponds to the letter " << letter << " on the keypad." << endl;
break;
default:
cout << "There is no digit on the telephone keypad that corresponds to " << letter << "." << endl;
break;
}
cin >> end;
}while( (end != 'Z') && (end != 'z') );
}
I don't get what you are asking, the code works the way you have it. After you put in a char it outputs the corresponding line then breaks to cin >> end; (line 91) and if you don't put in a 'z' or a 'Z' then it ends.
What did you want it to do or where is your issue with it?
When I input the letter A, It gives the results of
The digit 2 corresponds to the letter A on the keypad
. After that, it should prompt the question
Enter a single letter, and I will tell you what the corresponding digit is on the telephone keypad
. Instead, it doesn't show anything, unless I input a character and hit enter, then it will prompt the question. I don't think it works correctly because I shouldn't have to enter another character to the the question.
You're telling the program to ask for an input that it stores into variable "end"
before it loops back around.
Plus the statement (end != 'Z') && (end != 'z') will always be true seeing as end cannot be Z and z at the same time.
If you want the program to end when the user enters Z or z then you should remove line 91 cin >> end; and edit line 92 to the following: }while( (letter != 'Z') || (letter != 'z') );
Also remove the declaration of "end" because it's pointless now.