通过引用问题C ++传递

时间:2013-10-31 14:22:23

标签: c++ pass-by-reference overloading

有人可以帮忙解释为什么当我将2个数字相乘时它返回0,但它有3个和4个数字吗?

我为一堂课写了这篇文章,但我不知道它有什么不对,谢谢。

功能是使用重载函数乘以2,3或4个数字并通过引用传递产品。

#include <iostream>
using namespace std;

void mult(int& product, int n1, int n2);
void mult(int& product, int n1, int n2, int n3);
void mult(int& product, int n1, int n2, int n3, int n4);

int main() {
    int product, n1, n2, n3, n4;
    char ans;

    do {
        product = 0;
        n1 = 0;
        n2 = 0;
        n3 = 0;
        n4 = 0;
        cout << "Enter 2-4 numbers to multiply\n";
        cout << "First number: ";
        cin >> n1;
        cout << "Second number: ";
        cin >> n2;
        cout << "Enter a 3rd number? (y/n):";
        cin >> ans;

        if (ans == 'y') {
            cout << "Third number: ";
            cin >> n3;
            cout << "Enter a 4th number? (y/n):";
            cin >> ans;
        } else {
            mult(product, n1, n2);
        }

        if (ans == 'y') {
            cout << "Fourth number: ";
            cin >> n4;
            mult(product, n1, n2, n3, n4);
        } else {
            mult(product, n1, n2, n3);
        }

        cout << "The product is " << product << endl << n1 << n2 << n3 << n4;
        cout << "Would you like to calculate another? (y/n):";
        cin >> ans;

    } while (ans == 'y');
}

解释

void mult(int& product, int n1, int n2)
{
    product = (n1 * n2);
    cout << product;
}
void mult(int& product, int n1, int n2, int n3)
{
    product = (n1 * n2 * n3);
}
void mult(int& product, int n1, int n2, int n3, int n4)
{
    product = (n1 * n2 * n3 * n4);
}

2 个答案:

答案 0 :(得分:5)

这是因为你的控制结构执行语句

else{mult(product,n1,n2,n3);}

即使您只打算使用mult(product,n1,n2)。只有两个数字,n3将为0.因此结果也为零。

你可以通过重组它来解决它:

   cout << "Enter a 3rd number? (y/n):";
    cin >> ans;

    if (ans == 'y') {
        cout << "Third number: ";
        cin >> n3;
        cout << "Enter a 4th number? (y/n):";
        cin >> ans;
        if (ans == 'y') {
            cout << "Fourth number: ";
            cin >> n4;
            mult(product, n1, n2, n3, n4);
        } else {
            // Three numbers
            mult(product, n1, n2, n3);
        }
    } else {
        // Two numbers
        mult(product, n1, n2);
    }

答案 1 :(得分:2)

您的条件设置不正确:

if (ans == 'y') 
{
        cout << "Third number: ";
        cin >> n3;
        cout << "Enter a 4th number? (y/n):";
        cin >> ans;
}
else
{
    mult(product, n1, n2); // product is set here correctly for 2 numbers
}

if (ans == 'y') // ans is STILL 'n' here
{
    cout << "Fourth number: ";
    cin >> n4;
    mult(product, n1, n2, n3, n4);  
}
else
{
    mult(product, n1, n2, n3); // this overwrites the correct product with 0 because n3 is 0
}

我认为这是一次学术练习,所以更简单的方法可能如下:

int main()
{
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    int product = 0;

    unsigned short cnt = 0;

    do
    {
        std::cout << "Enter the number of operands (2-4):  ";
        if (!(std::cin >> cnt))
        {
            std::cin.clear();
            std::cout << "Invalid number of operands!" << std::endl;
        }

    } while (cnt < 2 || cnt > 4); 

    std::cout << "Please enter your operands:  ";

    switch (cnt)
    {
    case 2:
        {
            std::cin >> a >> b; // error checking left out for simplicity
            mult(product, a, b);
            break;
        }
    case 3:
        {
            std::cin >> a >> b >> c;
            mult(product, a, b, c);
            break;
        }
    case 4:
        {
            std::cin >> a >> b >> c >> d;
            mult(product, a, b, c, d);
            break;
        }
    }

    std::cout << "Product is " << product << std::endl;      

    return 0;
}