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...
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.
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
}
}
//-----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;
}
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?
usingnamespace 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?