Quotient Void

So this is our assignment:

Write the C++ code for a void function that receives three double variables: the first two by
value and the last one by reference. Name the formal parameters n1, n2, and answer. The
function should divide the n1 variable by the n2 variable and then store the result in the
answer variable. Name the function calcQuotient. Also write an appropriate function
prototype for the calcQuotient function. In addition, write a statement that invokes the
calcQuotient function in the main function, passing it the num1, num2, and
quotient variables

And this is the code that I have so far:
#include <iostream>

using namespace std;

void calcQuotient(int n1, int n2, int quo);

int main()
{
int num1 = 0;
int num2 = 0;
int quotient = 0;

cout << "Please enter the first number: ";
cin >> num1;
cout << "Please enter the second number: ";
cin >> num2;



calcQuotient(num1, num2, quotient);

cout << "The quotient of the two numbers is: " << quotient;

system("pause");
return 0;
}

void calcQuotient(int n1, int n2, int quo)
{
quo = n1/n2;
}

But the answer I keep getting is 0. How can I be able to fix this?
The instructions say that the function arguments should be doubles (not ints) and that the first two should be "by value" and the last one "by reference". So the prototype would look like this:
 
void calcQuotient(double a, double b, double &c);

The ampersand before the third parameter makes it a "reference" parameter. That means that the value of the original variable in the calling function can be modified from within the called function. If it isn't defined as a reference parameter then any changes you make to it will not be reflected in the passed variable.

The function body should be pretty much as you have it.

Remember to use code tags to post your code in the future: http://www.cplusplus.com/forum/articles/16853/
Last edited on
pass a pointer to quo, not quo itself.

or

have the function return quo as a value, and take only two arguments. (eta: but thats not how they said to do it. oh well.)
Last edited on
Topic archived. No new replies allowed.