Calculator Not Working Right

Hey, so i tried the calculator problem from "Jumping Into C++" chapter 6 problem 2. it asks you to create a calculator where the computations are performed in separate functions. I did it to the best of my abililty, and there are no errors anymore, but it does not work correctly. For example, Adding 5 & 5 gives out 1. Please Help. Thanks in advance.

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
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;


int add_fun();
int sub_fun();
int mult_fun();
int div_fun();

int main()
{
    int a,b,c;
cout<<"Enter a Number"<<endl;
cin>>a;
cout<<"One More"<<endl;
cin>>b;
cout<<"1 for addition, 2 for subtraction, 3 for multiplication, 4 for division"<<endl;
cin>>c;

if (c==1)
    {
int add_fun(int a,int b);
    }
else if (c==2)
    {
int sub_fun();
    }
else if (c==3)
    {
int mult_fun();
    }
else
    {
int div_fun();
    }
    }


int add_fun(int a,int b)
    {

        cout<< a+b<<endl;
    }

int sub_fun(int a, int b)
{
 cout<<a-b<<endl;
}


int mult_fun(int a, int b)
    {
        cout<<a*b<<endl;
    }


int div_fun (int a, int b)
    {
    cout<<a/b<<endl;
    }
closed account (Dy7SLyTq)
i would suggest taking a look at calling functions. i coud explain your mistake but there are better pages. this looks good: http://www.cplusplus.com/doc/tutorial/functions/
Here i fixed your code.But one question why did you included <cstdlib> and <string>?You know you dont actually need them to run your program...
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
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;


void add_fun(int a,int b);
void sub_fun(int a,int b);
void mult_fun(int a,int b);
void div_fun(int a,int b);

int main()
{
    int a,b,c;
cout<<"Enter a Number"<<endl;
cin>>a;
cout<<"One More"<<endl;
cin>>b;
cout<<"1 for addition, 2 for subtraction, 3 for multiplication, 4 for division"<<endl;
cin>>c;

if (c==1)
    {
add_fun(a,b);
    }
else if (c==2)
    {
sub_fun(a,b);
    }
else if (c==3)
    {
mult_fun(a,b);
    }
else
    {
div_fun(a,b);
    }
    return 0;
    }


void add_fun(int a,int b)
    {

        cout<< a+b<<endl;
    }

void sub_fun(int a, int b)
{
 cout<<a-b<<endl;
}


void mult_fun(int a, int b)
    {
        cout<<a*b<<endl;
    }


void div_fun (int a, int b)
    {
    cout<<a/b<<endl;
    }
@OP: You dont want to put 'int' before the functions, you call inside the 'if' / 'else'
All you have to do is put them like this:
1
2
3
if(c==1)
   add_func(a, b);
else //...// 


Like SorionAlex's example ^
alright thanks guys. yeah i dont know why i put string and cstdlib, i guess i forgot to take em out
Topic archived. No new replies allowed.