I need help with this program

April 21, 2009 at 06:51:29
Specs: Windows XP
Hi could someone help me out with this? I need to replace the statement from line 10 to Line 28 so that it only uses a switch structure to find the digit that corresponds to an uppercase letter. Thanks!!

#include <iostream>

using namespace std;

int main()
{
char letter;
char uppercase;

cout<< "Program to convert uppercase "
<< "letters to their corresponding "
<< "telephone digits." <<endl;

cout<< "To stop the program enter #."
<< endl;

cout<< "Enter a letter: ";
cin>> letter;
cout<< endl;

while (letter != '#')
{
cout << "The letter you entered is not uppercase: "
<< letter<< endl;
cout << "The corresponding telephone "
<< "digit is: ";


if (letter >= 'A' && letter <= 'Z') //line 10
switch (letter)
{
case 'A':
case 'B':
case 'C':
cout << "2" <<endl;
break;
case 'D':
case 'E':
case 'F':
cout << "3" <<endl;
break;
case 'G':
case 'H':
case 'I':
cout << "4" <<endl;
break;
case 'J':
case 'K':
case 'L':
cout << "5" <<endl;
break;
case 'M':
case 'N':
case 'O':
cout << "6" <<endl;
break;
case 'P':
case 'Q':
case 'R':
case 'S':
cout << "7" <<endl;
break;
case 'T':
case 'U':
case 'V':
cout << "8" <<endl;
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
cout << "9" <<endl;
}
else
cout << "Invalid input." <<endl; //Line 28

cout << "\nEnter another uppercase "
<< "letter to find its "
<< "corresponding telephone digit."
<< endl;
cout << "To stop the program enter #."
<< endl;

cout << "Enter a letter: ";
cin >> letter;
cout << endl;
}//end while

return 0;
}



See More: I need help with this program

Report •


#1
April 21, 2009 at 20:20:30
You could use toupper / tolower in your switch to get away from case issues. Use a default case to catch any other characters that you don't want.

#include <iostream>
#include <cctype>

using namespace std;

int main(void) {

   char letter;

   cout << "Enter a letter (# to quit): ";

   while((letter = cin.get()) != '#') {
       
      cin.ignore(1); // eat newline char            

      switch(toupper(letter)) {

         case 'A' : 
         case 'B' : 
         case 'C' : cout << "2" << endl;
                    break;         
   
         default : cout << "invalid letter " << letter << endl;
                   break;
      }

      cout << "Enter a letter (# to quit): ";
   }

   return (0);
}

HTH

______________________
My work in progress. I hate JS. :P


Report •
Related Solutions


Ask Question