Returning a pointer to a structure

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
closed account (j3Rz8vqX)
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?
 
CubScouts *&scouts[]


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
Believe the second option you described, is what im attempting to do.
Edited code posted
Last edited on
closed account (j3Rz8vqX)
1
2
3
4
5
6
7
8
//Function prototype:
void enterCubScouts(CubScouts *&scouts[],int size);

//...

//Function header:
void enterCubScouts(CubScouts *&scouts[],int size)
if i make it void then it cant return anything
closed account (j3Rz8vqX)
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
What else do i need to change



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);
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;
}

void 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;
	
}
closed account (j3Rz8vqX)
I don't know, I am not the programmer here; you are.

What were your intentions =D
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.
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
closed account (j3Rz8vqX)
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
I solved it and got everything formatted.


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
#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);

	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;

		cin.ignore();
		cout << "\n\nDEN NAME: ";
		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 << endl << horizontalLine << endl << endl;
	
}
Topic archived. No new replies allowed.