Calculator program passing values by reference.

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

using namespace std;

void CalcIntResults(int nFirst, int nSecond, int &nResult);

int main(void)
{
    int nFirst = 0;
    int nSecond = 0;
    int nResult = 0;

    cout << "Please enter two integer values: " << endl;
    cin >> nFirst >> nSecond;

    CalcIntResults(nFirst, nSecond, nResult);

    if (nSecond == 0)
    {
        cout << nFirst << " divided by 0 equals 0" << endl
             << "Division by zero is undefined..." << endl;
    }

}

void CalcIntResults(int nFirst, int nSecond, int &nResult)
{
    cout << "Here are the results: " << endl;

    nResult = nFirst + nSecond;
    cout << nFirst << " plus " << nSecond << " equals " << nResult << endl;

    nResult = nFirst - nSecond;
    cout << nFirst << " minus " << nSecond << " equals " << nResult << endl;

    nResult = nFirst * nSecond;
    cout << nFirst << " multiplied " << nSecond << " equals " << nResult << endl;

    if (nSecond > 0)
    {
        nResult = nFirst / nSecond;

        cout << nFirst << " divided by " << nSecond << " equals " << nResult
             << " with a remainder of " << nFirst % nSecond << endl;
    }
}


Is my code. Am I doing it right?

Here are the instructions:
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
You are to write a short program that prompts the user for two integer values. 
Then these values are passed to the CalcIntResults function, 
which will derive the sum, difference, product, and quotient with remainder 
all in one function call! But how can a function return more than one value? 
By using formal parameters that are reference variables.

So the main function needs to declare plenty of local variables, so that it 
can pass the following arguments to the CalcIntResults function:

the value of the first operand (pass by value)
the value of the second operand (pass by value)
a variable to store the sum (pass by reference)
a variable to store the difference (pass by reference)
a variable to store the product (pass by reference)
a variable to store the quotient (pass by reference)
a variable to store the remainder (pass by reference)

The CalcIntResults function returns no value to the caller. After the call 
to the CalcIntResults function returns, main must display the results to 
stdout. But that's not all -- what will you do if the user enters a value 
of zero for the second operand? If you don't handle the situation, your
program will crash because the CPU will not allow division by zero. 
That means CalcIntResults must not allow division by zero to occur, 
and that the main function must write an appropriate message when 
displaying the final results of the calculations.
Am I doing it right?

No. The instructions say to display the results in main.
main must display the results to stdout


You're not passing the required number of arguments to CalcIntResults. The instructions list 7 arguments that must be passed.

You're displaying an error message on diveide by zero inside CalcIntResult. The instructions say to display that message from main.
main function must write an appropriate message
@AbstractionAnon: yeah, I realized that and I fixed it accordingly.
This is what I have so far, but I'm having trouble 'returning' the required arithmetic from the function:

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

using namespace std;

void CalcIntResults(int nFirst, int nSecond, int &nResult);

int main(void)
{
    int nFirst = 0;
    int nSecond = 0;
    int nResult = 0;
    int nAdd = 0;
    int nSubtract = 0;
    int nMultiply = 0;
    int nDivide = 0;

    cout << "Please enter two integer values: " << endl;
    cin >> nFirst >> nSecond;

    cout << "Here are the results: " << endl;

    CalcIntResults(nFirst, nSecond, nAdd);
    cout << nFirst << " plus " << nSecond << " equals " << nAdd << endl;

    CalcIntResults(nFirst, nSecond, nSubtract);
    cout << nFirst << " minus " << nSecond << " equals " << nSubtract << endl;

}

void CalcIntResults(int nFirst, int nSecond, int &nResult)
{
    int nAdd = nFirst + nSecond;
    int nSubtract = nFirst - nSecond;
}


Note: the code isn't close to being finished yet, thus some variables are still unused. Ignore those for now.
Lets focus on the addition first.

At line 32, you're setting nAdd to the result of your additon. What you want to do is pass the result back through the third argument (I think the name nResult might be confusing you).

What happens if we change CalcIntResults as follows:
1
2
3
4
void CalcIntResults(int nFirst, int nSecond, int &nAdd)
{
    nAdd = nFirst + nSecond;
} 

Now, can you extend that to handle subtraction, multiplication and division?



Last edited on
@AbstractionAnon: See, that's what I had first, but then I went ahead and did this:
1
2
3
4
5
void CalcIntResults(int nFirst, int nSecond, int &nResult)
{
    nResult = nFirst + nSecond;
    nResult = nFirst - nSecond;
}


And what would happen is that the nResult for addition would get 'lost' and the only thing that would come back out would be subtraction.

