I am writing a program that takes letters and converts them to telephone numbers. I am just starting out in c++ and not really familar with classes or members which most of the examples I’ve seen on the web involve these concepts. I have the switch case portion working but cannot get the end of file part right. I have copied my code below:
//Explanation of Program
//This program converts inputted characters into numbers based on the telephone digit scheme
//**************************************Program begins here******************************************************************
#include <iostream> //Includes library file for input / output functionality
using namespace std;
int main()
{
//Declaration of variables
char cCharacter = 0; // User inputed character
char cNumber = 0; // Converted numbers from inputed characters
//Beginning of main program
cout << "This program converts inputted characters into numbers based on the telephone digit scheme"; //Explanation of program
while ( cCharacter != EOF)
{
cout << endl <<"Please hit '<ctrl> z' to exit, or enter a word on character at a time: ";
cin >> cCharacter;
cCharacter = toupper (cCharacter);
if (cCharacter == EOF)
{
break;
}
switch (cCharacter)
{
case 'A':
case 'B':
case 'C':
cNumber = '2';
break;
case 'D':
case 'E':
case 'F':
cNumber = '3';
break;
case 'G':
case 'H':
case 'I':
cNumber = '4';
break;
case 'J':
case 'K':
case 'L':
cNumber = '5';
break;
case 'M':
case 'N':
case 'O':
cNumber = '6';
break;
case 'P':
case 'Q':
case 'R':
case 'S':
cNumber = '7';
break;
case 'T':
case 'U':
case 'V':
cNumber = '8';
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
cNumber = '9';
break;
}
cout << "The number associated with the letter "<< cCharacter <<" is: "<<cNumber <<endl;
}
return 0;
}
Also, I would like to know if there is a way (maybe using arrays) to store each number and then print each in sequence. I don’t think there is beacuse my array would have to be variable length, though.
I would change a number of things, but as a intro CS program, it’s not too bad. A few things:
Whenever EOF occurs, your cout is going to try to display that EOF and will probably not work correctly. You should at least put an if check on the cout after the while loop checking against cCharacter != EOF to prevent that.
You have no default case. If an input occurs that you don’t expect, you cout will display its intialized 0 or the previous entry for the number.
To store each number and print them in sequnce, the easiest way would be to use a std::vector. These won’t be taught for a while though, so don’t worry about that too much. You could do it unsafely with a large array, but that isn’t a good method as you surmised.
You could also use an array table to replace your switch statement, but again that’s probably a but further along than your class.
I’m sure there’s a getChar method for cin. This would grab each character as it’s typed and would be much cleaner to use than a straight cin.
I hope this helps you a bit. Without further info from you on how the program is failing, I can only take educated guesses at what is wrong besides the above list.