Reputation: 1
Image of error It asks the question twice, just needing it asked once but the loops runs good and in-unison with the other parts of the program. Thank you for any ideas or suggestions, college class btw!
In a new program create the following functions: Story
, AskYesNo
; one string argument named question
, RollDie
; one int parameter named sides
with a default value of 6, Ending
, Adventure
, obviously main
.
Create the following global variables: health
, totalTreasure
.
The game should run as follows:
The player is told a story prompting them to seek treasure on an adventure. The player is asked if they would like to go adventuring. If the player says yes, numbers for an enemy attack, their own block, and an amount of treasure are randomly generated. If the player's block is higher than the enemy attack, they have successfully blocked and they receive the treasure. However, if the attack is higher, that number is subtracted from their health and they do not get the treasure. The player is then told their health and amount of totalTreasure
and asked if they would like to adventure again. They can continue to adventure as long as their health is greater than zero, at which point the ending is run and they are told that they are dead If they instead quit before their health is zero, they are told how much health they have and how much treasure they ended up with.
// Global variables
int playerHealth = 15;
int playerTreasure = 0;
// Function Prototypes
void ending();
void story();
void adventure();
int rollDie(int sides = 6);
int main() { //My so called storyboard lol
srand(time(0));
story();
adventure();
ending();
return 0;
}
//Functions
void ending() {
if (playerHealth <= 0) {
cout << endl;
cout << "Your out of health! :(\n";
cout << "The treasure you end up with is " << playerTreasure << " pieces!\n";
cout <<"Thanks for adventuring!\n";
} else { // if (askYesNo("Do you want to keep adventuring? (y/n)") == "n")
cout << "You retired from adventuring!\n";
cout << "You retired with " << playerTreasure << " treasure pieces!\n";
}
return;
}
void story() {
cout << "You wake up in a forrest, by yourself\n";
cout << "You only have " << playerHealth << " health points left.\n";
cout << endl;
}
string askYesNo(string givenQuestion) {
string input;
do {
cout << givenQuestion << endl;
cin >> input;
} while (input != "y" && input != "n");
return input;
}
int rollDie(int sides) {
return rand() % sides + 1;
}
void adventure() {
do {
if (askYesNo("Do you want to keep adventuring? (y/n)") == "y")
{
int enemyAttack = rollDie(100);
int playerBlock = rollDie(100);
if (enemyAttack > playerBlock)
{
playerHealth -= 3;
cout << "The enemy attack points is " << enemyAttack << "\n";
cout << "Your player block is " << playerBlock << "\n";
cout << "Your current health is " << playerHealth << " .\n";
cout << "You did not gain any treasure\n";
} else if (playerBlock >= enemyAttack)
{
playerTreasure += rollDie(6);
cout << "The enemy attack points is " << enemyAttack << "\n";
cout << "Your player block is " << playerBlock << "\n";
cout << "You have blocked!\n";
cout << "You gain treasure! your current treasure is " << playerTreasure << " \n";
}
} else {
cout << "Your current health is " << playerHealth << " .\n";
cout << "Your current amout of treasure is " << playerTreasure << " \n";
return;
}
} while(playerHealth > 0 && askYesNo("Do you want to keep adventuring? (y/n)") == "y");
}
No error messages.
Upvotes: 0
Views: 111
Reputation: 3744
I think you should use simple while
like this:
while (playerHealth > 0)
{
if (askYesNo("Do you want to keep adventuring? (y/n)") == "y")
{
// do the yes part
}
else
{
// do no part
return; // this will break your while
}
}
Upvotes: 0
Reputation: 21
The problem is with the askYesNo()
function called twice. If you are calling askYesNo()
inside the while()
condition, then no need to call askYesNo()
again inside the if()
. So, it's basically repeating that.
Upvotes: 1
Reputation: 881153
if (enemyAttack >= playerBlock)
else if (playerBlock < enemyAttack)
I think, for a start, you should fix that combination of conditions. The second can never be true if the first is false.
Since you state that "treasure is never gained" and the gaining is in the code that can never be executed, that will fix at least one of your issues.
I suspect what you're after is something like:
if attack > block:
lose health
else if attack < health:
gain treasure
else:
do neither
With suitable output in all cases of course.
Upvotes: 1