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:
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>
usingnamespace 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
constint min = 25;
constint 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;
elseif (p1.playerType == 2)
cout << "Player type: " << setw(17) << "2: Swordswoman" << endl;
elseif (p1.playerType == 3)
cout << "Player type: " << setw(17) << "3: Sorcerer" << endl;
elseif (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.
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().
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.
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.