Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Upon execution of the program, the menu will be displayed, properly centered as

ID: 3802938 • Letter: U

Question

Upon execution of the program, the menu will be displayed, properly centered as shown below. You will
be using Selection Constructs, the Looping Constructs, user-defined functions and references for this
assignment.


Help Arithmetic Relational   Logical   Quit


Once the menu is displayed, the user is prompted for a menu selection. A wrong menu option is flagged
as an error and the menu is displayed again.
H or h (for Help) will call the function help which briefly explains the problem being solved and the
purpose of each menu options. Once the user strikes a key followed by the return key, the menu will
appear again.
A or a (for Arithmetic) will call a function arithmetic which prompts the user for two integer numbers
followed by an arithmetic operation to be performed according to the following convention. If the user
inputs 1 for the operation, the multiplication operation will be performed. If the user inputs any other
number, the program displays a message stating incorrect operation and then will clear the screen and
shows the menu again. If 1 is entered again, the steps above will be repeated. At no time the program
should be terminated. You will decide the output format to be displayed. Once the user viewed the
results, striking a key will display the menu and the user is prompted again for a menu selection. You can
remain in this mode if you like by adding additional logic of your design. Parameter passing to the
function arithmetic is by reference.
R or r (for Relational) will call the function relational which prompts the user for two integer numbers
followed by a relational operation to be performed according to the following convention. If the user
inputs 2 for the operation, the less than (<) operation will be performed. If the user inputs any other
number, the program displays a message stating incorrect operation and then will clear the screen and
shows the menu again. If 2 is entered again, the steps above will be repeated. At no time the program
should be terminated. You will decide the output format to be displayed. Once the user viewed the
results, striking a key followed by a return key will display the menu and the user is prompted again for a
menu selection. You can remain in this mode if you like by adding additional logic of your design.
L or l (for Logical) will call the function logical which prompts the user for two integer numbers followed
by a logical operation to be performed according to the following convention. If the user inputs 3 for
the operation, the logical OR will be performed between the two integer numbers. If the user inputs any
other number, the program displays a message stating incorrect operation and then will clear the screen
and shows the menu. If 3 is entered again, the steps above will be repeated. At no time the program should be terminated. You will decide the output format to be displayed. Once the user viewed the results, striking a key followed by a return key will display the menu and the user is prompted again for a
menu selection. You can remain in this mode if you like by adding additional logic of your design.
Q or q (for Quit) will clear the screen and terminate the program.
Hints:
1. Your program will only have one main() function and many other user-defined functions as stated above.
In addition to the main() function which must have a return statement, other functions may have return
statements as well. There will be one .cpp file with multiple functions.
2. It is essential that function declarations (prototypes), definitions, and invocations follow the instructions
given in class.
3. Make sure your program executes correctly
4. Make sure the variables are correctly declared without any conflicts with other variables. You may have to
change some of the variables to make each variable unique.
5. Your program will terminate when the user selects Q or q.
6. Use conditional constructs, looping constructs, functions, and references for this assignment.

Explanation / Answer

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <limits>
#include <string>
#include <sstream>