This is the way I currently have it set up, where I'm stuck.
1
2
3
4
5
6
7
8
9
10
11
12
13
CalcIntResults(nFirst, nSecond, nAdd);
    cout << nFirst << " plus " << nSecond << " equals " << nAdd << endl;

    CalcIntResults(nFirst, nSecond, nSubtract);
    cout << nFirst << " minus " << nSecond << " equals " << nSubtract << endl;

}

void CalcIntResults(int nFirst, int nSecond, int &nResult)
{
    nResult = nFirst + nSecond;
    nResult = nFirst - nSecond;
}
Last edited on
Read the instructions closely. The instructions tell you to pass 7 arguments. 2 by value and 5 by reference. The instructions tell you want each argument is for.

the value of the first operand (pass by value)
the value of the second operand (pass by value)
a variable to store the sum (pass by reference)
a variable to store the difference (pass by reference)
a variable to store the product (pass by reference)
a variable to store the quotient (pass by reference)
a variable to store the remainder (pass by reference)
Last edited on
Apparently, the program is as easy as doing 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
51
#include <iostream>

using namespace std;

void CalcIntResults(int nFirst, int nSecond, int &nAdd, int &nSubtract, int &nMultiply, int &nDivide);

int main(void)
{
    int nFirst = 0;
    int nSecond = 0;
    int nResult = 0;
    int nAdd = 0;
    int nSubtract = 0;
    int nMultiply = 0;
    int nDivide = 0;

    cout << "Please enter two integer values: " << endl;
    cin >> nFirst >> nSecond;

    cout << "Here are the results: " << endl;

    CalcIntResults(nFirst, nSecond, nAdd, nSubtract, nMultiply, nDivide);
    cout << nFirst << " plus " << nSecond << " equals " << nAdd << endl;

    CalcIntResults(nFirst, nSecond, nAdd, nSubtract, nMultiply, nDivide);
    cout << nFirst << " minus " << nSecond << " equals " << nSubtract << endl;

    CalcIntResults(nFirst, nSecond, nAdd, nSubtract, nMultiply, nDivide);
    cout << nFirst << " multiplied by " << nSecond << " equals " << nMultiply << endl;

    if (nSecond == 0)
    {
        cout << nFirst << " divided by 0 equals 0" << endl;
    }

    else if (nSecond > 0)
    {
        CalcIntResults(nFirst, nSecond, nAdd, nSubtract, nMultiply, nDivide);
        cout << nFirst << " divided by " << nSecond << " equals " << nDivide << endl;
    }
}

void CalcIntResults(int nFirst, int nSecond, int &nAdd, int &nSubtract, int &nMultiply, int &nDivide)
{
    nAdd = nFirst + nSecond;
    nSubtract = nFirst - nSecond;
    nMultiply = nFirst * nSecond;
    nDivide = nFirst / nSecond;
}



The function as it stand is what I thought of doing first, but I though, 'no, that seems too easy..'

Anyway, I am now having this problem: whenever I input '0' as my second integer, I want it to display my first 'if' statement. However, it always just bypasses that statement and goes straight through to attempting to do the calculation.
Yes, it's that easy. A few things to clean up though.

You don't need to call CalcIntResult 4 times. All results are calculated in one call.

You need a check inside CalcIntResults to skip the division if nSecond is zero. You don't want to take a divide by zero trap.

Because you're calling CalcIntResults 4 times, you doing the divison 3 times before you ever get to your if statement at line 31.



Yeah, I changed it accordingly. However, I can't get rid of the divide by 0 error and I don't understand why because logically, I feel like I'm doing the if/else statement correctly.

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

using namespace std;

void CalcIntResults(int nFirst, int nSecond, int &nAdd, int &nSubtract, int &nMultiply, int &nDivide);

int main(void)
{
    int nFirst = 0;
    int nSecond = 0;
    int nResult = 0;
    int nAdd = 0;
    int nSubtract = 0;
    int nMultiply = 0;
    int nDivide = 0;

    cout << "Please enter two integer values: " << endl;
    cin >> nFirst >> nSecond;

    cout << "Here are the results: " << endl;

    CalcIntResults(nFirst, nSecond, nAdd, nSubtract, nMultiply, nDivide);

    cout << nFirst << " plus " << nSecond << " equals " << nAdd << endl;
    cout << nFirst << " minus " << nSecond << " equals " << nSubtract << endl;
    cout << nFirst << " multiplied by " << nSecond << " equals " << nMultiply << endl;
}

void CalcIntResults(int nFirst, int nSecond, int &nAdd, int &nSubtract, int &nMultiply, int &nDivide)
{
    nAdd = nFirst + nSecond;
    nSubtract = nFirst - nSecond;
    nMultiply = nFirst * nSecond;

    if (nSecond == 0)
    {
        cout << nFirst << " divided by 0 equals 0" << endl;
    }

    else if (nSecond >= 1)
    {
        nDivide = nFirst / nSecond;
        
        cout << nFirst << " divided by " << nSecond << " equals " << nDivide
             << " with a remainder of " << nFirst % nSecond << endl;
    }
}

