//// THIS IS JUST A SAMPLE I COOKED UP
/// PLEASE DO NOT CRITIQUE THIS AS IF IT
/// WERE REALLY GOING TO BE USED IN SOMETHING
/// THE REAL CODE IS HIGHLY ULTRA TOP SECRET
#include <iostream>
#include <Windows.h>
usingnamespace std;
void user_input();
int main()
{
user_input();
}
void user_input()
{
int randNum, randNum2;
cout << "Enter random number, you disgusting piece of garbage.\n>";
cin >> randNum;
cout << "Hope you choke on a salad."
<< "Enter another random number, clown face.\n>" ;
cin >> randNum2;
return;
}
I want to return the two variables, randNum and randNum2 to the main block so that I can use them for other things. Wat do?
Your sample is actually good, concise and to the point.
Functions in C++ can only return one value. That is not usually considered as a big inconvenience. There are many ways to get around it, depending on your level of knowledge and other considerations.
Here are some possibilities:
1. make two functions, each one returning a single value
2. return a structure containing multiple values
3. return std::pair or std::tuple
4. use references to return values through function parameters.
struct input_ {
int randNum;
int randNum2;
};
int main()
{
// how to return it?
// user_input(); // does not work.
// input_ user_input(); // builds successfully but does not fire the function, has error:
/* Warning 1
warning C4930: 'input_ user_input(void)': prototyped function not called (was a variable definition intended?)
e:\33visualstudio\array_test\array_test\array_source.cpp 33
*/
}
void user_input() {
...
}