Structures and dynamic allocation

I'm currently stuck on a homework problem. The questions asks "Assuming you have created the Speaker struct described in the accompanying question, write a getSpeaker function that creates a dynamically-allocated Speaker pointer, fills it's member variables with user input, and returns the pointer to the calling function.".

I have already did the part for the accompanying question correctly.
I'm just confused on what I'm doing wrong for the function and function call itself. Any help would be appreciated.

Function call starts on line 43 and the function itself is on line 56.

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

using namespace std;

//struct
struct Speaker
{
	string name;
	string phoneNumber;
	string email;
	string theme;
	double fee;
};

//prototype
*int getSpeaker();
/*************************************
* Function: main
* Description: 
**************************************/
int main()
{
	//declare speaker variable
	Speaker s1;

	//fill struct information
	s1.name = "Alex";
	s1.phoneNumber = "(425)123-4567";
	s1.email = "internetMail@gmail.com";
	s1.theme = "mysterious";
	s1.fee = 20.95;

	//output results of struct
	cout << "The speaker is " << s1.name << ". " << "Contact information is " 
		<< s1.phoneNumber << " and " << endl;

	cout << s1.email << ". " << "The theme is " 
		<< s1.theme << " and the fee is $" << s1.fee << "." << endl;
	cout << endl;

	//second part of program
	cout << getSpeaker(s1) << endl;

	//end without error
	return 0;
}

/*************************************
* Function: 
* Description: 
* Input: 
* Output: 
**************************************/

*int getSpeaker(Speaker s)
{
	Speaker *sPtr;
	sPtr = new Speaker;
	sPtr->name = "John"; 
	sPtr->phoneNumber = "(425)111-2222";
	sPtr->email = "test@gmail.com";
	sPtr->theme = "spooky";
	sPtr->fee = 10.99;

	return *sPtr;

	delete sPtr;
}
Last edited on
1) A pointer to an int is int* instead of *int
2) getSpeaker returns a Speaker struct not a int*
3) cout doesn't have an overloaded operator that takes in a random structure (like Speaker) at least if that's what you're trying to print. You either make one (not beginner stuff) or you pass the elements of the stuct individually (like ptr->name)
5) You fill-out the speaker data pass it to getSpeaker, don't do anything with it and then fill out a new Speaker. This could be correct syntactically but it seems to lack any functionality.

OK I'm a bit confused.
Can you elaborate?
Urgh. I fixed it up but it still isn't working.

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

using namespace std;

//struct
struct Speaker
{
	string name;
	string phoneNumber;
	string email;
	string theme;
	double fee;
};

//prototype
int* getSpeaker(Speaker s);
/*************************************
* Function: main
* Description: 
**************************************/
int main()
{
	//declare speaker variable
	Speaker s1;
	Speaker s2;
	//fill struct information
	s1.name = "Alex";
	s1.phoneNumber = "(425)123-4567";
	s1.email = "internetMail@gmail.com";
	s1.theme = "mysterious";
	s1.fee = 20.95;

	//output results of struct
	cout << "The speaker is " << s1.name << ". " << "Contact information is " 
		<< s1.phoneNumber << " and " << endl;

	cout << s1.email << ". " << "The theme is " 
		<< s1.theme << " and the fee is $" << s1.fee << "." << endl;
	cout << endl;

	//second part of program
	cout << getSpeaker(s2) << endl;

	
	//end without error
	return 0;
}

/*************************************
* Function: 
* Description: 
* Input: 
* Output: 
**************************************/

int* getSpeaker(Speaker s)
{
	
	Speaker *s;
	s = new Speaker;

	cout << "enter name: ";
	cin >> s->name; 
	cout << "enter number: ";
	cin >> s->phoneNumber;
	cout << "enter email: ";
	cin >> s->email;
	cout << "enter theme: ";
	cin >> s->theme;
	cout << "enter fee: ";
	cin >> s->fee;

	return s;
}
Last edited on
I just don't get why i get red squigglies when I create the dynamic part on line 61. After that, the cin statements have a red squiggly under the s before the arrow and a red squiggly under the s in the return statement on line 74. Am I doing something wrong?
Last edited on
"Am I doing something wrong?"

Yes.

- You declare s in your function while Speaker s is already a parameter of getSpeaker. You can't have twice the same local name.

- Apart from that it is nonsensical to assign s to a new Speaker object since it exists already.



Last edited on
How would I go about doing it the correct way? Would i have to change my return data type to a struct* also?
Last edited on
If you intend to return a Speaker struct, yes.
Or as a struct pointer via the function parameters.
Ok i have figured it out. Thanks for helping.
Topic archived. No new replies allowed.