Apr 30, 2014 at 1:53am UTC
Can someone help me fix this program, it need to return a pointer from this function, but i dont understand how to do so.
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#include <iostream>
using namespace std;
//Cub Scout Info Variable
struct CubScouts
{
string name;
int schoolGrade;
string denName;
};
//Function Prototype
CubScouts *enterCubScouts(CubScouts *scouts[], int );
void printCubScouts(CubScouts scouts[], int );
int main()
{
int numScouts;
cout << "\n\nHow many cub scouts are in your pack?\n" ;
cin >> numScouts;
cin.ignore();
CubScouts scouts[numScouts];
//Call Functions
enterCubScouts(scouts, numScouts);
//CubScouts *ptr = new cubscouts *[numScouts]
printCubScouts(scouts, numScouts);
return 0;
}
CubScouts *enterCubScouts(CubScouts *&scouts[],int size)
{
for (int x=0; x<size; x++)
{
cout << "\nCUB SCOUT " << x+1 << ": \n" ;
cout << "NAME: " ;
getline(cin, scouts[x].name);
cout << "\n\nGRADE (1-5): " ;
cin >> scouts[x].schoolGrade;
cout << "\n\nDEN NAME: " ;
cin.ignore();
getline(cin, scouts[x].denName);
cin.sync();
}
return *scouts;
}
void printCubScouts(CubScouts scouts[],int size)
{
string horizontalLine(65, '-' );
cout << horizontalLine << "\n\n\n" ;
for (int x=0; x<size; x++)
{
cout << "\nCUB SCOUT " << x+1 << ": \n" ;
cout << "NAME: " ;
cout << scouts[x].name;
cout << "\n\nGRADE (1-5): " ;
cout << scouts[x].schoolGrade;
cout << "\n\nDEN NAME: " ;
cout << scouts[x].denName;
cout << endl << endl;
}
cout << horizontalLine << endl << endl;
}
Last edited on Apr 30, 2014 at 2:03am UTC
Apr 30, 2014 at 1:59am UTC
You're returning the value of scout(the whole array). ("*" causes the pointer object to be dereferenced).
Simply return:
return scouts;//This is return the pointer object.
Or did you want to pass the pointed table by reference into the function? Making changes correlate without have to return the pointer to function?
If the first then you will have to catch the return pointer in the main function:
scouts = enterCubScouts(scouts, numScouts);//Something like this
Have a good day.
Last edited on Apr 30, 2014 at 2:00am UTC
Apr 30, 2014 at 2:01am UTC
Believe the second option you described, is what im attempting to do.
Edited code posted
Last edited on Apr 30, 2014 at 2:03am UTC
Apr 30, 2014 at 2:04am UTC
if i make it void then it cant return anything
Apr 30, 2014 at 2:06am UTC
Scout was passed by reference as a pointer type array.
Any changes that were made will correspond to that of the main functions pointer-array.
You won't need to return a pointer to the array.
Hint: "&" == passed by reference.
If your intentions are the first then simply leave the code the same as before and return scout;//This will return the pointer to this array
Last edited on Apr 30, 2014 at 2:09am UTC
Apr 30, 2014 at 2:10am UTC
I don't know, I am not the programmer here; you are.
What were your intentions =D
Apr 30, 2014 at 2:14am UTC
To make it work? "which will accept the pointer to the allocated array and the number of cub scouts." and that's what its apparently for.
Apr 30, 2014 at 2:14am UTC
1 2 3 4 5
CubScouts scouts[numScouts]; // error : variable length array
// ...
CubScouts *ptr = new cubscouts *[numScouts] // ptr should be **ptr (pointer to pointer)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// ...
int main()
{
int numScouts;
cout << "\n\nHow many cub scouts are in your pack?\n" ;
cin >> numScouts;
cin.ignore();
CubScouts *ptr = new CubScouts[ numScouts ];
delete [] ptr;
// ...
}
CubScouts *enterCubScouts(CubScouts *scouts, int size)
{
// ...
return scouts; // why do you need to do 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 33 34 35 36 37 38
int main()
{
int numScouts;
cout << "\n\nHow many cub scouts are in your pack?\n" ;
cin >> numScouts;
cin.ignore();
CubScouts **ptr = new CubScouts*[ numScouts ];
for ( size_t i = 0; i < numScouts; ++i )
CubScouts[i] = new CubScouts;
// ...
for ( size_t i = 0; i < numScouts; ++i )
delete CubScouts[i];
delete [] ptr;
// ...
}
CubScouts *enterCubScouts(CubScouts *scouts[], int size)
{
// ...
for (int x=0; x<size; x++)
{
cout << "\nCUB SCOUT " << x+1 << ": \n" ;
cout << "NAME: " ;
getline(cin, scouts[x]->name);
// ...
}
return *scouts; // why do you need to do this ?
}
Last edited on Apr 30, 2014 at 2:17am UTC
Apr 30, 2014 at 2:17am UTC
try:
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#include <iostream>
using namespace std;
//Cub Scout Info Variable
struct CubScouts
{
string name;
int schoolGrade;
string denName;
};
//Function Prototype
void enterCubScouts(CubScouts *&scouts, int size);//<<--- modified
void printCubScouts(CubScouts scouts[], int );
int main()
{
int numScouts;
cout << "\n\nHow many cub scouts are in your pack?\n" ;
cin >> numScouts;
cin.ignore();
CubScouts scouts[numScouts];
CubScouts *ptr = scouts;//<<--- modified
//Call Functions
enterCubScouts(ptr, numScouts);//<<--- modified
//CubScouts *ptr = new cubscouts *[numScouts]
printCubScouts(scouts, numScouts);
return 0;
}
void enterCubScouts(CubScouts *&scouts, int size)//<<--- modified
{
for (int x=0; x<size; x++)
{
cout << "\nCUB SCOUT " << x+1 << ": \n" ;
cout << "NAME: " ;
getline(cin, scouts[x].name);
cout << "\n\nGRADE (1-5): " ;
cin >> scouts[x].schoolGrade;
cout << "\n\nDEN NAME: " ;
cin.ignore();
getline(cin, scouts[x].denName);
cin.sync();
}
//return *scouts;
}
void printCubScouts(CubScouts scouts[],int size)
{
string horizontalLine(65, '-' );
cout << horizontalLine << "\n\n\n" ;
for (int x=0; x<size; x++)
{
cout << "\nCUB SCOUT " << x+1 << ": \n" ;
cout << "NAME: " ;
cout << scouts[x].name;
cout << "\n\nGRADE (1-5): " ;
cout << scouts[x].schoolGrade;
cout << "\n\nDEN NAME: " ;
cout << scouts[x].denName;
cout << endl << endl;
}
cout << horizontalLine << endl << endl;
}
I'm unsure of the behavior and needs of the program; simply trying to resolve your function pass/call.
Last edited on Apr 30, 2014 at 2:20am UTC