calculator need help

trying to make a calculator just started learning c++ wats wrong with my code when i remove 3 functions it works but not with all of them there. help pls

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
 
#include "stdafx.h"
# include <iostream>



int divide(int t, int g)
{
	using namespace std;
	return t / g;
}
int multiply(int e, int r)
{
	using namespace std;
	return e * r;
}

int subtract(int q, int w)
{
	using namespace std;
	return q - w;
}

int add(int x, int y)
{
	using namespace std;
	return x + y;
}

int main()
{
	using namespace std;
	
	int type; // variable for funtion chooser
	// function chooser
	cout << "do you want to add, subtract, multiply or divide" << endl;
	cout << "press 1, 2, 3 or 4 respectivly" << endl;
	cin >> type;

	// for adding 
	if (type = 1){
		int a, b;
		cout << "type the first number" << endl;
		cin >> a;
		cout << "type the second number" << endl;
		cin >> b;
		cout << "the answer is " << add(a, b) << endl;
		return 0;
	}
	
    // for subtracting 
	if (type = 2){
		int q, w;
		cout << "type the first number" << endl;
		cin >> q;
		cout << "type the second number" << endl;
		cin >> w;
		cout << "the answer is " << subtract(q, w) << endl;
		return 0;
	}

	// for mulitplying 
	if (type = 3){
		int e, r;
		cout << "type the first number" << endl;
		cin >> e;
		cout << "type the second number" << endl;
		cin >> r;
		cout << "the answer is " << multiply(e, r) << endl;
		return 0;
	}
	// for dividing
	if (type = 4){
		int t, g;
		cout << "type the first number" << endl;
		cin >> t;
		cout << "type the second number" << endl;
		cin >> g;
		cout << "the answer is " << divide(t, g) << endl;
		return 0;
	}
	
	
}

This

1
2
3
int type = 0; // or whatever

if (type = 1){


is setting type to 1 and then testing the return code of operator=, which returns the value just set. That is, it's like

1
2
3
4
5
int type = 0; // or whatever

type = 1;

if (type){


and any int other than 0 is true.

Try

if (type == 1){ // == is comparison

Andy

PS Once it works, try and cut down on the amount of code by sharing the input sequence with all operators.
Last edited on
get rid of this:
#include "stdafx.h"

and you only 'need' this ONCE, at the top (or preferably not at all):
using namespace std;

be careful of this:
1
2
3
4
5
int divide(int t, int g)
{
	return t / g;
}


dividing an integer by an integer will not give you what you think, also you should return a double here, not an int ()e.g. 5 divided by 2 will certainly not yield an integer.

if (type = 1){
that is wrong as well. You need 2 ='s for comparison, not one.

edit: damn, I'm too slow.
Last edited on
thanks
using the double equals fixed it :)
Don't forget to fix your divide function as well..
finished product in case anyone is a noob like me


#include "stdafx.h"
# include <iostream>
using namespace std;

int multiply(int a, int b)
{
return a * b;
}

int subtract(int a, int b)
{
return a - b;
}

int add(int a, int b)
{
return a + b;
}

int main()
{

int type; // variable for funtion chooser
// function chooser
cout << "do you want to add, subtract or multiply" << endl;
cout << "press 1, 2 or 3 respectivly" << endl;
cin >> type;

int a, b;
cout << "type the first number" << endl;
cin >> a;
cout << "type the second number" << endl;
cin >> b;
// for adding
if (type == 1){
cout << "the answer is " << add(a, b) << endl;
return 0;
}
// for subtracting
if (type == 2){
cout << "the answer is " << subtract(a, b) << endl;
return 0;
}

// for mulitplying
if (type == 3){
cout << "the answer is " << multiply(a, b) << endl;
return 0;
}
}
1
2
3
4
double divide(int t, int g)
{
	return t /(static_cast<double> (g));
}
Could you please code tag the final version of your code. So any noob who looks at your code in the future can get it quicker!

Andy

PS For int fanatics, ...

1
2
3
4
5
int divide(int t, int g, int& rem)
{
    	rem = t % g;   
	return t / g;
}


and

1
2
3
4
5
6
7
8
9
    	// for dividing
    	if (type == 4){
    	    	int rem = 0;
    	    	cout << "the answer is " << divide(a, b, rem);
    	    	if(0 < rem)
    	    	    	cout << " rem " << rem;
    	    	cout << "\n";
    	    	return 0;
    	}


AND you're over keen of return 0. One at the end of main() is enough. And else if would help, too (or even a switch statement.)
Last edited on
Topic archived. No new replies allowed.