return to start

I'm trying to make a calculator in which user can use it until a wrong key is given by user. After result when i press y to start the process again it start showing all the code in cmd and keeps continue. please help me how to stop the process so i can give values again.

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
48
#include<iostream>
using namespace std;
int main()
{
	int a;
	int b;
	int c;
	int y;
	char c1;
	char c2;
	char c3;
	char c4;

start:


	cout<<"Enter The First Value"<<endl;
	cin>>a;
	cout<<"Enter The Second Value"<<endl;
	cin>>b;

	cout<<"Enter 1 to Add"<<endl;
	cout<<"Enter 2 to Subtract"<<endl;
	cout<<"Enter 3 to Multiply"<<endl;
	cout<<"Enter 4 to Divide"<<endl;
cin>>c;

if(c==1)
{cout<<"The Sum Is: "<<a+b<<endl;}
else if(c==2)
{cout<<"The Difference Is: "<<a-b<<endl;}
else if(c==3)
{cout<<"The Multiplication Is: "<<a*b<<endl;}
else if(c==4)
{cout<<"The Product Is: "<<a/b<<endl;}

else 
{cout<<"Wrong Entry"<<endl;}

cout<<"If You Want To Continue Press Y"<<endl;
cin>>y;
if(y)
{goto start;}
else 

return 0;
}

Well first, you haven't declared a variable called "c", so create char c; so the option system works. The other thing to do is to make the calculation part into a function, which would look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int calc()
{
// code for calculation
}

int main()
{

do
{
calc();
cout<<"If You Want To Continue Press Y"<<endl;
cin>>y;
}while(y == 'Y');

return 0;
}


Base your program on the one above and it should bring the user back to the calc function every time they enter y.
Last edited on
http://www.cplusplus.com/forum/general/89535/ why making a second topic about the same thing?
Anyway, if you use a char to read the input for the operation to perform, its value will be equal to the ASCII code of the number
http://www.cplusplus.com/doc/ascii/ shows that '1' == 0x31 == 49
So to check the input you must do if(c == '1') or if(c == 49). The first one is preferred for readability

However if c is an int, its value will be the actual number the user enters

If you use the approach softwaretime suggested, remember to declare y inside main
Just modify 'if(y)' to 'if(y == 'Y')'
Topic archived. No new replies allowed.