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);
}