// Zeinalipour-Yazti Demetrios // csyiazti // cs10xx - // Lab Sec#24 // lab4 /* * This program that lets the user enter a letter, then outputs the number * associated with the given letter on the telephone. No number corresponds * to either Q or Z. For these letters, the program prints a message indicating * that they are not used on a telephone. * ABC=2, DEF=3, GHI=4, JKL=5, MNO=6, PRS=7, TUV=8, WXY=9. * The program will only recognize upper case letters, for all other characters * it will output that it is an invalid character. After outputting the digit * (if there is one associated with the character entered), it asks the user * if they desire to see the written form of the digit and print it if they desire. * */ #include void main() { char letter; /* The letter entered by the user */ bool quit = false; /* Should the program stop executing */ char quitquestion; /* Field to store users answer on quit question */ bool correctinput = false; /* Has the user entered a correct input */ int digit2use; /* The digit that is associated with the input */ /* Getting the letter */ while (!correctinput) //start while1 { cout << "\nPlease type a letter (A-P) or (R-Y) in uppercase: "; cin >> letter; correctinput = true; switch (letter) { /* with gnu compiler you could use * case 'A'..'P' : break; */ case 'A': case 'B': case 'C': digit2use=2; break; case 'D': case 'E': case 'F': digit2use=3; break; case 'G': case 'H': case 'I': digit2use=4; break; case 'J': case 'K': case 'L': digit2use=5; break; case 'M': case 'N': case 'O': digit2use=6; break; case 'P': case 'R': case 'S': digit2use=7; break; case 'T': case 'U': case 'V': digit2use=8; break; case 'W': case 'X': case 'Y': digit2use=9; break; case 'Q': correctinput = false; break; case 'Z': correctinput = false; break; default: correctinput = false; cout << "Error# - Please use only (A-P) or (R-Y) in uppercase!\n\n"; } /* Displaying the associated digit (if any) */ if (letter=='Q' || letter=='Z') { cout << " Q or Z are not used in a telephone " << endl; } else if (correctinput) { cout << endl << letter << " is associated with the digit " << digit2use << endl; } } //end while1 correctinput = false; /* Setting the variable to false again - reuse it*/ /* Asking if the user want to see digit written out */ while (!correctinput) // start while2 { correctinput = true; cout << "\nWould you like to see digit written out? (y/n): "; cin >> quitquestion; cout << endl; switch (quitquestion) { case 'y' : switch (digit2use) { case 1 : cout << digit2use << " is written as one" << endl; break; case 2 : cout << digit2use << " is written as two" << endl; break; case 3 : cout << digit2use << " is written as three" << endl; break; case 4 : cout << digit2use << " is written as four" << endl; break; case 5 : cout << digit2use << " is written as five" << endl; break; case 6 : cout << digit2use << " is written as six" << endl; break; case 7 : cout << digit2use << " is written as seven" << endl; break; case 8 : cout << digit2use << " is written as eight" << endl; break; case 9 : cout << digit2use << " is written as nine" << endl; break; } case 'n' : break; default : correctinput = false; cout << "Error# - Please use \"y\" for yes OR \"n\" for no!\n\n"; } }//end while2 }