Returning multiple variables in 1 function to main()
Oct 22, 2013 at 4:30am UTC
I am trying to return 2 numbers from my function to main(). They are both read in from an input file but it is not working out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <fstream>
#include <iostream>
using namespace std;
ofstream outfile;
void heading(int );
int stuid(int ,int );
int main()
{
int x,y,h;
heading(h);
stuid(x,y);
cout << x << endl;
system("PAUSE" );
return 0;
}
void heading(int ) // Table Heading
{
outfile.open("Table" );
outfile << "\t\t\t\t\tStudent Information" << endl << endl;
outfile << "Student ID\t\tGrade\t\tAverage\t\tLetter Grade" << endl<< endl;
}
int stuid(int a, int b) // Student ID
{
int x,y;
ifstream infile("grades.txt" );
infile >> x;
infile >> y;
return x,y;
}
Oct 22, 2013 at 4:42am UTC
Only one value can be returned using "return".
Oct 22, 2013 at 4:45am UTC
Option one: pass parameters by reference
Option two: return a
std::pair of integers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include <iostream>
#include <utility>
// pass by reference
void foo( int & a, int & b )
{
a = 100 ;
b = 234 ;
}
// return pair by value
std::pair<int ,int > bar()
{
int a = 500 ;
int b = 678 ;
return std::make_pair( a, b ) ;
}
int main()
{
int i, j ;
foo(i,j) ;
std::cout << i << ' ' << j << '\n' ;
std::pair<int ,int > result = bar() ;
const int k = result.first ;
const int l = result.second ;
std::cout << k << ' ' << l << '\n' ;
}
Oct 22, 2013 at 9:32am UTC
Create a custom struct
with meaningful members and return this struct. This is similar to returning a std::pair
but imho more readable.
Oct 22, 2013 at 12:36pm UTC
Returning multiple values are what tuples are for:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include <fstream>
#include <iostream>
#include <tuple>
using namespace std;
void heading(int ) // Table Heading
{
ofstream outfile("Table.txt" );
outfile << "\t\t\t\t\tStudent Information" << endl << endl;
outfile << "Student ID\t\tGrade\t\tAverage\t\tLetter Grade" << endl<< endl;
}
tuple<int ,int > stuid() // Student ID
{
int x,y;
ifstream infile("grades.txt" );
infile >> x;
infile >> y;
return make_tuple(x,y);
}
int main()
{
int x,y,h;
heading(h);
tie(x,y) = stuid();
cout << x << endl;
system("PAUSE" );
return 0;
}
The more traditional method is via references:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include <fstream>
#include <iostream>
using namespace std;
void heading(int ) // Table Heading
{
ofstream outfile("Table.txt" );
outfile << "\t\t\t\t\tStudent Information" << endl << endl;
outfile << "Student ID\t\tGrade\t\tAverage\t\tLetter Grade" << endl<< endl;
}
void stuid(int & a, int & b) // Student ID
{
ifstream infile("grades.txt" );
infile >> a;
infile >> b;
}
int main()
{
int x,y,h;
heading(h);
stuid(x,y);
cout << x << endl;
system("PAUSE" );
return 0;
}
I am still amazed that people are taught to put
main() at the beginning of the file instead of at the end, where it belongs (IMNSHO), so I swapped things around a little. Much less confusing that way.
Notice also that you don't need that global variable.
Lastly, your variable names are not helpful. Try to come up with something that tells you what they are for.
Hope this helps.
Topic archived. No new replies allowed.