Frustrated with problem.

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!

-Albatross
Last edited on
How can I set the digit if it changes depending on the letter typed? Should I maybe do a digitABC = 2, digitDEF = 3, etc?
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.

-Albatross
Last edited on
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.
Make digit an int.
Oh, right. One more thing. It's int main, not void main.

Also, you should be using an int for digit, not a char.
EDIT: Late on this one.

Silly me for missing that.

-Albatross

Last edited on
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?

And thank you both for the help so far.
Kness wrote:
if I enter an incorrect digit, the program errors

Isn't that what you want? You want to enter letters, not digits.

EDIT: It works ok for me.
Last edited on
You should get no run-time errors from entering an incorrect character (be it a digit, letter, or any other ASCII character).

Indeed, after all the modifications listed above, this program works fine for me.

-Albatross
Last edited on
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.
Post your code again.

EDIT: Oh, yeah, and what Albatross says \/
Last edited on
Then something's definitely not right, assuming you're using the code that we see above with its proper modifications.

EDIT: Oh, and you can enter Z and it won't correspond to a digit on the keypad in your program. Just sayin'.

-Albatross
Last edited on
I know. I missed something on the assignment page that he listed for me. I'm not allowed to use Q or Z, and W, X, Y, were left out of my program.

So when you compile and run this code, the "invalid" message appears for you? Ugh. I don't know what's wrong with my program.
Repost your code, plz? Then we'll determine if you need a new compiler or not. What are you using, by the way?

-Albatross
I'm using Visio 2010

#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

-Dr. Albatross
If you use a console project for this, try creating an empty project.
Else... I don't know... I guess what Dr. Albatross said.
Last edited on
It's Visual Studio. Sorry about that. I'm getting my programs crossed.

I will talk about it to my instructor tomorrow. Thank you for your help.
closed account (3hM2Nwbp)
Unless your phone's keypad has uppercase and lowercase letters, you can use the toupper / tolower methods to chop off a few of those conditionals.

1
2
3
cin >> letter;
letter = toupper(letter);
if(letter == 'A' || letter == 'B' || letter == 'C')
Topic archived. No new replies allowed.