Lets take a look at your code. The instructions says to,
At the beginning, the program displays the value of the integer entered and write the following menu: |
However, you are asking the user for the value, after the menu (Line 18-19). You should move those two line above the menu. Take a look at lines 22,26,30,34 and 38. You are comparing the variable
choice
, which is an integer (according to Line 9) to characters (i.e.
'1', '2', '3','4'
). You need to take the ' ' off since you need to compare integers with integers. Let's take a look at another instruction,
If the user enters a value from 1 to 3, the operation will be computed and the result will be displayed before showing the menu again. |
Since it wants to see the menu again, you may want to use a do/while loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
int choice, value;
double add, mutiply, subtract;
cout << "1.Add 1" << endl;
cout << "2.Mutiply by 2" << endl;
cout << "3.Subtract by 4" << endl;
cout << "4.Quit" << endl;
cout << endl;
cout << "Enter value : ";
cin >> value;
while (!(choice >= '1' && choice <= '4'))
{
cout << "Choose value between 1 and 4 : ";
cin >> choice;
if (choice == '1'){
add = value + 1;
cout << "Final answer is " << add << endl;
}
if (choice == '2'){
mutiply = value * 4;
cout << "Final answer is " << mutiply << endl;
}
if (choice == '3'){
subtract = value - 4;
cout << "Final answer is " << subtract << endl;
}
if (choice == '4'){
break;
}
}
system("Pause");
return 0;
}
|
A DISCLAIMER: Below is just a way and not the way. I would use switch, but for the sake of getting it close to your code, below is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
int choice, value;
double add, mutiply, subtract;
do
{
cout << "Enter value : ";
cin >> value;
cout << "1.Add 1" << endl;
cout << "2.Mutiply by 2" << endl;
cout << "3.Subtract by 4" << endl;
cout << "4.Quit" << endl;
cout << endl;
cout << "Enter choice : ";
cin >> choice;
if (choice == 1){
add = value + 1;
cout << "Final answer is " << add << endl;
}
if (choice == 2){
mutiply = value * 2;
cout << "Final answer is " << mutiply << endl;
}
if (choice == 3){
subtract = value - 4;
cout << "Final answer is " << subtract << endl;
}
if (choice == 4){
break;
}
} while (choice > 0 && choice < 4);
return 0;
}
|
I hope this helps you on giving you the idea.
EDIT: Take a look at line 31 on your code. Is it suppose to be
value * 4
or
value * 2
?