I am having trouble with a very simple program. I need to call in a method (getNumber) outside of main that takes two numbers from the user and then store those numbers. Those two numbers are then used in a calculation method (math) which is also called into main. I am getting an uninitialized local variable for my two numbers I am calling in from getNumber. I would like the user to enter two numbers have them added together and display the result but by calling in methods.
#include <iostream>
#include <string>
using namespace std;
int getNumber(int x, int y)
{
// here is where the user is prompted to input two numbers
cout << "Please enter two values" << endl;
cin >> x >> y;
return x, y;
}
int math(int x , int y) // here is where the calculations are done
{
int result;
result = x + y;
return result;
}
int main()
{
int x;
int y;
int result;
x = getNumber(x, y); // trying to call in the input method here
result=math(x,y); // calling in claculation method
cout << result;
system("pause");
return 0;
}
void getNumber(int &x, int &y)
{
cout << "Please enter two values" << endl;
cin >> x >> y;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.