Help with this program.

Write a program that is able to compute some operations on an integer received from the user. At the beginning, the program displays the value of the integer entered and write the following menu:

1. Add 1
2. Mutiply by 2
3. Subract 4
4. Quit

The program asks the user to choose a value betwwen 1 and 4 for desired operation. 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. If the user enters 4, the program quits.


This is my code;

Im new to C++. can anyone help me to fix it ? it seems unable to load after enter value and the program show the variable of choice is uninitialized.

#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;
}


Last edited on
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?
Last edited on
@chicofeo

thank you for your solution. before you post i managed to solve it.

ops.. sorry suppose to be (value * 2)

this is my code:
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()
{
	char choice;
	int 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<<"Choose value between 1 and 4 : ";
	cin>>choice;
	if(choice=='1')
	{
		add=value + 1;
		cout<<"Final answer is "<<add<<endl;
	}
	else if(choice=='2')
	{
		mutiply=value * 2;
		cout<<"Final answer is "<<mutiply<<endl;
	}
	else if(choice=='3')
	{
		subtract=value - 4;
		cout<<"Final answer is "<<subtract<<endl;
	}
	else if(choice=='4')
	{
		break;
	}
}while(!(choice >= '1' && choice <= '4'));

	system("Pause");
	return 0;
}



would you show me for switch case ? i would like to learn another skill for this program.
Last edited on
Take a look at this tutorial:

http://www.cplusplus.com/doc/tutorial/control/ (The switch portion is towards the end)

Besides, you may be learning switch case in your class pretty soon.

Okay. Thank you
Topic archived. No new replies allowed.