void arithmetic()
{
bool run = true;
int x1 = 0, x2 =0;
while(run)
{
std::cout << "Enter first number: ";
std::string x1_str;
std::getline(std::cin, x1_str);
std::cout << "Enter second number: ";
std::string x2_str;
std::getline(std::cin, x2_str);
std::cout << "Enter operation (1 for Multiplication, 2 for substraction, 3 for division, 4 for addition): ";
std::string op_str;
std::getline(std::cin, op_str);
if(op_str == "1")
{
//convert string to integer
std::stringstream s1(x1_str);
s1 >> x1;
//convert string to integer
std::stringstream s2(x2_str);
s2 >> x2;
std::cout << "Result of multiplication is: "<< x1 * x2 << std::endl;
std::cout << "Press enter to continue";
std::getchar();
run = false;
}
else if(op_str == "2")
{
//convert string to integer
std::stringstream s1(x1_str);
s1 >> x1;
//convert string to integer
std::stringstream s2(x2_str);
s2 >> x2;
std::cout << "Result of substraction is: "<< x1 - x2 << std::endl;
std::cout << "Press enter to continue";
std::getchar();
run = false;
}
else if(op_str == "3")
{
//convert string to integer
std::stringstream s1(x1_str);
s1 >> x1;
//convert string to integer
std::stringstream s2(x2_str);
s2 >> x2;
std::cout << "Result of division is: "<< x1 / x2 << std::endl;
std::cout << "Press enter to continue";
std::getchar();
run = false;
}
else if(op_str == "4")
{
//convert string to integer
std::stringstream s1(x1_str);
s1 >> x1;
//convert string to integer
std::stringstream s2(x2_str);
s2 >> x2;
std::cout << "Result of addition is: "<< x1 + x2 << std::endl;
std::cout << "Press enter to continue";
std::getchar();
run = false;
}
else
{
std::cout << "Incorrect operation, press any key to continue";
std::getchar();
system("clear");
}
}
}
void relational()
{
bool run = true;
int x1 = 0, x2 =0;
while(run)
{
//get first number
std::cout << "Enter first number: ";
std::string x1_str;
std::getline(std::cin, x1_str);
//get second number
std::cout << "Enter second number: ";
std::string x2_str;
std::getline(std::cin, x2_str);
std::cout << "Enter operation (1 for >, 2 for <): ";
std::string op_str;
std::getline(std::cin, op_str);
if(op_str == "1")
{
//convert string to integer
std::stringstream s1(x1_str);
s1 >> x1;
//convert string to integer
std::stringstream s2(x2_str);
s2 >> x2;
std::cout << "Result of relational opearator (>) is: "<< (x1 > x2? "True":"False") << std::endl;
std::cout << "Press enter to continue";
std::getchar();
run = false;
}
else if(op_str == "2")
{
//convert string to integer
std::stringstream s1(x1_str);
s1 >> x1;
//convert string to integer
std::stringstream s2(x2_str);
s2 >> x2;
std::cout << "Result of relational opearator (<) is: "<< (x1 < x2?"True":"False") << std::endl;
std::cout << "Press enter to continue";
std::getchar();
run = false;
}
else
{
std::cout << "Incorrect operation, press any key to continue";
std::getchar();
system("clear");
}
}
}

void logical()
{
bool run = true;
int x1 = 0, x2 =0;
while(run)
{
//get first number
std::cout << "Enter first number: ";
std::string x1_str;
std::getline(std::cin, x1_str);
//get second number
std::cout << "Enter second number: ";
std::string x2_str;
std::getline(std::cin, x2_str);
//get operator
std::cout << "Enter operation (1 for AND, 3 for OR): ";
std::string op_str;
std::getline(std::cin, op_str);
if(op_str == "1")
{
//convert string to integer
std::stringstream s1(x1_str);
s1 >> x1;
//convert string to integer
std::stringstream s2(x2_str);
s2 >> x2;
std::cout << "Result of logical opearator (AND) is: " << (x1 & x2) << std::endl;
std::cout << "Press enter to continue";
std::getchar();
run = false;
}
else if(op_str == "3")
{
//convert string to integer
std::stringstream s1(x1_str);
s1 >> x1;
//convert string to integer
std::stringstream s2(x2_str);
s2 >> x2;
std::cout << "Result of logical opearator (OR) is: " << (x1 | x2) << std::endl;
std::cout << "Press enter to continue";
std::getchar();
run = false;
}
else
{
std::cout << "Incorrect operation, press any key to continue";
std::getchar();
system("clear");
}
}
}


int main()
{
bool run = true;
while(run)
{
//loop until we get q/Q as user input
std::cout << "Help Arithmetic Relational Logical Quit" << std::endl;
std::cout << "Enter menu option: " ;
//get the option from user
char ch = std::getchar();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), ' ');
if(ch == 'q' || ch == 'Q')
{
//clear the screen and exit
system("clear");
run = false;
}
else if(ch == 'h' || ch == 'H')
{
//print help message
std::cout << "This program does arithmetic, logical, relational operations on two integers" << std::endl;
std::cout << "Press h/H for printing help" << std::endl;
std::cout << "Press a/A for arithmetic operations" << std::endl;
std::cout << "Press r/R for relational operations" << std::endl;
std::cout << "Press l/L for logical operations" << std::endl;
std::cout << "Press q/Q to exit program" << std::endl;
std::cout << "Press enter to continue: ";
std::getchar();
}
else if(ch == 'a' || ch == 'A')
{
//call arithmetic function
arithmetic();
}
else if(ch == 'r' || ch == 'R')
{
//call relational function
relational();
}
else if(ch == 'l' || ch == 'L')
{
//call logical function
logical();
}
else
{
//print invalid option message
std::cout << "Invalid menu selection, Possible values are [q/h/a/r/l/Q/H/A/R/L]" << std::endl;
}
}
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote