is my program correct ?

write a program that is able to compute some operations on an integer. The program writes the value of the integer and writes the following menu:

1. Add 1
2. Multiply by 2
3. Subtract 4
4. Quit

The program ask the user to type a value between 1 and 4. If the user types a value from 1 to 3 the operation is computed, the integer is written and the menu is displayed again. If the user types 4, the program quits.

#include <iostream>
using namespace std;

int main()
{
int a, MenuIn; //Moved 'a' here and declared another int MenuIn.
do{ //Your do-while would only repeat outputs, this lets the user also change the input.
cout<<"Enter a number:";
cin>>a;

cout<<endl << "1. Add 1"; //You will want to end lines or they will be printed one after another
cout<<endl << "2. Multiply by 2";
cout<<endl << "3. Subtract 4" <<endl << "4. Quit"<<endl; //You can use one cout if you like for different lines, just seperate them with 'endl'
cin >> MenuIn; //You need to let the user input something here.


if(a==1)cout<<"The sum is:"<<a+1<<endl;
if(a==2)cout<<"The product is:"<<a*2<<endl;
if(a==3)cout<<"The difference is:"<<a-3<<endl;
if(a==4)break;
}while(a<5); //Your-do while loop didn't change 'a' and so it would repeat forever it it didn't return (which it would),

cin.get(); //Don't use system("anything")

return 0;
}

is my work, correct. i was able to run it, but i am not sure if it is right.
It's not right, because you check 'a' instead of 'MenuIn'. Do a look here:
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
#include <iostream>

using namespace std;


enum { non = -1, add = 1, mult, subs, quit };


int main()
{
	for(int number, item = non; item != quit; )
	{
		cout << "Input the number:   ";
		cin >> number;

		cout << "Chose one of this items:" << endl
			<< "\t1) Add one" << endl
			<< "\t2) Multiply by two" << endl
			<< "\t3) Substract four" << endl
			<< "\t4) Quit" << endl << endl;
		cin >> item;

		switch(item)
		{
		case add:
			cout << "Result:   " << number + 1;
			break;
		case mult:
			cout << "Result:   " << number * 2;
			break;
		case subs:
			cout << "Result:   " << number - 4;
			break;
		case quit:
			break;
		default:
			cout << "Unresolved number was inputed. Try again.";
		}
		cout << endl << endl;
	}

	return 0;
}
Topic archived. No new replies allowed.