在一个函数中传递多个变量?

时间:2018-11-29 07:40:11

标签: function visual-c++

我是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 . . .*/

2 个答案:

答案 0 :(得分:1)

main()中的num1和num2变量与getData()中的num1和num2是不同的变量。因此,您可以在getData()中设置它们,但是除了显示之外,对它们什么也不做。 main()中的num1和num2不受影响。将这些(作为参考)传递给getData(int&num1,int&num2),不要在getData()本身中声明它们。在“自动”变量声明(在堆栈中声明)中读取。

答案 1 :(得分:0)

//==========================================================
/*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 . . .*/
//==========================================================