Create the Following Menu in a loop so the menu will continually show until the
ID: 3554422 • Letter: C
Question
Create the Following Menu in a loop so the menu will continually show until the user chooses to exit.
Add a value returning function to menu option 1 and menu option 2. This means that you will create two functions: an add function and a multiply function. The functions will receive two doubles and return a double.
The following is code I have written to complete this assignment. When entering option 3 after entering option 1 or 2, it does not exit from the program. Also, the menu is appended instead of deleting everything and displaying the original menu.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int options ();
int option1 ();
int option2 ();
int option3 ();
double addition (double num1, double num2);
double multiplication (double num1, double num2);
int main()
{
int option = 0;
cout << "1. Add" << endl;
cout << "2. Multiply" << endl;
cout << "3. Exit" << endl;
cout << "Enter option 1 for addition, option 2 for multiplication, or option 3 to exit"
" Enter an option: ";
cin >> option;
switch (option)
{
case 1:option1;
int option1 ();
{
double num1 = 0;
double num2 = 0;
// void int key = 0;
cout << " Enter a number: ";
cin >> num1;
cout<< " Enter a second number: ";
cin >> num2;
double sum = addition(num1, num2);
cout << " Sum is: " << sum <<endl;
cout << "Press any key to continue: ";
// cin >> key;
system("cls");
}
break;
case 2: option2;
int option2 ();
{
double num1 = 0;
double num2 = 0;
cout << " Enter a number:";
cin >> num1;
cout << " Enter a second number: ";
cin >> num2;
double product = multiplication(num1, num2);
cout << " Product is: " << product << endl;
system("cls");
}
break;
case 3: option3;
int option3();
{
return 0;
}
break;
default:
cout << " Invalid number entered: ";
}
//
//do {main();}
//while (option < 3);
}
double addition(double num1, double num2)
{
return num1 + num2;
}
double multiplication(double num1, double num2)
{
return num1*num2;
}
Explanation / Answer
I have made only few changes.
1. Added a separate function to clear screen for both WINDOWS and LINUX.
2. Uncommented your do while loop, it works fine.
3, Used exit(0) in option3() function and also included <cstdlib> accordingly.
Please have a look...
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
#include <cstdlib>
#define LINUX 1
int options ();
int option1 ();
int option2 ();
int option3 ();
double addition (double num1, double num2);
double multiplication (double num1, double num2);
void console_clear_screen() {
#ifdef WINDOWS
system("cls");
#endif
#ifdef LINUX
system("clear");
#endif
}
int main()
{
int option = 0;
cout << "1. Add" << endl;
cout << "2. Multiply" << endl;
cout << "3. Exit" << endl;
cout << "Enter option 1 for addition, option 2 for multiplication, or option 3 to exit"
" Enter an option: ";
cin >> option;
switch (option)
{
case 1:option1;
int option1 ();
{
double num1 = 0;
double num2 = 0;
// void int key = 0;
cout << " Enter a number: ";
cin >> num1;
cout<< " Enter a second number: ";
cin >> num2;
double sum = addition(num1, num2);
console_clear_screen();
cout << " Sum is: " << sum <<endl <<endl <<endl;
cout << "Press any key to continue: ";
// cin >> key;
}
break;
case 2: option2;
int option2 ();
{
double num1 = 0;
double num2 = 0;
cout << " Enter a number:";
cin >> num1;
cout << " Enter a second number: ";
cin >> num2;
double product = multiplication(num1, num2);
console_clear_screen();
cout << " Product is: " << product << endl <<endl <<endl;
}
break;
case 3: option3;
int option3();
{
console_clear_screen();
std:exit(0);
}
break;
default:
cout << " Invalid number entered: ";
}
//
do {
main();
}while (option < 3);
}
double addition(double num1, double num2)
{
return num1 + num2;
}
double multiplication(double num1, double num2)
{
return num1*num2;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.