My instructor gave us an assignment, and he even listed the beginning example we are supposed to use with the program. The program is supposed to allow users to enter a letter and get the corresponding digit on the telephone pad. However, the program runs, but it doesn't run correctly. It's supposed to show the letter entered and the corresponding digit, but I can't figure out how to get it to display correctly. No matter what I type I get the "there is no" response which should display if a user enters anything other than a letter. Thank you for any help.
#include <iostream>
#include <string>
using namespace std;
void main()
{
char letter ;
char digit ;
cout << "Enter a single letter, and I will tell you what the corresponding digit is on the telephone keypad." << endl;
cin >> letter;
if(letter == 'A' || letter == 'B' || letter == 'C' || letter == 'a' || letter == 'b' || letter == 'c')
digit = 2;
else
if(letter == 'D' || letter == 'd' || letter == 'E' || letter == 'e' || letter == 'F' || letter == 'f')
digit = 3;
else
if(letter == 'G' || letter == 'g' || letter == 'H' || letter == 'h' || letter == 'I' || letter == 'i')
digit = 4;
else
if(letter == 'J' || letter == 'j' || letter == 'K' || letter == 'k' || letter == 'L' || letter == 'l')
digit = 5;
else
if(letter == 'M' || letter == 'm' || letter == 'N' || letter == 'n' || letter == 'O' || letter == 'o')
digit = 6;
else
if(letter == 'P' || letter == 'p' || letter == 'Q' || letter == 'q' || letter == 'R' || letter == 'r' ||
letter == 'S' || letter == 's')
digit = 7;
else
if(letter == 'T' || letter == 't' || letter == 'U' || letter == 'u' || letter == 'V' || letter == 'v')
digit = 8;
if(digit == '2' || digit == '3' || digit == '4' || digit == '5' || digit == '6' || digit == '7' ||
digit == '8')
cout << "The digit " << digit << "corresponds to the letter " << letter << "on the keypad." << endl;
else cout << "There is no digit on the telephone keypad that corresponds to " << letter << "." << endl;
}
Try setting digit to something before getting letter. This might help in some instances.
This is not the main problem, however. The main problem is that you have single quotes around the numbers in your final if statement. Get rid of them! Those single quotes radically change the values you're comparing your numbers to!
No, what you have there for changing the digit seems to be correct. What you have for checking the value of the digit is incorrect because of those 'integer's. All of the single-quotes in that line need to be removed. Kbyes.
I removed the quotes, and the program will run, but it still doesn't run correctly. Instead of the digit showing up as output when a proper key is typed, a symbol appears where the digit should appear.
That was the other problem with getting the number to appear, but if I enter an incorrect digit, the program errors. How do I get my else statement to appear, the one that says it was not a valid letter?
That's just it. The program is supposed to tell a user that enters anything other than a letter that it is invalid. Hence the final else statement. But when I enter anything else, it gives me a runtime error that the character entered is undefined.
#include <iostream>
#include <string>
using namespace std;
int main()
{
char letter ;
int digit ;
cout << "Enter a single letter, and I will tell you what the corresponding digit is on the telephone keypad." << endl;
cin >> letter;
if(letter == 'A' || letter == 'B' || letter == 'C' || letter == 'a' || letter == 'b' || letter == 'c')
digit = 2;
else
if(letter == 'D' || letter == 'd' || letter == 'E' || letter == 'e' || letter == 'F' || letter == 'f')
digit = 3;
else
if(letter == 'G' || letter == 'g' || letter == 'H' || letter == 'h' || letter == 'I' || letter == 'i')
digit = 4;
else
if(letter == 'J' || letter == 'j' || letter == 'K' || letter == 'k' || letter == 'L' || letter == 'l')
digit = 5;
else
if(letter == 'M' || letter == 'm' || letter == 'N' || letter == 'n' || letter == 'O' || letter == 'o')
digit = 6;
else
if(letter == 'P' || letter == 'p' || letter == 'R' || letter == 'r' || letter == 'S' || letter == 's')
digit = 7;
else
if(letter == 'T' || letter == 't' || letter == 'U' || letter == 'u' || letter == 'V' || letter == 'v')
digit = 8;
else
if(letter == 'W' || letter == 'w' || letter == 'X' || letter == 'x' || letter == 'Y' || letter == 'y')
digit = 9;
if(digit == 2 || digit == 3 || digit == 4 || digit == 5 || digit == 6 || digit == 7 ||
digit == 8 || digit == 9)
cout << "The digit " << digit << " corresponds to the letter " << letter << " on the keypad." << endl;
else cout << "There is no digit on the telephone keypad that corresponds to " << letter << "." << endl;
}
If by Visio you meant Visual C++, then you should probably reinstall your compiler. This looks fine to me (aside from no return 0 but that's not mandatory).
If by Visio you meant Visio, then how did you compile a C++ program with Visio?!? Take Visual C++ and call me in the morning. :P