Function calculator

I got a problem here.
Make your calculator program perform computations in a separate function for each type of computation

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
int plus(int y,int x)
{
    return x+y;
}
int minus(int y,int x)
{
    return x-y;
}
int divide(int y,int x)
{
    if(x=0)
    {
        cout<<"error";    //cannot divide by 0

    }
    else
    {
        return x/y;      
    }
}
int times(int y,int x)
{  
    return x*y;          
}
int main()
{
int val1,val2;
cout<<Enter integers;           //I dont know how i will correct this main
cin>>val1>>val2;
cout<<plus<<times<<divide<<times;

}


can someone correct this codes?
line 11 needs 2 equals. you're using the assignment operator at the moment.
I think you need to test for y equalling zero not x.

also you're doing integer division so you're answer will be truncated.

line 30. You do not call functions like this. See:
http://www.cplusplus.com/doc/tutorial/functions/
hello mutexe thank you but it was not my problem

line 30 i dont really get it :(
he means they are functions so need calling like that...
cout<<plus(val1,val2)<<times(val1,val2)<<divide(val1,val2)<<times(val1,val2);

error: reference to 'plus' is ambiguous

can you correct the code and explain it to me please
You have probably just first-hand encountered the problems of having using namespace std in your code. std::plus already exists as a function taking two arguments, while you have provided another possibility for the call, hence the ambiguity of the call. If you instead remove that line and fully qualify your statements, you won't run across that problem later. Try something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

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

// etc. etc. etc.

int main() {
    int val1;
    int val2;

    std::cout << "Enter two numbers: ";
    std::cin >> val1 >> val2;

    std::cout << plus(val1, val2)   << ' '
              << minus(val1, val2)  << ' '
              << times(val1, val2)  << ' '
              << divide(val1, val2) << '\n';

    return 0;
}


What else do you need explaining?
Last edited on
but it was not my problem

line 11 IS a problem.

can you correct the code and explain it to me please

If you'd had read that link i posted it would explain it all for you.
I mean its a typo :D hehe

Im using this site's tutorial of course I've read it

. And thank you this thread solved
Topic archived. No new replies allowed.