Returning a Struct from a function

Jul 7, 2011 at 12:31am
Okay, we discussed this before but I'm trying to return a struct from a function...(within a header file) Here is the struct:

1
2
3
4
5
struct Pairs
{
int pairCard1; //Numeric value of 1st set of matching variables
int pairCard2; //Numeric value of 2nd set of matching variables
}input[2];


here is the function:

1
2
3
4
5
6
7
8
9
10
11
12
int PairCardReturn(int card1, int card2, int flop1, int flop2, int flop3)
{
	int numbs[5]= {card1, card2, flop1, flop2, flop3}, n = 5, j=1;
	for (int i = 0; (i < n - 1); i++)
        {			
			for (j = i + 1; (j < n) && m<2; j++)
			{
				if (numbs[i] == numbs[j]) return numbs[i]; //First numeric value from 2 matching variables			
			}					
        }	
	return 0;	
}


I need the function above to apply the numeric value of 2 matching variables to the struct Pairs (pairCard1) and and second numeric value from a 2nd set of matching variables to the struct Pairs (pairCard2)...Any ideas would be appreciated...
Jul 7, 2011 at 12:36am
Why are you making two Pairs structs? All you need to do is change the function return type from int to Pairs, and inside the unction make a Pairs object and set the data and return it.
Jul 7, 2011 at 12:56am
Are you saying the function should loook like this?

struct Pairs{
int pairCard1;
int pairCard2;
}input[2];

Pairs ReturnStruct()
{
Pairs input;
int numbs[5]= {1, 2, 1, 2, 4}, n = 5, j=1, m = 0;
for (int i = 0; (i < n - 1); i++)
{
for (j = i + 1; (j < n) && m<2; j++)
{
if (numbs[i] == numbs[j]) input[m] = numbs[i]; //First numeric value from 2 matching variables
}
}

return Pairs;
}


What should I return? Pairs? input?
Jul 7, 2011 at 12:56am
closed account (zwA4jE8b)
just set the return type to whatever struct you need and in your function return it.
Jul 7, 2011 at 1:02am
Here is the code in its entirety, could you tell me what's wrong...?

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
//-----Function
struct Pairs{
int pairCard1;
int pairCard2;
}input[2];

Pairs ReturnStruct()
{
	Pairs input;
        int numbs[5]= {1, 2, 1, 2, 4}, n = 5, j=1, m = 0;
        for (int i = 0; (i < n - 1); i++)
        {                       
                        for (j = i + 1; (j < n) && m<2; j++)
                        {
                                if (numbs[i] == numbs[j]) input[m] = numbs[i]; //First numeric value from 2 matching variables                      
                        }                                       
        }       

}

//----------Main Program

int main()
{
	ReturnStruct();
	return 0;
}
Jul 7, 2011 at 1:31am
closed account (zwA4jE8b)
you are not returning anything. only void functions do not return.

return 'your struct'; //at the end of the function

Also, you never increase m. so input[m] will always be on index 0.
Last edited on Jul 7, 2011 at 1:34am
Jul 7, 2011 at 1:32am
If you're going to do it like that you might as well put all the code from ReturnStruct() into main() and not even use a function in the first place.
Last edited on Jul 7, 2011 at 1:32am
Jul 7, 2011 at 1:33am
okay, understood! so how do I access the information from the main program..? If you add the 'return input' code to the ReturnStruct function, the program does nothing...how do I access the struct from the main program?
Jul 7, 2011 at 1:34am
By assigning it to a struct in the main program...?
Jul 7, 2011 at 1:35am
closed account (zwA4jE8b)
when you set up your function to return a struct then in main you just declare a struct and assign it to your function.
Jul 7, 2011 at 1:42am
closed account (zwA4jE8b)
example of assigning a function to a variable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int intfunction()
{
   return 3;
}

int main()
{
   int a = intfunction();
   std::cout << a;
   return 0;
}
Last edited on Jul 7, 2011 at 1:44am
Jul 7, 2011 at 1:54am
for the sake of eliminating problems, I put all the code into the main program...it looks like this:

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
using namespace std;

struct Pairs{
int pairCard1;
int pairCard2;
};

struct pairCard{
int pairCard1;
int pairCard2;
};


pairCard ReturnStruct(struct Pairs mystruct);
int main()
{
	
	Pairs input;
	pairCard output;
	output = ReturnStruct(input);
	
	return 0;
}

pairCard ReturnStruct()
{
	pairCard answer;
	answer.pairCard1 = 3;
	answer.pairCard2 = 4;
	
	return answer;
}


This code returns an error: conversion from 'Pairs" to non-scalar type 'PairCard' requested...Is this error caused because I am asking the program to return two values?
Last edited on Jul 7, 2011 at 2:00am
Jul 7, 2011 at 1:59am
closed account (zwA4jE8b)
you do not need two structs that contain the same data...

in main

Pairs input = ReturnStruct();

thats it. you declare input as a datatype Pairs and assign it to be of that type.

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
using namespace std;

struct Pairs{
int pairCard1;
int pairCard2;
};

Pairs ReturnStruct();

int main()
{
	
	Pairs inmain = ReturnStruct();
              cin.get();  //To hold the console open
	return 0;
}

Pairs ReturnStruct()
{
	Pairs infunc;
	infunc.pairCard1 = 3;
	infunc.pairCard2 = 4;
	
	return infunc;
}


*fixed.
Last edited on Jul 7, 2011 at 2:02am
Jul 7, 2011 at 2:08am
Got it thanks a lot to all!
Last edited on Jul 7, 2011 at 2:15am
Jul 7, 2011 at 2:14am
closed account (zwA4jE8b)
try the fixed code. I edited my last post.


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
#include <iostream>

using namespace std;

struct Pairs{
int pairCard1;
int pairCard2;
};

Pairs ReturnStruct();

int main()
{
	
	Pairs inmain = ReturnStruct();
	cout << inmain.pairCard1 << " " << inmain.pairCard2;
    cin.get();  //To hold the console open
	return 0;
}

Pairs ReturnStruct()
{
	Pairs infunc;
	infunc.pairCard1 = 3;
	infunc.pairCard2 = 4;
	
	return infunc;
}


Jul 7, 2011 at 2:17am
I added ReturnStruct after struct Pairs declaration...same diff thanks!

1
2
3
4
struct Pairs{
int pairCard1;
int pairCard2;
}ReturnStruct();
Jul 7, 2011 at 2:59am
I put the Pairs ReturnStruct() function in a header file but when I run the program, now, parse and syntax errors occur...any ideas why?
Topic archived. No new replies allowed.