Good Morning,
This is my second post, wahoo (check out my intro here:
forum link)
I had the following homework problem recently, and this is how I solved it. Just looking for things I could have done better, since it works... I am not looking for anyone to do my homework, in fact, its already turned in...
Function: Write a C++ function that receives a single letter and prints out the corresponding digit on the telephone.
NOTE: Your program should accept only uppercase letters (error message for special characters or lowercase letters)
Program: Write a program that consists of a main function and the function described above. Read in letters in the main function and call the function to print a corresponding message. It should repeat to do so until a symbol “$” is read.Well, it took my some time to figure out how to go about doing this. At first I was going to convert the input to an int, divide by 3, round up using ceil(#), and then each key would have a corresponding number. That of course did not work and I was a bit lost. Then I turned here,
to this topic, and got some ideas. I used an if-else, with the switch inside the if.
Here is my code:
CODE
#include <iostream>
#include <cmath>
using namespace std;
void letterprocessor(char); // function prototype
int main() // main is the value returning function
{
char user_entry;
cout << "This program will convert uppercase letters to their\n";
cout << "corresponding numeral on a standard telephone keypad.\n";
cout << endl;
cout << "The program will continue until you type the '$' character.\n";
cout << endl;
cout << "Enter a letter or a group of letters to be converted to\n";
cout << "a number on a telephone keypad.\n" << endl;
while (user_entry != 36) // 36 is ASCII for $, thus the loop will continue until $ is typed
{
cin >> user_entry;
letterprocessor(user_entry);
}
return 0;
}
void letterprocessor(char user_entry) // does the work
{
if (user_entry > 64 && user_entry < 91 || user_entry == 36) // checks if the letter is between A and Z, of if it is $
{
switch (user_entry) // switch checks different scenarios
{
case 'A':
case 'B':
case 'C':
cout << user_entry << " corresponds with 2" << endl;
break;
case 'D':
case 'E':
case 'F':
cout << user_entry << " corresponds with 3" << endl;
break;
case 'G':
case 'H':
case 'I':
cout << user_entry << " corresponds with 4" << endl;
break;
case 'J':
case 'K':
case 'L':
cout << user_entry << " corresponds with 5" << endl;
break;
case 'M':
case 'N':
case 'O':
cout << user_entry << " corresponds with 6" << endl;
break;
case 'P':
case 'R':
case 'S':
cout << user_entry << " corresponds with 7" << endl;
break;
case 'T':
case 'U':
case 'V':
cout << user_entry << " corresponds with 8" << endl;
break;
case 'W':
case 'X':
case 'Y':
cout << user_entry << " corresponds with 9" << endl;
break;
case 'Q':
cout << "The letter Q is not on a keypad." << endl;
break;
case 'Z':
cout << "The letter Z is not on a keypad." << endl;
break;
case '$':
cout << "The program will now exit." << endl;
exit(0);
}
}
else
cout << "You have entered an invalid or lowercase character"; // if the input wasnt in the boolean parameters, then you get this error
}
The result looks like:
CODE
This program will convert uppercase letters to their
corresponding numeral on a standard telephone keypad.
The program will continue until you type the '$' character.
Enter a letter or a group of letters to be converted to
a number on a telephone keypad.
J
J corresponds with 5
$
The program will now exit.
Note that I'm programming under Xcode on the mac.
Any suggestions for what I could have done better, optimizations, etc?
Thanks in advance,
Elliott