Function 在函数中传递多个变量? 我是一个第一年的CS学生,试图更好地理解C++中的函数,因为我现在在这方面很薄弱。我试图创建一个程序,要求用户输入两个整数,然后将它们传递给一个计算函数,最后传递给一个显示函数来显示计算结果。现在这里是我的代码,输出在底部。我不太清楚为什么num1和num2不能正确地传递给计算函数?任何帮助都是感激的,请不要理会它的风格,我通常会在它工作后尝试清理它 #include <iostream> #include <cmath> #include <iomanip> using namespace std; void getData(); void doTheMath(int num1, int num2); void displayResults(int num1, int num2, int& sum, int& diff, int& prod, int& quot, int& rem); int main() { int num1; int num2; int sum; int diff; int prod; int quot; int rem; getData(); doTheMath(num1, num2); displayResults(num1, num2, sum, diff, prod, quot, rem); system("pause"); return 0; } void getData() { int num1; int num2; cout << "Please enter two integer values:\n"; cin >> num1; cin >> num2; cout << "The first number is " << num1 << " and the second is "<< num2 << "\n\n"; } void doTheMath(int num1, int num2) { int sum = num1 + num2; int diff = num1 - num2; int prod = num1 * num2; int quot = num1 / num2; int rem = num1 % num2; } void displayResults(int num1, int num2, int& sum, int& diff, int& prod, int& quot, int& rem) { if (num2 == 0) { cout << "Here are the results:\n\n"; cout << "The sum of " << num1 << " and " << num2 << " is " << sum << ".\n"; cout << "The difference, (" << num1 << " minus " << num2 << ") is " << diff << ".\n"; cout << "The product of " << num1 << " and " << num2 << " is " << prod << ".\n"; cout << "Cannot divide by zero.\n\n"; } else { cout << "Here are the results:\n\n"; cout << "The sum of " << num1 << " and " << num2 << " is " << sum << ".\n"; cout << "The difference, (" << num1 << " minus " << num2 << ") is " << diff << ".\n"; cout << "The product of " << num1 << " and " << num2 << " is " << prod << ".\n"; cout << num1 << " divided by " << num2 << " is " << quot << " with a remainder of " << rem << ".\n\n"; } } //Output /*Please enter two integer values: 12 0 The first number is 12 and the second is 0 Here are the results: The sum of -858993460 and -858993460 is -858993460. The difference, (-858993460 minus -858993460) is -858993460. The product of -858993460 and -858993460 is -858993460. -858993460 divided by -858993460 is -858993460 with a remainder of -858993460. Press any key to continue . . .*/

Function 在函数中传递多个变量? 我是一个第一年的CS学生,试图更好地理解C++中的函数,因为我现在在这方面很薄弱。我试图创建一个程序,要求用户输入两个整数,然后将它们传递给一个计算函数,最后传递给一个显示函数来显示计算结果。现在这里是我的代码,输出在底部。我不太清楚为什么num1和num2不能正确地传递给计算函数?任何帮助都是感激的,请不要理会它的风格,我通常会在它工作后尝试清理它 #include <iostream> #include <cmath> #include <iomanip> using namespace std; void getData(); void doTheMath(int num1, int num2); void displayResults(int num1, int num2, int& sum, int& diff, int& prod, int& quot, int& rem); int main() { int num1; int num2; int sum; int diff; int prod; int quot; int rem; getData(); doTheMath(num1, num2); displayResults(num1, num2, sum, diff, prod, quot, rem); system("pause"); return 0; } void getData() { int num1; int num2; cout << "Please enter two integer values:\n"; cin >> num1; cin >> num2; cout << "The first number is " << num1 << " and the second is "<< num2 << "\n\n"; } void doTheMath(int num1, int num2) { int sum = num1 + num2; int diff = num1 - num2; int prod = num1 * num2; int quot = num1 / num2; int rem = num1 % num2; } void displayResults(int num1, int num2, int& sum, int& diff, int& prod, int& quot, int& rem) { if (num2 == 0) { cout << "Here are the results:\n\n"; cout << "The sum of " << num1 << " and " << num2 << " is " << sum << ".\n"; cout << "The difference, (" << num1 << " minus " << num2 << ") is " << diff << ".\n"; cout << "The product of " << num1 << " and " << num2 << " is " << prod << ".\n"; cout << "Cannot divide by zero.\n\n"; } else { cout << "Here are the results:\n\n"; cout << "The sum of " << num1 << " and " << num2 << " is " << sum << ".\n"; cout << "The difference, (" << num1 << " minus " << num2 << ") is " << diff << ".\n"; cout << "The product of " << num1 << " and " << num2 << " is " << prod << ".\n"; cout << num1 << " divided by " << num2 << " is " << quot << " with a remainder of " << rem << ".\n\n"; } } //Output /*Please enter two integer values: 12 0 The first number is 12 and the second is 0 Here are the results: The sum of -858993460 and -858993460 is -858993460. The difference, (-858993460 minus -858993460) is -858993460. The product of -858993460 and -858993460 is -858993460. -858993460 divided by -858993460 is -858993460 with a remainder of -858993460. Press any key to continue . . .*/,function,visual-c++,Function,Visual C++,main中的num1和num2变量与getData中的num1和num2变量不同。所以您在getData中设置了这些,但除了显示之外,什么也不做。主管道中的num1和num2不受影响。将它们作为对getDataint&num1、int&num2的引用传递,不要在getData本身中声明它们。在堆栈上声明的“auto”变量声明中读取 我知道它没有被正确地传递,因为我在doTheMath函数中使用了cout语句来调用num1和num2,结果是num1=-858993460和num2=-8589934

