C ++函数问题=帮助我理解基本概念

时间:2017-01-14 20:18:10

标签: c++

我试图让用户输入两个数字,然后重复他们的总和。现在,当我尝试在main()

中调用函数时,VS 2015无法识别参数

我试图了解基本功能和参数概念化的细分。我到了那里,但我需要看看我的思维过程在哪里崩溃。

#include <iostream>

using namespace std;


int addNumbers(int FirstNumber, int SecondNumber) {
    cout << "enter first number: " << endl;
    cin >> FirstNumber;
    cout << "enter second number: " << endl;
    cin >> SecondNumber;
    int answer = FirstNumber + SecondNumber; 
    cout << answer;
    return answer; 

}

int main() {

    cout << "Test\n";
    cout << addNumbers(FirstNumber, SecondNumber);
    return 0;
}

1 个答案:

答案 0 :(得分:1)

您需要定义您正在使用的变量。而且,您只需要在需要的地方定义它们。例如

#include <iostream>
using namespace std;                      // this is not recommended but I won't 
                                          // change your code completely
int addNumbers()
{
    int FirstNumber, SecondNumber;        // local variables
    cout << "enter first number: " << endl;
    cin >> FirstNumber;
    cout << "enter second number: " << endl;
    cin >> SecondNumber;
    return FirstNumber + SecondNumber;    // compute and return result
}

int main() {    
    cout << "Test\n";
    cout << addNumbers();
    // note: return 0; is not needed.
}