Nevermind, I just did this instead:

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

using namespace std;

void CalcIntResults(int nFirst, int nSecond, int &nAdd, int &nSubtract, int &nMultiply, int &nDivide);

int main(void)
{
    int nFirst = 0;
    int nSecond = 0;
    int nResult = 0;
    int nAdd = 0;
    int nSubtract = 0;
    int nMultiply = 0;
    int nDivide = 0;

    cout << "Please enter two integer values: " << endl;
    cin >> nFirst >> nSecond;

    cout << "Here are the results: " << endl;

    if (nSecond == 0)
    {
        cout << nFirst << " plus 0 equals 0" << endl;
        cout << nFirst << " minus 0 equals 0" << endl;
        cout << nFirst << " multiplied by 0 equals 0" << endl;
        cout << nFirst << " divided by 0 equals 0" << endl;
    }

    else
    {
    CalcIntResults(nFirst, nSecond, nAdd, nSubtract, nMultiply, nDivide);

    cout << nFirst << " plus " << nSecond << " equals " << nAdd << endl;
    cout << nFirst << " minus " << nSecond << " equals " << nSubtract << endl;
    cout << nFirst << " multiplied by " << nSecond << " equals " << nMultiply << endl;
    cout << nFirst << " divided by " << nSecond << " equals " << nDivide
         << " with a remainder of " << nFirst % nSecond << endl;

    }
}

void CalcIntResults(int nFirst, int nSecond, int &nAdd, int &nSubtract, int &nMultiply, int &nDivide)
{
    nAdd = nFirst + nSecond;
    nSubtract = nFirst - nSecond;
    nMultiply = nFirst * nSecond;
    nDivide = nFirst / nSecond;
}



Although I feel like there's a better solution. Thoughts?

Edit: I just fixed my if statement. Please disregard that brain fart on my part.
Last edited on
I would take exception to the following two lines:
1
2
  cout << nFirst << " plus 0 equals 0" << endl;
  cout << nFirst << " minus 0 equals 0" << endl;

Last time I checked, adding or subtracting 0 doesn't equal 0 unless both numbers are 0.
I edited that part in my last reply. I changed my code to the following:

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

using namespace std;

void CalcIntResults(int nFirst, int nSecond, int &nAdd, int &nSubtract, int &nMultiply, int &nDivide);

int main(void)
{
    int nFirst = 0;
    int nSecond = 0;
    int nResult = 0;
    int nAdd = 0;
    int nSubtract = 0;
    int nMultiply = 0;
    int nDivide = 0;

    cout << "Please enter two integer values: " << endl;
    cin >> nFirst >> nSecond;

    cout << "Here are the results: " << endl;

    CalcIntResults(nFirst, nSecond, nAdd, nSubtract, nMultiply, nDivide);

    cout << nFirst << " plus " << nSecond << " equals " << nAdd << endl;
    cout << nFirst << " minus " << nSecond << " equals " << nSubtract << endl;
    cout << nFirst << " multiplied by " << nSecond << " equals " << nMultiply << endl;

    if (nSecond == 0)
    {
        cout << "Divide by zero is undefined..." << endl;
    }

    else
    {
        cout << nFirst << " divided by " << nSecond << " equals " << nDivide
             << " with a remainder of " << nFirst % nSecond << endl;
    }

}

void CalcIntResults(int nFirst, int nSecond, int &nAdd, int &nSubtract, int &nMultiply, int &nDivide)
{
    nAdd = nFirst + nSecond;
    nSubtract = nFirst - nSecond;
    nMultiply = nFirst * nSecond;

    if (nSecond == 0)
    {

    }

    else
    {
        nDivide = nFirst / nSecond;
    }
}


If you'll notice, in order to make everything look 'neat' I leave the if statement within the function blank - is this good practice? Is there any code I can write that basically says "do nothing"?

Also, I use Putty/nano to turn in my homework to my school's server, but at home I use Code::Blocks to compile, and later upload to the server. However, as the program stands right now, Code::Blocks will shut down due to the divide by zero 'error'. However, this isn't an issue at all while using nano (which uses the same compiler as Code::Blocks, g++). Ideas?
If you'll notice, in order to make everything look 'neat' I leave the if statement within the function blank - is this good practice?

I personally find that awkward. I prefer
1
2
if (nSecond)
    nDivide = nFirst / nSecond;

The compiler is actually going to generate the same code for both, so it's a matter of personal style.

I don't see why Code::Blocks should be complaining. You code looks fine. I tried your program with VS2010 and got "Divide by zero is undefined..." as expected.
As you sure you're compiling the right file and not one of your earlier attempts that would have trapped?

Read your instructions again. They say to pass the remainder by reference. You're not doing that.

Topic archived. No new replies allowed.