Function returning two user input values

I would like to be able to have a function that simply receives two input values ​​from the user and returns these values ​​for the main function to work.

I would like the values ​​a and b to be found only in the gradation function and passed to the main function as x and y.

I think that I am probably going to do something wrong, since I searched a lot and cannot find similar ways to do this, but any help would be appreciated.

#include <iostream> using namespace std; int x = 100; int y = 42; int result1; int result2; int a; int b; int getvals(int,int) { cout << "input value a "; cin >> a; cout << "input value b "; cin >> b; return a,b; } int main() { getvals(x,y); result1 = x + y; cout << "\n\n"; cout << " x + y = " << result1; return 0; } 
+4
source share
5 answers

Use the links for a and b .

 void getvals(int &a, int &b) { cout << "input value a "; cin >> a; cout << "input value b "; cin >> b; } 

This declares getvals() to accept two control parameters. Modifying an object reference changes the object that was passed to the function call.

Without a reference, the parameter is passed by value, which creates a copy of the object passed to the function. Then the changes made to the parameter in the function affect only the copy.

Alternatively, you can use std::pair<int, int> to return two integer values ​​from your function (then you will not need external parameters). You can manually unpack the first and second elements into your x and y variables, or you can implement a helper class for this. For instance:

 std::pair<int, int> getvals () { std::pair<int, int> p; std::cin >> p.first; std::cin >> p.second; return p; } template <typename T, typename U> struct std_pair_receiver { T &first; U &second; std_pair_receiver (T &a, U &b) : first(a), second(b) {} std::pair<T, U> operator = (std::pair<T, U> p) { first = p.first; second = p.second; return p; } }; template <typename T, typename U> std_pair_receiver<T, U> receive_pair (T &a, U &b) { return std_pair_receiver<T, U>(a, b); } int main () { int x, y; receive_pair(x, y) = getvals(); //... } 

If you have C ++ 11, you can use the more general tuple and tie helper to do it in a similar way in a cleaner way. This is illustrated in Benjamin Lindley's answer.

+4
source

You can return only one value from a function. Fortunately, you can wrap two values ​​in a structure or class and return it as a single object. That is what std::pair is for.

 std::pair<int,int> getvals() { std::pair<int,int> p; cout << "input value a "; cin >> p.first; cout << "input value b "; cin >> p.second; return p; } int main() { std::pair<int,int> p = getvals(); int result1 = p.first + p.second; ... } 

C ++ 11 introduces a more general std::tuple , which allows an arbitrary number of elements.

 std::tuple<int,int> getvals() { int a,b; cout << "input value a "; cin >> a; cout << "input value b "; cin >> b; return std::make_tuple(a,b); } int main() { int x,y; std::tie(x,y) = getvals(); ... } 
+8
source

You seem to be halfway to returning through options. All you need to change is this:

 void getvals( int& a, int& b ) { cout << "input value a "; cin >> a; cout << "input value b "; cin >> b; } 

Pay attention to & in front of parameter names, that is, pass by reference. This means that when they change in a function, they also change in the caller. No return .

+2
source

Pass the values ​​by reference to your function and change its definitions to return void. Something like that:

 void getvals(int &a,int &b) { cout << "input value a "; cin >> a; cout << "input value b "; cin >> b; return; } 
+1
source

You can return only one thing from a function. This thing may be an array, which may be appropriate in some cases.

Most often, Right Thing (TM) will declare variables in your calling function. Pass references to these variables to the function that should set them. Using the links you passed in the functions, you can set the variables in the caller very much like returning them. If you go along this route, it is probably a good idea for the function to return success / failure and process all the data output via the links.

Good luck. I pull to you. We are all in this together.

0
source

Source: https://habr.com/ru/post/1497337/


All Articles