not showing the total.

Pages: 12
guys, how can the total show up?
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
#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;



}
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;
}
What total? Specify your errors, problems so we can help you.
the program does run, but when im entering numbers, there's no over all total, kindly try it in your codeblock. :-) thanks.
did you call the function?
You're not calling any functions.
You never called the 'sum' function. ._.
i do not know if this is what you want but 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<<endl;
}
void difference(int num1, int num2)
{
    cout<<"The result is : "<<num1-num2<<endl;
}
void product(int num1, int num2)
{
    cout<<"The result is : "<<num1*num2<<endl;
}
void qoutient(int num1, int num2)
{
    cout<<"The result is : "<<num1/num2<<endl;
}
}

Edit: Thanks chipp
Edit Edit: I really should run my code before posting, this time it works, I am sure of it, I hope
Last edited on
@coder: no offense, really... your example is wrong bro... check again...
i did call the sum function, but i removed it cause it still has the same result.
@coder oh guys it worked, look what i did,
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
#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 new problem is that it wont stop. kindly try it guys. please?
the new problem is that it wont stop. kindly try it guys. please?

What do you mean by 'it won't stop'? I tried it and it works as expected.
Last edited on
@Dash ok thanks, i will. :-)
@xhtmlx it did show the whole result. it suppose to stop in sum right? oh wait. should i put a switch in it?
@xhtmlx it did show the whole result. it suppose to stop in sum right? oh wait. should i put a switch in it?

No, you called the four functions (difference, sum, product, quotient). You mean the console is closing before you can see the results, correct? Simply add something like 'std::cin.get()' to the end of main.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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);
  
    cin.get();
    return 0;
}


You should also cast 'num1' and 'num2' as doubles when dividing the two. If you don't then the two numbers will be rounded down and there will be no decimal point displayed.

1
2
3
4
void qoutient(int num1, int num2)
{
    cout<<"The result is : "<<static_cast<double>(num1)/static_cast<double>(num2);
}


or the 'C-styled' cast:

1
2
3
4
void qoutient(int num1, int num2)
{
    cout<<"The result is : "<<(double)num1/(double)num2;
}


There is a difference between the two.
Last edited on
@xhtmlx the result is still the same, continuos,. im wondering how could it stop in sum, then next ill run it ill try the difference and so on,,,.
What do you mean by "continuous" ? Do you know how the logic flow of the program work ?
@nicole
What's happening is that you're calling all four functions after all of your input. The issue with this is that each one of them will execute their code, return to main, then move down to the next function call. In turn, you're adding, subtracting, multiplying, and dividing every time. You had the right idea with the switch. This will allow you to manually select which function you want to call. A quick example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char operation;

cout << "(A)dd\n(S)ubtract\n(M)ultiply\n(D)ivide";
cin >> operation;

switch (operation) {
   // Addition
   case 'A':
   case 'a':
      sum(num1, num2);
      break;
   // Subtraction
   case 'S':
   case 's':
      // ...
}
Last edited on
@xhtmlx the result is still the same, continuos,. im wondering how could it stop in sum, then next ill run it ill try the difference and so on,,,.


It's hard to understand you, so I'll try my best..

1
2
3
Enter your first number : 5
Enter your second number : 10
The result is: 15


Next Run:
1
2
3
Enter your first number : 14
Enter your second number : 11
The result is: 3


And so on until all the functions have been called?


You had the right idea with the switch. This will allow you to manually select which function you want to call. A quick example:

That's what he's been trying to tell us this whole time?! Derp. >_>

In that case, here's how I would implement a switch statement into 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
#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;
    cout<<"Enter your second number : ";
    cin>>num2;
    cout<<"Enter operation: ";
    cin>>op;

    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;
}
Last edited on
ill try that, because the result would be for example
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


you get me? :-)
Pages: 12