Solution
// programmer: Clayton Price
// file: hw5.cpp class: cs1570
// purpose: this file contains the main and auxiliary functions for a program
// that offers the user translation options for char input.
#include <cstdlib>
#include <iostream>
using namespace std;
/* ------------------------ PROTOTYPES ----------------- */
void greetings(); // presents greetings to user
char present_menu(); // presnts a menu of options for user
// handles the options the user inputs
bool option_handler(const char opt,char & input_char, bool & char_entered);
string morse(const char c); // converts input char to morse code
char get_input_from_user(); // gets input char from user
short get_ascii(const char c); // gets the ascii equiv of input char
char change_case(const char c); // outputs the other case of input char
void input_error(); // displays error message
void goodbyes(); // presents exiting message
int main()
{
/* --------------------- DECLARATIONS ---------------------- */
bool quit = false;
char option; // menu option
char input_val; // user input val
bool char_entered = false; // used to flag lack of input
/* -------------------- GREETINGS AND OPERATIONS ------------- */
system ("clear");
greetings();
do
{
option = present_menu();
quit = option_handler(option,input_val,char_entered);
} while (!quit);
/* -------------------- SIGN OFF / END PROGRAM -------------- */
goodbyes();
return 0;
}
/* ----------------------- AUXIALLARY FUNCTIONS ------------- */
void greetings()
{
cout<<"\n\n\nSalutations and Greetings. Enjoy my translator!"<<endl<<endl;
return;
}
char present_menu()
{
char choice;
cout<<"\t\tMENU OF OPTIONS"<<endl<<endl
<<"\t1. Enter a character (alphabetic)"<<endl
<<"\t2. Morse code equivalent"<<endl
<<"\t3. ASCII equivalent"<<endl
<<"\t4. Change u.c. -> l.c. or l.c. -> u.c."<<endl
<<"\t5. Kwit"<<endl<<endl
<<"\tYour choice: ";
cin>>choice;
return choice;
}
bool option_handler(const char opt, char & input_char, bool & char_entered)
{
char change_input; // temp values
string m_code;
short ascii_val;
bool quit_flag = false; // quit or not
switch (opt)
{
case '1': case 'e':
input_char = get_input_from_user();
char_entered = true; // flag input of valid character
break;
case '2': case 'm':
if (char_entered) // check for input of character
{
m_code = morse(input_char);
cout<<"\nThe morse code for "<<input_char<<" is "<<m_code<<endl<<endl;
}
else
input_error();
break;
case '3': case 'a':
if (char_entered)
{
ascii_val = get_ascii(input_char);
cout<<"\nThe ASCII value of "<<input_char<<" is "<<ascii_val<<endl<<endl;
}
else
input_error();
break;
case '4': case 'c':
if (char_entered)
{
change_input = change_case(input_char);
cout<<"\nThe other case of "<<input_char<<" is "<<change_input<<endl<<endl;
}
else
input_error();
break;
case '5': case 'q':
quit_flag = true;
break;
default:
cout<<"\n\n\t******ERROR: Invalid choice; try again......****"<<endl;
}
return quit_flag;
}
short get_ascii(const char c)
{
return static_cast<short>(c);
}
void input_error()
{
cout<<"\n\tERRROR: You did choose option 1 first!"<<endl;
return;
}
char get_input_from_user()
{
char in;
do
{
cout<<"\n\tPlease enter a alphabetic character: ";
cin>>in;
} while (!(in>='A' && in<='Z' || in>='a' && in<='z'));
return in;
}
char change_case(const char c)
{
return ((c >= 'A' && c <= 'Z')?c+32:c-32);
}
void goodbyes()
{
cout<<"\n\nHope you enjoyed your translation experience!"<<endl<<endl;
return;
}
string morse (const char letter)
{ // demonstrating HORRIBLE CODE !!
char c=((letter>='A'&&letter<='Z')?letter+32:letter);
return (c!='a'?(c!='b'?(c!='c'?(c!='d'?(c!='e'?(c!='f'?(c!='g'?(c!='h'?
(c!='i'?(c!='j'?(c!='k'?(c!='l'?(c!='m'?(c!='n'?(c!='o'?(c!='p'?
(c!='q'?(c!='r'?(c!='s'?(c!='t'?(c!='u'?(c!='v'?(c!='w'?(c!='x'?
(c!='y'?"--..":"-.--"):"-..-"):".--"):"...-"):"..-"):"-"):"..."):
".-."):"--.-"):".--."):"---"):"-."):"--"):".-.."):"-.-"):".---"):
".."):"...."):"--."):"..-."):"."):"-.."):"-.-."):"-..."):".-");
}