main中的num1和num2变量与getData中的num1和num2变量不同。所以您在getData中设置了这些,但除了显示之外,什么也不做。主管道中的num1和num2不受影响。将它们作为对getDataint&num1、int&num2的引用传递,不要在getData本身中声明它们。在堆栈上声明的“auto”变量声明中读取

我知道它没有被正确地传递,因为我在doTheMath函数中使用了cout语句来调用num1和num2,结果是num1=-858993460和num2=-858993460。另外,我需要将我的两个整数作为值传递,而不是作为引用,但是我得到了一个未初始化的局部变量错误。-858993460是一个幻数,十六进制0xCCCCCCC。这意味着您正在使用未初始化的变量。是的,你做到了,你需要解决这个问题。你已经知道如何传递多个变量了,displayResult就是这样做的。谢谢你,我现在又读了一遍,来唤起我的记忆。好的,我开始工作了,但还有一个简单的问题。如果我想把num1和num2作为一个值而不是一个引用来传递,我将如何进行呢?我需要保持getData函数在功能上是空的,我想我已经解决了。谢谢你的帮助
//==========================================================
/*Description:
    This program is to showcase my understanding of
    functions that I learned from our Lecture 7b. The user
    is prompted to enter two integer values, where they
    are then passed to a calculation function to calculate
    the sum, difference, product, quotient, and remainder
    of the two numbers entered. After all the values are
    calculated, they are showcased in a display function
    to the user in the output stream.*/
//==========================================================
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

void getData(int& num1, int& num2);
void doTheMath(int num1, int num2, int& sum, int& diff, int& prod, int& quot, int& rem);
void displayResults(int num1, int num2, int sum, int diff, int prod, int quot, int rem);
//====================== main ===========================
//
//=======================================================
int main()
{
    int num1;
    int num2;
    int sum;
    int diff;
    int prod;
    int quot;
    int rem;

    //Gets two integers from user
    getData(num1, num2);
    //Does the calculation from integers received
    doTheMath(num1, num2, sum, diff, prod, quot, rem);
    //Displays calculated results from two integers
    displayResults(num1, num2, sum, diff, prod, quot, rem);
    system("pause");
    return 0;
}



/*===================== getData ==========================
This function gets the information from the user of the
two integers they wish to input. It assigns the user's
numbers to num1 and num2.

Input:
    num1 - First integer assigned by user
    num2 - Second integer assigned by user
Output:
    The values being assigned to be used in the doTheMath
    function.*/
//========================================================
void getData(int& num1, int& num2)
{
    cout << "Please enter two integer values:\n";
    cin >> num1;
    cin >> num2;
}



/*==================== doTheMath =========================
This function calculates the user's two integers inputted 
into the previous function and assigns the calculated 
answers to variables named by the calculation performed.
It first checks to see if num2 is 0, because this system 
can't divide by zero without crashing.

Input:
    sum - adds the two integers
    diff - subtracts the two integers
    prod - multiplies the two integers
    quot - divides the two integers
    rem - gets the remainder of the two integers
Output:
    Variables are now assigned new values to be displayed
    inside of the displayResults function.*/
//========================================================
void doTheMath(int num1, int num2, int& sum, int& diff, int& prod, int& quot, int& rem)
{
    if (num2 == 0)
    {
        sum = (num1 + num2);
        diff = (num1 - num2);
        prod = (num1 * num2);
    }
    else
    {
        sum = (num1 + num2);
        diff = (num1 - num2);
        prod = (num1 * num2);
        quot = (num1 / num2);
        rem = (num1 % num2);
    }
}



/*================= displayResults ======================
This function takes the calculations from the doTheMath
function and displays them to the user in a standard 
output stream. It first checks to see if num2 is 0, 
because this system can't divide by zero without
crashing.

Input:
    Calculations from the doTheMath function, as well as
    num1 and num2.
    (sum, diff, prod, quot, rem).
Output:
    Displays the calculations from the doTheMath function
    to the user in a standard output stream.*/
//========================================================
void displayResults(int num1, int num2, int sum, int diff, int prod, int quot, int rem)
{
    if (num2 == 0)
    {
        cout << "Here are the results:\n\n";
        cout << "The sum of " << num1 << " and " << num2
            << " is " << sum << ".\n";
        cout << "The difference, (" << num1 << " minus "
            << num2 << ") is " << diff << ".\n";
        cout << "The product of " << num1 << " and "
            << num2 << " is " << prod << ".\n";
        cout << "Cannot divide by zero.\n\n";
    }
    else
    {
        cout << "Here are the results:\n\n";
        cout << "The sum of " << num1 << " and " << num2
            << " is " << sum << ".\n";
        cout << "The difference, (" << num1 << " minus "
            << num2 << ") is " << diff << ".\n";
        cout << "The product of " << num1 << " and "
            << num2 << " is " << prod << ".\n";
        cout << num1 << " divided by " << num2 << " is "
            << quot << " with a remainder of " << rem
            << ".\n\n";
    }

}
//==========================================================
/*OUTPUT (When num2 != 0):
Please enter two integer values:
12
3
Here are the results:

The sum of 12 and 3 is 15.
The difference, (12 minus 3) is 9.
The product of 12 and 3 is 36.
12 divided by 3 is 4 with a remainder of 0.

Press any key to continue . . .*/
//==========================================================

//==========================================================
/*OUTPUT (When num2 == 0):
Please enter two integer values:
12
0
Here are the results:

The sum of 12 and 0 is 12.
The difference, (12 minus 0) is 12.
The product of 12 and 0 is 0.
Cannot divide by zero.

Press any key to continue . . .*/
//==========================================================