Help with program involving structs and selection statements

Hey guys,
I wrote this program and everything is working fine except this one part. I have to ask the user to pick a number that corresponds to a 'player' in the 'game'. I wrote the function for it but when it comes to displaying all of the player info at the end, it doesn't display the player type, just the name and the power level. I used selection statements to display the correct one, but it doesn't do it. i don't have any errors and it compiles fine. This is my program:
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206

#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;

struct player
{
	string playerName;
	int playerType;
	int playerPower;
};

// Function Prototypes
void description();
void types();
player readPlayer();
player readPlayerType();
int randomGenerator(int min, int max);
void displayPlayer(const player p1);

// main function
int main()
{
	// variable declarations
	const int min = 25;
	const int max = 50;
	player one;
	string answer;
	

	// call to function decription
	description();

	cout << "Would you like to create a player for this game?" << endl << endl;

	cin >> answer;

	if (answer == "yes")
	{

	cout << "These are the classes you can select for your player type: " << endl;

	// call to function types
	types();
	
    // call to function readPlayer
	one = readPlayer();

	cout << endl;

    // call to randomGenerator function and assign it to playerPower
	one.playerPower = randomGenerator(min, max);

    // call to function displayPlayer
	displayPlayer(one);

	}

	else 
		cout << "You've entered an invalid response!" << endl
			<< "GOOD DAY SIR/MAM!" << endl;



	cout << "Please press enter once or twice to continue...";
	cin.ignore().get();    			// hold console window open
	return EXIT_SUCCESS;           	// successful termination
	
}


/* 
Data Requirements:
none

Design:
    1. Display description of game to user
        1.1. Use cout statements to display a description
            of the game.
*/
void description()
{
	cout << "        WARRIORS         " << endl;
	cout << "This game allows you to  " << endl;
	cout << "provide a name and select" << endl;
	cout << "a type of warrior. It also" << endl;
	cout << "randomly picks a power " << endl;
	cout << "level for your character." << endl << endl << endl;
}


/*
Data Requirements:
None

Design:
    1. Display types of players to choose from to user
        1.1. Use cout statements to display the types
        of player that the user can enter. 
*/
void types()
{
	cout << "1: Swordsman" << endl;
	cout << "2: Swordswoman" << endl;
	cout << "3: Sorcerer" << endl;
	cout << "4: Witch" << endl << endl << endl;
}


/*
Data Requirements:
None

Design:
    1. Prompt the user to enter which warrior they would
    like to be
    2. Read and store data in struct data playerType
    3. Prompt the user to enter a name for their warrior.
    4. Read and store data in struct data playerName.
    5. Return playerObject
*/
player readPlayer()
{
	player playerObject;

	readPlayerType();


	cout << endl << "Now enter a name for your warrior: " << endl << endl;

	cin >> playerObject.playerName;
	
	return playerObject;

}

player readPlayerType()
{
	player playerObjectType;

	cout << "Please enter the number corresponding to the warrior that you would like to be: " << endl << endl;

	cin >> playerObjectType.playerType;

	if (playerObjectType.playerType > 4 || playerObjectType.playerType <= 0)
	{
		playerObjectType.playerType = 3;
	
	}

	 
	return playerObjectType;
}


/*
Data Requirements:
int min = 25
int max = 50

Relevant Formula:
(min + rand() % (max - min + 1))

Design:
1. create a random number generator.
    1.1. use the formula : (min + rand() % (max - min + 1))
2. Return randomGenerator
*/
int randomGenerator(int min, int max)
{
	return (min + rand() % (max - min +1));
}

/* 
Data requirements:
None

Design:
1. Use cout statments to display all player information 
stored in struct player.
2. Use iomanip routines to display the information in columns,
and within a length specified. 
*/
void displayPlayer(const player p1)
{ 
	
	
	cout << "******************************" << endl;
	cout << "Player name: " << setw(17) << p1.playerName << endl;
	if (p1.playerType == 1)
		cout << "Player type: " << setw(17) << "1: Swordsman" << endl;
	else if (p1.playerType == 2)
		cout << "Player type: " << setw(17) << "2: Swordswoman" << endl;
	else if (p1.playerType == 3)
		cout << "Player type: " << setw(17) << "3: Sorcerer" << endl;
	else if (p1.playerType == 4)
		cout << "Player type: " << setw(17) << "4: Witch" << endl;
	cout << "Player power: " << setw(16) << p1.playerPower << endl;
	cout << "******************************" << endl<< endl << endl;
}


I think the problem lies within the readPlayerType function, but I'm not sure. Any help would be appreciated. I'm going to continue to try and work it out. Thank.
One obvious thing to do to help you catch problems is have a final "else" block in displayPlayer(), that could print out an error message such as "Error, invalid value for playerType: " followed by the value of p1.playerType. This would immediately alert you to something being wrong.

We call this a "dangling else-if" - a situation where you have code to handle some possible conditions, but not all of them.

The problem is that in readPlayer(), you completely ignore the object that's returned from readPlayerType(). You create an object of type player, and you set the name of it, but that's all.
Last edited on
Thanks for the response, I appreciate it.
That's what I was thinking, that I call the function readPlayerObject, but it doesn't know what to do with it so it doesn't display it. How would I go about fixing it?
In readPlayer(), you need to use the value returned by readPlayerType(). You obviously already know how to do this, because you've done it with the call to readPlayer() in main().
Last edited on
i tried doing this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
player readPlayer()
{
	player playerObject;
	

	playerobject.playerType = readPlayerType();


	cout << endl << "Now enter a name for your warrior: " << endl << endl;

	cin >> playerObject.playerName;
	
	return playerObject;

}


but it gives me an error saying I cannot convert type player to type int. I feel like i'm almost there. It's driving me bananas.
but it gives me an error saying I cannot convert type player to type int.

Well, yes. What type is playerobject.playerType? An int. What type does readPlayerType() return? A player object.
How do I go about getting readPlayerType to return a type int?
Nevermind, just fixed it. I just changed the function type to int. Here's the code for anyone interested:

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
player readPlayer()
{
	player playerObject;

	playerObject.playerType = readPlayerType();
	

	cout << endl << "Now enter a name for your warrior: " << endl << endl;

	cin >> playerObject.playerName;
	
	return playerObject;

}

int readPlayerType()
{
	int playerObjectType;

	cout << "Please enter the number corresponding to the warrior that you would like to be: " << endl << endl;

	cin >> playerObjectType;

	if (playerObjectType > 4 || playerObjectType <= 0)
	{
		playerObjectType = 3;
	
	}

	 
	return playerObjectType;


Thanks for all the help! I was thinking too hard on this one.
You're welcome - glad it worked out :)
Topic archived. No new replies allowed.