C++ does not allow multiple retruns like you're attempting. There are multiple ways of doing this.
Solution one: passing pointers to values and setting them in read
function:
#include <iostream>
using namespace std;
void read(int* n1, int* n2, int* n3);
void display(int n1, int n2, int n3);
int main()
{
int n1, n2, n3;
read(&n1, &n2, &n3);
display(n1, n2, n3);
//print(n1,n2,n3);
//reverse(n1,n2,n3);
return 0;
}
void read(int* n1, int* n2, int* n3)
{
cout << "Enter three numbers: ";
cin >> *n1;
cin >> *n2;
cin >> *n3;
}
void display(int n1, int n2, int n3)
{
cout << n1 << " " << n2 << " " << n3 << endl;
return;
}
Alternatively, for this same method, you can deal with references instead of pointers, in C++, passing by reference can apply to all types, not just primitive types. It would look like this:
void read(int &n1, int &n2, int &n3)
{
cout << "Enter three numbers: ";
cin >> n1;
cin >> n2;
cin >> n3;
}
The function call it main
would become:
read(n1, n2, n3);
Note that it is crucial the &
is present in read
function signature to make sure you pass by reference and not by value. Passing by value will not reflect on the original passed parameters.
This has the same effect as passing by pointers, but it looks much cleaner and it does not require you use *
.
Solution two: declaring them global within the cpp file. I do not recommend this solution because it's better that functions carry their own data without relying too much on global variables, unless there is no other way. Generally, classes/structs are better for this.
#include<iostream>
void read(void);
void display();
int n1, n2, n3; // declared here
using namespace std;
int main()
{
read();
display();
//print(n1,n2,n3);
//reverse(n1,n2,n3);
return 0;
}
void read(void)
{
cout << "Enter three numbers: ";
cin >> n1;
cin >> n2;
cin >> n3;
}
void display()
{
cout << n1 << " " << n2 << " " << n3 << endl;
return;
}
Solution three: having a data structure to hold the values and dealing with it instead. In this approach, we put n1 n2 n3
as part of a struct and get it from/to functions.
#include<iostream>
struct data{
int n1;
int n2;
int n3;
}
data read(void);
void display(data);
using namespace std;
int main()
{
data d = read();
display(d);
//print(n1,n2,n3);
//reverse(n1,n2,n3);
return 0;
}
data read(void)
{
data d;
cout << "Enter three numbers: ";
cin >> d.n1;
cin >> d.n2;
cin >> d.n3;
return d;
}
void display(data d)
{
cout << d.n1 << " " << d.n2 << " " << d.n3 << endl;
return;
}
As others have suggested in the comments, you can also use std::tuple
to return multiple values, it works similar to how data
struct is used in solution three, and it is the solution I would recommend the most for your case:
#include <iostream>
#include <tuple>
using namespace std;
tuple<int, int, int> read(void);
void display(tuple<int, int, int>);
int main()
{
tuple<int, int, int> d = read();
display(d);
//print(n1,n2,n3);
//reverse(n1,n2,n3);
return 0;
}
tuple<int, int, int> read(void)
{
int n1, n2, n3;
cout << "Enter three numbers: ";
cin >> n1;
cin >> n2;
cin >> n3;
return std::make_tuple(n1, n2, n3);
}
void display(tuple<int, int, int> d)
{
cout << std::get<0>(d) << " " << std::get<1>(d) << " " << std::get<2>(d) << endl;
return;
}