not showing the total.

Pages: 12
the switch thing doesn't work either. :-(
Is this your desired output?
enter your first number : 2
enter your second number : 3
The sum is : 5
the difference is :  -1
the product is : 6
the qoutient is 1


If so, your code was correct 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
44
#include <iostream>
using namespace std;


void sum(int num1, int num2);
void difference(int num1, int num2);
void product(int num1, int num2);
void qoutient(int num1, int num2);


int main()
{
    int num1;
    int num2;


    cout<<"Enter your first number : ";
    cin>>num1;
    cout<<"Enter your second number : ";
    cin>>num2;

    sum(num1, num2);
    difference(num1, num2);
    product(num1, num2);
    qoutient(num1, num2);


}
void sum(int num1, int num2)
{
    cout<<"The result is : "<<num1+num2;
}
void difference(int num1, int num2)
{
    cout<<"The result is : "<<num1-num2;
}
void product(int num1, int num2)
{
    cout<<"The result is : "<<num1*num2;
}
void qoutient(int num1, int num2)
{
    cout<<"The result is : "<<num1/num2;
}


The only thing you need to add is a new line at the end of the function calls or cout statements:
cout << "The result is : " << num1+num2 << "\n";

If that's not your expected output, what is?
do you mean "pause" the screen?
I don't think anyone here actually understand the OP.

OP: Why exactly are you using functions for something like this?
Last edited on
@chip yeah, it should stop when the result of the sum has come up, instead of showing all the result.



@xhtmlx op stands for operation. :-)
@volatilepulse exactly but it has to stop with the sum result only, so i used a switch but it doesn't work.
@chipp yeah, "pause" the screen.
okay, the result is
1
2
3
4
5
6
enter your first number : 2
enter your second number : 3
The sum is : 5
the difference is :  -1
the product is : 6
the qoutient is 1


but didn't it suppose to output like this?
 
The result is: 5


also it should make me choose which operation im going to use, that's why i used a switch, the problem is that, it didn't work.
What do you mean by stopping? Do you mean adding in a way to "Pause" the program so the user can read the sum, press a button, then read the difference, and so on?

There is a few ways to do that. You can add a system pause (this isn't a good way to do it), you can create your own style of a pause, or do a number of other things.

Do you only want it to display the sum?

Simply remove the calls to the other four functions. If you only want to display the sum, why did you create the functions for difference, product, and quotient? That seems kinda dumb.

If the switch wasn't what you wanted (it allows you to select which answer you want to see), then I'm unsure why you have four functions if you don't want all four.

BTW, OP stands for Original Poster. The comments were saying that we're not 100% as to what you're asking for. If you'd like to clarify, walk us through, step by step, what you'd like your program to do. What you want the display to look like, and what you want the user to enter. I have a fairly good idea as to what you're asking for, but still not positive.
switch isn't magic. All it does is pick something to do based on a value of a variable. It is up to you to set that variable. If you want that variable to be set by the user, you have to write some code to get input from the user.
Simply remove the calls to the other four functions. If you only want to display the sum, why did you create the functions for difference, product, and quotient? That seems kinda dumb.

Why did he create functions in the first place? There is no need to when doing something like this.

OP: Is this not what you want? The code I provided works just fine. http://i.imgur.com/h8YVo.png
Last edited on
If you want to choose the operation, use something like mine or the one xhtmlx showed you. If you use xhtmlx's you will have a result similar to this:
Enter your first number : 5
Enter your second number : 6
Enter operation : +
The result is : 11


Is that not what you're expecting?
@op: is this?

1
2
3
4
5
6
7
8
9
if (sum (num1, num2) > 0) {
      sum (num1, num2);
      cin.get();
      return 0;
}

else
      cout << "the sum is less than 0" << endl;
return 0;
Well, if you want to use what I did then you might as well just do 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
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
#include <iostream>
using namespace std;

void sum(int num1, int num2);
void difference(int num1, int num2);
void product(int num1, int num2);
void qoutient(int num1, int num2);

int main()
{
    int num1;
    int num2;
    char op;

    cout<<"Enter your first number : ";
    cin >> num1 >> op >> num2;

    switch(op) {
        case('+'): // if op = '+'
            sum(num1, num2);
            break;
        case('-'): // if op = '-'
            difference(num1, num2);
            break;
        case('/'): // if op = '/'
            qoutient(num1, num2);
            break;
        case('*'): // if op = '*'
            product(num1, num2);
            break;
    }
    return 0;
}

void sum(int num1, int num2)
{
    cout<<"The result is : "<<num1+num2;
}
void difference(int num1, int num2)
{
    cout<<"The result is : "<<num1-num2;
}
void product(int num1, int num2)
{
    cout<<"The result is : "<<num1*num2;
}
void qoutient(int num1, int num2)
{
    cout<<"The result is : "<<num1/num2;
}


And then we could remove these horrid functions.

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
#include <iostream>

int main()
{
    int num1, num2;
    char op;

    std::cin >> num1 >> op >> num2;

    switch(op) {
        case('+'): // if op = '+'
            std::cout << num1 << ' ' << op << ' ' << num1 << " = " << num1+num2;
            break;
        case('-'): // if op = '-'
            std::cout << num1 << ' ' << op << ' ' << num1 << " = " << num1-num2;
            break;
        case('/'): // if op = '/'
            std::cout << num1 << ' ' << op << ' ' << num1 << " = " << num1/num2;
            break;
        case('*'): // if op = '*'
            std::cout << num1 << ' ' << op << ' ' << num1 << " = " << num1*num2;
            break;
    }
    return 0;
}


Now doesn't that look a lot better?
Last edited on
@op: btw, your request is a little bit strange/weird... in case that what xhtmlx or volatile's codes is not what you wanted, then, your request is gotta be very odd... just from my personal opinion...
Topic archived. No new replies allowed.
Pages: 12