Accessing Class objects to compare with global variables - c++

Im making a text store menu. I have a basic weapon class with a string for name and weapon sound, a bool for if you already own the item or not and a double for the price.
I'm trying to compare the double price in the class to the global variable p_balance (players balance) to
check if the player can afford the item
Here's the class
class Weapon
{
public:
string name, sound;
bool owned = false;
double price;
public:
Weapon() //Default Constructor
{
name = "name";
sound = "sound";
owned = owned;
price = 0;
}
Weapon(string i_name, string i_sound, bool i_owned, double i_price)//Constructor
{
name = i_name;
sound = i_sound;
owned = i_owned;
price = i_price;
void displayItems();
{
cout << name << " - " << price << "g." << endl;
}
}
};
(i know i know i should switch Weapon Weapons[i] to Weapon weapons[i] to make my life easier)
And here's my most recent attempt at accessing it
while (p_balance < Weapons[0].getPrice()) // Not enough gold
{
cout << "Sorry, it seems you don't have enough gold." << endl;
goto MainMenu;
}
I had tried to make a method within the class to get the price but that didnt work out, but thats why its calling on a method that doesnt exist
I've also toyed around with if loops but the were getting messy and displaying more info than i liked
After the owner money check, i have it perform a ownership check and check if something equipped to that items slot, but when I used nested if statements it displayed the result of all of them even if the first if (price) was failed so I figured maybe a while statement?
I'm not looking for the answer cause I want to learn but last time I posted here I got some great hints that really helped me out and I'm hitting the point of frustration with this so I figured I'd ask for help!
EDIT:
I took what you guys said into account and went back to the pseudo.
I'm quite pleased with how it is now:
#include <iostream>
#include <string>
using namespace std;
int p_choice;
double p_balance = 125;
//Simple Weapon Class
class Weapon
{
public:
int slot;
string name, sound;
bool owned = false;
double price;
public:
Weapon() //Default Constructor
{
slot = 0;
name = "name";
sound = "sound";
owned = owned;
price = 0;
}
Weapon(int i_slot, string i_name, string i_sound, bool i_owned, double i_price)//Constructor
{
slot = i_slot;
name = i_name;
sound = i_sound;
owned = i_owned;
price = i_price;
double* p_ptr;
p_ptr = &price;
void displayItems();
{
int x = 0;
cout << slot << " - " << name << " - " << price << "g." << endl;
}
}
};
int main()
{
int p_selection;
bool hasWeapon, offHand, inStore;
hasWeapon = false;
offHand = false;
inStore = true;
//Weapon Array
cout << "---------------------------------------------" << endl;
Weapon Weapons[6];
Weapons[0] = Weapon(1, "Great Sword of Greatening", "Tssng", false, 50);
Weapons[1] = Weapon(2, "Claw", "Thwack", false, 10);
Weapons[2] = Weapon(3, "Silent Pea-shooter", "Pew Pew", false, 15);
Weapons[3] = Weapon(4, "Loudening Silencer Attachment", "...", false, 5);
Weapons[4] = Weapon(5, "Tiger-Repelling Rock ", "Meooowraaarah", false, 25);
Weapons[5] = Weapon(6, "The Stick of Truth", "How do you kill that which has no life", false, 100);
cout << "---------------------------------------------" << endl;
cout << "Welcome to my store, traveller!" << endl;
cout << "How can I help you?" << endl;
//Menu
do
{
cout << "1. Buy" << endl;
cout << "2. Check Balance" << endl;
cout << "3. Exit" << endl;
cin >> p_choice;
if (p_choice == 1)
{
//Ask player which weapon the want to buy
cout << "Which item are you interested in?" << endl;
cout << "(Choose using 1-6)" << endl;
cin >> p_selection;
if (p_selection >= 1 && p_selection <= 6)
{
int i_choice = p_selection - 1;// Take one off the choice so it corresponds correctly to the object in the array
if (Weapons[i_choice].owned == false && p_balance >= Weapons[p_selection].price)// check if item is owned/can be bought
{
if (hasWeapon == false)// flip "Slot" flag
{
hasWeapon = true;
}
Weapons[i_choice].owned = true;// flip ownership flag
p_balance = p_balance - Weapons[i_choice].price; // update balance
cout << "Good eye, that's a fine item! Enjoy your " << Weapons[i_choice].name << endl;
}
else if (Weapons[i_choice].price > p_balance)
{
cout << "Seems you're a little short on gold..." << endl;
}
else if (Weapons[i_choice].owned == true)
{
cout << "Seems you don't know how to dual wield, why don't you pick something else." << endl;
}
}
}
// Check Balance
if (p_choice == 2)
{
cout << "Your balance is: " << p_balance << "g." << endl;
}
// Leave
if (p_choice == 3 && inStore == true)
{
cout << "Thanks for stopping by! Good luck out there!" << endl;
inStore = false;
}
else if (p_choice == 3 && hasWeapon == false)
{
cout << "You're gonna want a weapon out there, traveller" << endl;
}
} while (inStore == true);
//Scenario
bool scenario = true;
cout << "Inventory" << endl;
for (int x = 0; x < 6; x++)
{
if (Weapons[x].owned == true)
{
//convey slot
cout << "Press " << Weapons[x].slot << " to use your " << Weapons[x].name << endl;
cin >> p_choice;
}
}
do
{
for (int y = 0; y < 1; y++)
{
int use = p_choice - 1;
if (Weapons[use].owned == true)
{
cout << Weapons[use].sound << endl;
}
}
} while (scenario == true);
system("pause");
return 0;
}
Only issue is it plays the "sound" nonstop, that for loop was an attempt to have it run 1 time lol

Here's the final final product
#include <iostream>
#include <string>
using namespace std;
int p_choice;
double p_balance = 125;
//Simple Weapon Class
class Weapon
{
public:
int slot, durability;
string name, sound;
bool owned = false;
double price;
public:
Weapon() //Default Constructor
{
slot = 0;
name = "name";
sound = "sound";
owned = owned;
price = 0;
durability = 0;
}
Weapon(int i_slot, string i_name, string i_sound, bool i_owned, double i_price, int i_durability)//Constructor
{
slot = i_slot;
name = i_name;
sound = i_sound;
owned = i_owned;
price = i_price;
durability = i_durability;
void displayItems();
{
int x = 0;
cout << slot << " - " << name << " - " << price << "g." << endl;
}
}
};
int main()
{
int p_selection;
bool hasWeapon, offHand, inStore;
hasWeapon = false;
offHand = false;
inStore = true;
//Weapon Array
cout << "---------------------------------------------" << endl;
cout << "For Sale:" << endl;
Weapon Weapons[6];
Weapons[0] = Weapon(1, "Great Sword of Greatening", "Tssng", false, 50, 25);
Weapons[1] = Weapon(2, "Claw", "Thwack", false, 10, 15);
Weapons[2] = Weapon(3, "Silent Pea-shooter", "Pew Pew", false, 15, 20);
Weapons[3] = Weapon(4, "Loudening Silencer Attachment", "...", false, 5, 5);
Weapons[4] = Weapon(5, "Tiger-Repelling Rock ", "Meooowraaarah", false, 25, 30);
Weapons[5] = Weapon(6, "The Stick of Truth", "How do you kill that which has no life", false, 100, 50);
cout << "---------------------------------------------" << endl;
cout << "Welcome to my store, traveller!" << endl;
cout << "How can I help you?" << endl;
//Menu
do
{
cout << "1. Buy" << endl;
cout << "2. Check Balance" << endl;
cout << "3. Exit" << endl;
cin >> p_choice;
if (p_choice == 1)
{
//Ask player which weapon the want to buy
cout << "Which item are you interested in?" << endl;
cout << "(Choose using 1-6)" << endl;
cin >> p_selection;
if (p_selection >= 1 && p_selection <= 6)
{
int i_choice = p_selection - 1;// Take one off the choice so it corresponds correctly to the object in the array
if (Weapons[i_choice].owned == false && p_balance >= Weapons[p_selection].price)// check if item is owned/can be bought
{
if (hasWeapon == false)// flip weapon flag
{
hasWeapon = true;
}
Weapons[i_choice].owned = true;// flip ownership flag
p_balance = p_balance - Weapons[i_choice].price; // update balance
cout << "Good eye, that's a fine item! Enjoy your " << Weapons[i_choice].name << endl;
}
else if (Weapons[i_choice].price > p_balance)
{
cout << "Seems you're a little short on gold..." << endl;
}
else if (Weapons[i_choice].owned == true)
{
cout << "Seems you don't know how to dual wield, why don't you pick something else." << endl;
cout << "(You already own this item)" << endl;
}
}
}
// Check Balance
if (p_choice == 2)
{
cout << "Your balance is: " << p_balance << "g." << endl;
}
// Leave
if (p_choice == 3 && inStore == true)
{
cout << "Thanks for stopping by! Good luck out there!" << endl;
inStore = false;
}
else if (p_choice == 3 && hasWeapon == false)
{
cout << "You're gonna want a weapon out there, traveller" << endl;
}
} while (inStore == true);
//Scenario
bool scenario, equipped;
int attack, equip, choose;
equipped = false;
scenario = true;
do
{
Equip:
cout << "----------------------------------------------------" << endl;
cout << " Inventory" << endl;
for (int x = 0; x < 6; x++)
{
if (Weapons[x].owned == true)
{
//convey slot and display equippable items
cout << Weapons[x].slot << " - " << Weapons[x].name << " - Uses Left: " << Weapons[x].durability << endl;
}
}
cout << "----------------------------------------------------" << endl;
cout << endl;
cout << "What would you like to equip? (Select using the given number + Enter)" << endl;
cout << "(Press 9 to quit)" << endl;
cin >> choose;
equip = choose - 1;
if (choose == 9)
{
scenario = false;
}
else if (Weapons[equip].owned == true)
{
int action;
cout << "You have equipped " << Weapons[equip].name << endl;
Action:
cout << "Press 1 and Enter to use." << endl;
cout << "Press 2 and Enter to equip a new item" << endl;
cout << "Press 3 and Enter to go home" << endl;
cin >> action;
if (Weapons[equip].durability <= 0)// cant use broken items
{
cout << "That item appears to have broken!" << endl;
}
else if (action == 1 && Weapons[equip].durability >= 0)// attack and durability check
{
cout << Weapons[equip].sound << endl;
cout << endl;
Weapons[equip].durability--;// durability decrement
goto Action;
}
else if (action == 2)// change equip
{
goto Equip;
}
else if (action == 3)// exit
{
scenario = false;
}
}
} while (scenario == true);
cout << endl;
cout << "I wish you a safe journey home, traveller." << endl;
system("pause");
return 0;
}
Thanks again for the guidance.

Related

BlackJack Application. All menu options work except for the first option to actually play the game

I wrote a Black jack application in C++ and for some reason, when I choose the first option to play a game, it doesn't play. All other menu options work. Any help would be appreciated.
#include <iostream>
#include <string>
#include <conio.h>
#include "Card.h"
#include <vector> // Stadard Template Library (STL)
using namespace std;
string showCards( vector<Card> cards);
short sumCardValues( vector<Card> cards);
int main()
{
short cash = 100;
cout << "Welcome to Kyle's BlackJack Extreme! " << endl;
cout << "\nYou are starting out with $ " << cash << endl;
cout << "\nPress any key to continue... ";
_getch();
short choice = 0;
do
{
system("cls");
cout << "Menu\n" << endl;
cout << "1) Play a hand" << endl;
cout << "2) Show current cash balance" << endl;
cout << "3) Exit" << endl;
cout << "\nEnter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "\nPlaying a hand..." << endl;
break;
case 2:
cout << "\nYour current cash balance: $" << cash << endl;
break;
case 3:
cout << "\nThank you for playing Kyle's Extreme BlackJack! " << endl;
cout << "\nYour final cash position: $ " << cash << endl;
break;
default:
cout << "\nError. Please select from the menu. " << endl;
break;
}
cout << "\nPress any key to continue...";
_getch();
} while (choice != 3);
return 0;
}
/// Show the cards in the vector (resizable aray)
string showCards(vector<Card> cards)
{
string output = "";
for (Card c : cards)
{
output += c.toString() + " ";
}
return output;
}
/// Add up the total value of the cards
short sumCardValues(vector<Card> cards)
{
short total = 0;
for (Card c : cards)
{
total += c.getValue(); // total = total + c.getValue();
}
return total;
}
/// Play a single hand of BlackJack
void playHand(short &cash)
{
// create two ArrayLists that can hold Card objects
// an AAraylist is a seizable array
// in C++, the ArrayList is called a vector
vector<Card> dealerCards;
vector<Card> playerCards;
short dealerCardsTotal = 0;
short playerCardsTotal = 0;
// get be amount
short bet = 0;
cout << "\nEnter bet amount: ";
cin >> bet;
// create two cards for the dealer and show the first one
int number;
Card card1;
Card card2;
dealerCards.push_back(card1);
dealerCards.push_back(card2);
dealerCardsTotal = sumCardValues(dealerCards);
cout << "\nDealer is showing: " << dealerCards[0].toString() << endl;
// create two cards for the player and show them both
playerCards.push_back(Card()); // create the card and put it directly into the ArrayList (vector)
playerCards.push_back(Card());
playerCardsTotal = sumCardValues(playerCards);
cout << "\nHere are your cards: " << showCards(playerCards) << endl;
// give cards to the player until they stand ('S')
char answer = '?';
do
{
cout << "\nDo you want to hit or stand (H/S)? ";
cin.sync(); // flush the input stream (keyboard buffer) -- cin.ignore(100, '\n')
cin >> answer;
cin.sync();
if (toupper(answer) == 'H')
{
// give a card to the player
Card c;
cout << "\nYou were dealt this card: " << c.toString() << endl;
playerCards.push_back(c); // add card to Player's hand
// sum up the card values
playerCardsTotal = sumCardValues(playerCards);
// did the player bust?
if (playerCardsTotal > 21)
{
// do you have an Ace that can be dropped to a 1 value?
for (Card c : playerCards)
{
if (c.getValue() == 11)
{
cout << "\nYour total is " << playerCardsTotal << endl;
c.flipAceToOne();
cout << "However, you have an Ace that was converted to '1' value" << endl;
playerCardsTotal = sumCardValues(playerCards);
cout << "\nYour new total is " << playerCardsTotal << endl;
// if we are good now, break out of the loop. otherwise, keep looping and looking for Aces
if (playerCardsTotal <= 21)
break;
}
}
}
// show the cards and the total
cout << "\nHere are your cards: " << showCards(playerCards) << endl;
cout << "Your total is " << playerCardsTotal << endl;
// if busted, stop the loop
if (playerCardsTotal > 21)
answer = 'S';
}
} while (toupper(answer) != 'S');
// if player's cardTotal is more than 21, the player busted so take money away
if (playerCardsTotal > 21)
{
cout << "\nYou busted!" << endl;
cash = cash - bet;
}
else
{
// player stands so the dealer hits until 17 or greater
do
{
if (dealerCardsTotal < 17)
{
Card c;
cout << "\nDealer was dealt: " << c.toString() << endl;
dealerCards.push_back(c); // add the card to the dealer's hand
cout << "\nDealer cards: " << showCards(dealerCards) << endl;
dealerCardsTotal = sumCardValues(dealerCards);
cout << "Dealer total: " << dealerCardsTotal << endl;
}
} while (dealerCardsTotal < 17);
// show the cards for the dealer and the player
cout << "\nYour cards: " << showCards(playerCards) << " (" << playerCardsTotal << ")" << endl;
cout << "\nDealer cards: " << showCards(dealerCards) << "(" << dealerCardsTotal << ")" << endl;
// who wins?
if (dealerCardsTotal > 21)
{
cout << "\nDealer Busted!" << endl;
cash = cash + bet;
}
else if (dealerCardsTotal > playerCardsTotal)
{
cout << "\nDealer Wins." << endl;
cash = cash - bet;
}
else if (playerCardsTotal > dealerCardsTotal)
{
cout << "\nYou win!" << endl;
cash = cash + bet;
}
else
{
cout << "\nYour pushed the dealer (tie)." << endl;
}
}
// show the current cash position
cout << "\nYour current cash balance: $" << cash << endl;
}
Card.H
#pragma once
#include <ctime>
#include <stdlib.h>
#include <string>
using namespace std;
class Card
{
private:
// attributes
char suit;
char face;
short value;
static bool randomizerSeeded; // class-wide variable (one copy in memory is shared by all Card objects)
public:
// constructor and destructor
Card();
~Card();
// behaviors
string toString();
bool flipAceToOne();
// accessors and mutators
inline char getSuit() { return suit; }
inline char getFace() { return face; }
inline short getValue() { return value; }
};
Card.cpp
#include "Card.h"
bool Card::randomizerSeeded = false;
Card::Card()
{
// seed the randomizer only once for all card objects
if( !randomizerSeeded ) //if (randomizerSeeded == false)
{
srand(time(NULL));
randomizerSeeded = true;
}
short min = 3;
short max = 6;
suit = rand() % (max - min + 1) + min;
min = 2;
max = 14;
short number = rand() % (max - min + 1) + min;
if (number >= 2 && number <= 9)
{
value = number;
face = number + 48;
}
else if (number == 10)
{
value = number;
face = 'T';
}
else if (number == 11)
{
value = 10;
face = 'J';
}
else if (number == 12)
{
value = 10;
face = 'Q';
}
else if (number == 13)
{
value = 10;
face = 'K';
}
else if (number == 14)
{
value = 11;
face = 'A';
}
else
{
value = -1;
face = 'E';
}
}
Card::~Card()
{
}
string Card::toString()
{
string output = "";
output += suit;
output += face;
return output;
}
bool Card::flipAceToOne()
{
if (value == 11)
{
value = 1;
return true;
}
else
{
return false;
}
}
In the case to play a hand, you never call the playHand() function.
case 1:
cout << "\nPlaying a hand..." << endl;
break;
Adding this call will fix that issue.
playHand(cash)

How to Carry Over Depleted Health to the next Loop?

I have this program where instantiate player character, enemy characters, and their weapoons. The player characters choose who to attack, and the enemies attack the players at random. They have have their own health, positions, and weapon damage set at random. I also have a Weapon function dodamage(), which decreases the player's healthpoints by weapon damage. In main(), I set a while(true) loop where each character attacks and depletes health. The problem is each loop after the first, the health, and damage are the same as the beginning. They never change after the first loop. I was wondering how I could carry resulting damage over to the next loop until health reaches <=0.
Main.cpp
void playerAttack(Character c1, Character c2, Weapon w, string target)
{
if (target == "1")
{
w.DoDamage(c1.healthPoints, w.Damage);
if (c1.healthPoints <= 0)
c1.die();
}
else if (target == "2")
{
w.DoDamage(c2.healthPoints, w.Damage);
if (c2.healthPoints <= 0)
c2.die();
}
}
void enemyAttack(Character c1, Character c2, Weapon w)
{
srand(time(NULL));
int randTarget = rand() % 2 + 1;
if (randTarget = 1)
{
w.DoDamage(c1.healthPoints, w.Damage);
if (c1.healthPoints <= 0)
c1.die();
}
else if (randTarget = 2)
{
w.DoDamage(c1.healthPoints, w.Damage);
if (c2.healthPoints <= 0)
c2.die();
}
}
int main()
{
PlayerCharacter p1, p2;
EnemyCharacter e1, e2;
Weapon p1W = p1.currentWeapon;
Weapon p2W = p2.currentWeapon;
Weapon e1W = e1.currentWeapon;
Weapon e2W = e2.currentWeapon;
string enemyTarget, playerTarget;
p1.printCharacter();
p2.printCharacter();
e1.printCharacter();
e2.printCharacter();
while (true)
{
cout << "Player One. Choose your Target (1 or 2)" << endl;
cin >> playerTarget;
playerAttack(e1, e2, p1W, playerTarget);
cout << "Enemy One Attacks" << endl;
enemyAttack(p1, p2, e1W);
cout << "Player Two. Choose your Target (1 or 2)" << endl;
cin >> playerTarget;
playerAttack(e1, e2, p2W, playerTarget);
cout << "Enemy Two Attacks" << endl;
enemyAttack(p1, p2, e2W);
}
if (p1.isDead && p2.isDead)
cout << "Game Over!" << endl;
else if (e1.isDead && e2.isDead)
cout << "You Win!" << endl;
system("pause");
return 0;
}
Weapon.cpp
void Weapon::printWeapon()
{
srand(time(NULL));
cout << "Weapon: " << WeaponType[randomName] << endl;;
cout << "Type: " << object->WeaponName[randomType] << endl;
cout << "Damage: " << Damage << endl;;
}
int Weapon::DoDamage(int health, int damage)
{
int resultHealth = health - damage;
cout << "Damage - " << damage << endl;
cout << "Health: " << resultHealth << endl << endl;
health = resultHealth;
return health;
}
Character.cpp (Base class fro playercharacter and enemycharacter)
Character::Character()
{
srand(time(NULL));
populatePosition(3);
}
void Character::printCharacter()
{
srand(time(NULL));
healthPoints = rand() % 100;
cout << "Enter First Name" << endl;
cin >> firstName;
cout << "Enter Last Name" << endl;
cin >> lastName;
cout << endl;
cout << firstName << " " << lastName << ": " << endl;
cout << "Health: " << healthPoints << endl;
cout << "Position: "<<endl;
for (auto p:position)
{
cout << p << ", " << endl;
}
cout << endl;
}
void Character::populatePosition(int vectorSize)
{
for (int i = 0; i < vectorSize; i++)
{
f = rand() % 20;
position.push_back(f);
}
}
void Character::die()
{
cout << firstName << " " << lastName << " is dead " << endl << endl;
isDead = true;
}
The issue is that DoDamage never updates anything outside of it's scope.
You're passing health by value, so the results of DoDamage never get used. You're also not catching the results of the return value, which could also be used to update the value.
To fix it, either A, make the heath parameter of DoDamage be pass by reference
int Weapon::DoDamage(int & health, int damage)
{
int resultHealth = health - damage;
cout << "Damage - " << damage << endl;
cout << "Health: " << resultHealth << endl << endl;
health = resultHealth;
return health;
}
references are basically like pointers to data, but you don't have to remember to dereference them, the compiler will remember that for you. You just have to remember that the value coming in is at the same address as the value that you're working with.
Or B, use the value being returned to re-assign the healthpoints of the character in the attack functions.
e.g:
w.DoDamage(c1.healthPoints, w.Damage);
could look like:
c1.healthPoints = w.DoDamage(c1.healthPoints, w.Damage);

Why is failed input validation returning me to the start of my program?

Really sorry if this is a dumb question. I know it must have a super easy solution but I've been staring at this for so long I can't see it. It doesn't help that I'm really new at this either.
Long story short for some reason entering an invalid input past the first time returns me back to my menu, and sometimes also asks me to enter weight immediately after instead of allowing me to enter a menu choice. It's just all around broken and I don't know why. Thanks.
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
bool loopFlag = true;
bool loopFlagTwo = true;
int choice = 0;
int time = 0;
float weightPounds = 0;
float weight = 0;
const int BIKING = 8;
const int RUNNING = 10;
const int LIFTING = 3;
const float YOGA = 2.5;
int main()
{
cout << "Welcome to my Fitness Center" << endl;
do
{
cout << "\n\t____________________________________________________________" << endl;
cout << "\n\t\t\tMy Fitness Center" << endl;
cout << "\t\t\tActivity System" << endl;
cout << "\t____________________________________________________________" << endl;
cout << "\t\t\t Main Menu\n" << endl;
cout << "\t\t\t1) Stationary Bike" << endl;
cout << "\t\t\t2) Treadmill" << endl;
cout << "\t\t\t3) Weight Lifting" << endl;
cout << "\t\t\t4) Hatha Yoga" << endl;
cout << "\t\t\t5) End" << endl;
cout << "\t____________________________________________________________" << endl;
cout << "\n\nEnter the workout that you wish to track, or end to exit:" << endl;
do
{
cin >> choice;
if (cin.fail() || choice > 5 || choice < 1)
{
cout << "Invalid choice. Please choose from option 1 through 5." << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
else if (choice == 5)
{
return 0;
}
else
{
loopFlag = false;
}
}
while (loopFlag);
do
{
cout << "\nPlease enter your weight in pounds: " << endl;
cin >> weightPounds;
if (cin.fail() || weightPounds <= 0)
{
cout << "Invalid weight entry!" << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
else
{
loopFlag = false;
}
}
while (loopFlag);
weight = weightPounds / 2.2;
cout << "\nYour weight is: \n" << fixed << setprecision(1) << weight << " kilograms." << endl;
if (choice == 1)
{
do
{
cout << "For how many minutes did you do this activity? " << endl;
cin >> time;
if (cin.fail() || time <= 0)
{
cout << "Invalid time entry!" << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
else
{
loopFlag = false;
}
}
while (loopFlag);
}
}
while (choice != 5);
return 0;
}
You need to set loopFlag to true before every do...while() you have, or use another flag, because after the first do...while(), loopFlag is always false.

Moving object from a stack to an array C++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Hey guys so I am writing some coursework and I am almost there, in my code I create a treasure chest and a bag for the player. When the Player is shown the items in the chest they are asked to keep the items which are then stored in there bag or discard the items.
While testing my code I have noticed that the item the player is being shown is not the one that then stores in the bag, almost like the bag is taking from an invisible stack. Then at the end when I call the players bag it still shows as empty as if no items where ever stored????
Please can someone tell me where I am going wrong and how I might resolve it???
These are the specific sections of code that is causing the bug:
void PrintTreasureChest()
{
//TreasureChest A;
int j;
for (j = 1; j < 5; j++)
{
cout << "Item " << j << " in your chest is: " << endl;
cout << "Name:" << (TreasureChest().Chest.top()).Name << endl;
cout << "Rarity out of 3: " << (TreasureChest().Chest.top()).Rarity << endl;
cout << "Part of a set: " << (TreasureChest().Chest.top()).Set << endl;
cout << " " << endl;
PlayerChoice(A);
TreasureChest().Chest.pop();
cout << " " << endl;
}
cout << " " << endl;
cout << "This chest is now empty" << endl;
cout << " " << endl;
cout << " " << endl;
cout << "Items in bag: " << endl;
//Game().ShowRucksack();
return;
}
void PlayerChoice()
{
char Answer;
cout << "If you want to keep the item press Y" << endl;
cout << "If you want to discard the item press N" << endl;
cin >> Answer;
while (Answer == 'Y' || Answer == 'y')
{
cout << "Item stored in your bag" << endl;
StoreItem();
return;
}
while (Answer == 'N' || Answer == 'n')
{
cout << "Item was discared from the Treasure Chest" << endl;
return;
}
while (Answer != 'y' || Answer != 'Y' || Answer != 'N' || Answer != 'n')
{
cout << "To decide press Y for accept OR press N for Decline" << endl;
cin >> Answer;
if (Answer == 'Y' || Answer == 'y') {
cout << "Item stored in your bag" << endl;
//store item in bag
return;
}
else (Answer == 'N' || Answer == 'n'); {
cout << "Item was discared from the Treasure Chest" << endl;
return;
}
return;
}
}
void StoreItem()
{
int dim = 10;
int P = index(Rucksack, dim);
Rucksack[P] = A.Chest.top();
cout << "Item placed in your Bag: " << Rucksack[P].Name << endl;
return;
}
Here is the whole code:
// Loot Class v11.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <stack>
#include <vector>
#define LootNumber 14
using namespace std;
// the 3 arrays initialise the 3 stats of each item
string NameOption[] = { "Stone", "Leather Gloves", "Dragon Gauntlets", "Chair Leg", "Dragon Scale Helmet", "Pebble", "Rusted Breastplate", "Dragon Breastplate", "Empty Bottle", "Chainmail Trousers", "Dragon Skin Trousers", "Broken Stick", "Dagger", "Dragons Sword" };
int RarityOption[] = { 0, 1, 3, 0, 3, 0, 2, 3, 0, 2, 3, 1, 2, 3 };
bool SetOption[] = { 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1 };
// this class is grouping Name, Rarity and Set. Resulting in every Loot object made, contains 3 variables
class Loot
{
public:
string Name;
int Rarity;
bool Set;
// constructor initialises each of the feilds
Loot(string N, int R, bool S)
{
Name = N;
Rarity = R;
Set = S;
}
// default constructor
Loot()
{
Name = "Empty";
Rarity = 0;
Set = false;
}
// Prints a randomly selected item of loot to the screen, used to check randomisation and that all loot variables print in the correct order
void PrintLoot()
{
int i = rand() % LootNumber;
Loot A(NameOption[i], RarityOption[i], SetOption[i]);
cout << "Loot item: " << A.Name << endl;
cout << "Rarity out of 3: " << A.Rarity << endl;
cout << "Part of set: " << A.Set << endl;
}
};
// enables the creation of a container to stack Loot items in
class TreasureChest
{
public:
stack<Loot> Chest;
// stacks 4 random items in the chest
TreasureChest()
{
int i = rand() % LootNumber;
int j = rand() % LootNumber;
int k = rand() % LootNumber;
int h = rand() % LootNumber;
Chest.push(Loot(NameOption[j], RarityOption[j], SetOption[j]));
Chest.push(Loot(NameOption[k], RarityOption[k], SetOption[k]));
Chest.push(Loot(NameOption[i], RarityOption[i], SetOption[i]));
Chest.push(Loot(NameOption[h], RarityOption[h], SetOption[h]));
}
// prints full contents of Treasure Chest to screen
void ShowFullChest()
{
int i;
for (i = 1; i < 5; i++)
{
cout << "Item: " << i << endl;
cout << "Name:" << TreasureChest().Chest.top().Name << endl;
cout << "Rarity out of 3: " << TreasureChest().Chest.top().Rarity << endl;
cout << "Part of a set: " << TreasureChest().Chest.top().Set << endl;
TreasureChest().Chest.pop();
}
}
};
// Creates container for player to store their chosen Loot items
class PlayerRuckSack
{
public:
Loot Rucksack[10];
// default constructor initialising each array
PlayerRuckSack()
{
for (int i = 0; i < 10; i++)
{
Rucksack[i] = { "Empty", 0, false };
}
};
// prints contents of a rucksack to the screen to allow the player to see what they have collected
void ShowRucksack()
{
for (int i = 0; i < 10; i++)
{
cout << Rucksack[i].Name << " " << Rucksack[i].Set << " " << Rucksack[i].Rarity << " " << endl;
}
}
// replaces an each array with items of Loot and prints when all arrays have been replaced
int index(Loot x[], int n)
{
int i = 0;
int index;
while (x[i].Name != "empty" && 0 && false && i < n)
{
i++;
index = i;
return index;
}
while (i == n)
{
cout << "BAG FULL" << endl;
}
}
};
// For runing the game
class Game : public PlayerRuckSack
{
public:
string PlayerName;
TreasureChest A;
Game()
{
PlayerName = "User 1";
}
Game(string U)
{
PlayerName = U;
}
// intro message to start game
void StartGame()
{
cout << "Welcome to the Cave of Luck" << endl;
cout << "What is your name brave warrior" << endl;
cin >> PlayerName;
cout << PlayerName << " There are 3 Treasure Chests in this cave" << endl;
cout << "Treasure Chests contain many different items" << endl;
cout << "However it appears your bag is small and can only hold 10 items in total" << endl;
cout << "Choose wisley " << PlayerName << endl;
cout << "Good Luck!!" << endl;
cout << " " << endl;
cout << " " << endl;
}
//Gives player choise whether to keep or discard each loot item
void PlayerChoice()
{
char Answer;
cout << "If you want to keep the item press Y" << endl;
cout << "If you want to discard the item press N" << endl;
cin >> Answer;
while (Answer == 'Y' || Answer == 'y')
{
cout << "Item stored in your bag" << endl;
StoreItem();
return;
}
while (Answer == 'N' || Answer == 'n')
{
cout << "Item was discared from the Treasure Chest" << endl;
return;
}
while (Answer != 'y' || Answer != 'Y' || Answer != 'N' || Answer != 'n')
{
cout << "To decide press Y for accept OR press N for Decline" << endl;
cin >> Answer;
if (Answer == 'Y' || Answer == 'y') {
cout << "Item stored in your bag" << endl;
//store item in bag
return;
}
else (Answer == 'N' || Answer == 'n'); {
cout << "Item was discared from the Treasure Chest" << endl;
return;
}
return;
}
}
// Prints the top of TreasureChest to the screen plus uses Playerchoise() after each item is shown
void PrintTreasureChest()
{
//TreasureChest A;
int j;
for (j = 1; j < 5; j++)
{
cout << "Item " << j << " in your chest is: " << endl;
cout << "Name:" << (TreasureChest().Chest.top()).Name << endl;
cout << "Rarity out of 3: " << (TreasureChest().Chest.top()).Rarity << endl;
cout << "Part of a set: " << (TreasureChest().Chest.top()).Set << endl;
cout << " " << endl;
PlayerChoice(A);
TreasureChest().Chest.pop();
cout << " " << endl;
}
cout << " " << endl;
cout << "This chest is now empty" << endl;
cout << " " << endl;
cout << " " << endl;
cout << "Items in bag: " << endl;
//Game().ShowRucksack();
return;
}
// informs player another chest is coming
void NextChest()
{
cout << "Your next chest contains: " << endl;
cout << " " << endl;
}
// Prints end Game message
void EndGame()
{
cout << " " << endl;
cout << " " << endl;
cout << "You have opened all the Chests, come back soon to the Cave of Treasures" << endl;
cout << " THANKYOU FOR PLAYING" << endl;
}
void StoreItem()
{
int dim = 10;
int P = index(Rucksack, dim);
Rucksack[P] = A.Chest.top();
cout << "Item placed in your Bag: " << Rucksack[P].Name << endl; //B.Rucksack[P].Set << B.Rucksack[P].Rarity << endl;
return;
}
};
int main()
{
Game A;
//TreasureChest A;
PlayerRuckSack B;
//A.StartGame();
srand(time(NULL));
A.PrintTreasureChest();
for (int i = 0; i < 10; i++)
{
cout << B.Rucksack[i].Name << " " << B.Rucksack[i].Set << " " << B.Rucksack[i].Rarity << " " << endl;
}
//A.NextChest();
A.EndGame();
//TreasureChest A;
//PlayerRuckSack B;
//int dim = 10;
//int P = index(B.Rucksack, dim);
//B.Rucksack[P] = A.Chest.top();
//cout << "Item in your Bag " << B.Rucksack[P].Name << B.Rucksack[P].Set << B.Rucksack[P].Rarity << endl;
//cout << P << endl;
return 0;
}
I know why nothing is stored in your Rucksack
int index(Loot x[], int n)
{
int i = 0;
int index;
while (x[i].Name != "empty" && 0 && false && i < n)
{
i++;
index = i;
return index;
}
while (i == n)
{
cout << "BAG FULL" << endl;
}
}
Take a careful look at this line:
while (x[i].Name != "empty" && 0 && false && i < n)
&& 0 && false? Both 0 and false mean this is always false and the Rucksack will be reported as empty no matter what.
Then the code will fall out the bottom of index() without returning a value.
After that program behaviour is undefined because you use a return value that was not returned. Any craziness might come after that.
Fix that and come back with another question if you can't figure out your other problem.
It is currently a bit vague.

Left of '.getDrinkPrice' must have class/struct/union

This is a project from a c++ book I'm learning from.
I keep getting the error
"left of '.getDrinkPrice' must have class/struct/union"
I have tried fixing it but I just keep messing it up further. The visual studio error lists aren't too friendly for new users and this is really bothering me. I obviously don't want the code to be reposted fixed but I do want to be pointed in the right direction.
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
double wallet = 10.00;
int change;
bool isRunning;
bool isChoosing;
int userChoice;
double pricePaid;
class Soda {
private:
string drinkName;
double drinkPrice;
int drinkAmt;
public:
Soda() {
drinkName;
drinkPrice = .75;
drinkAmt = 20;
}
void setDrinkName(string name) {
drinkName = name;
}
string getDrinkName() {
return drinkName;
}
void setDrinkPrice(double price) {
drinkPrice = price;
}
double getDrinkPrice() {
return drinkPrice;
}
void setDrinkAmt(int amt) {
drinkAmt = amt;
}
void setNewDrinkAmount(int amt, int i) {
drinkAmt = amt - i;
}
int getDrinkAmt() {
return drinkAmt;
}
};
// Declares classes
Soda * Cola = new Soda;
Soda * RootBeer = new Soda;
Soda * LemonLime = new Soda;
Soda * Grape = new Soda;
Soda * Cream = new Soda;
Soda *soda = new Soda;
static void init() {
// Pushes the Vending Machine data to the screen
cout << "Drink Name: " << " " << "Drink Cost: " << " " << "Number in machine: \n";
cout << *Cola->getDrinkName << " " << *Cola->getDrinkPrice << " " << *Cola->getDrinkAmt <<endl;
cout << *RootBeer->getDrinkName << " " << *RootBeer->getDrinkPrice << " " << *RootBeer->getDrinkAmt << endl;
cout << *LemonLime->getDrinkName << " " << *LemonLime->getDrinkPrice << " " << *LemonLime->getDrinkAmt << endl;
cout << *Grape->getDrinkName << " " << *Grape->getDrinkPrice << " " << *Grape->getDrinkAmt << endl;
cout << *Cream->getDrinkName << " " << *Cream->getDrinkPrice << " " << *Cream->getDrinkAmt << endl;
cout << endl;
cout << "You have $" << wallet << endl;
cout << endl;
}
void checkValidPurchase(Soda soda, double w, double p) {
double priceOfDrink = soda.getDrinkPrice();
int amountOfDrink = soda.getDrinkAmt;
// If there are enough drinks are in the machine
if (amountOfDrink > 0) {
// If user has enough money
if (w >= priceOfDrink) {
// If amount paid is greater than drink price
if (p > priceOfDrink) {
// Calculate price and change
w = w - p;
double getChange = p - priceOfDrink;
// Return Change
cout << "Paid " << p << " returning " << getChange << endl;
// Update amounts
int j = soda.getDrinkAmt;
soda.setNewDrinkAmount(j, 1);
w = w + getChange;
}
// If amount paid is equal to drink price
else if (p == priceOfDrink) {
// Calculate price
w = w - p;
cout << "Paid " << p << endl;
// Update amounts
int j = soda.getDrinkAmt;
soda.setNewDrinkAmount(j, 1);
}
// If amount paid is less than drink price
else if (p < priceOfDrink) {
// Prompt error and return to drink select
cout << "You did not enter enough money, returning to drink select. \n";
Sleep(3000);
}
}
// If user does not have enough money
else {
cout << "Not enough money in wallet. \n";
double amtNeeded = w + (w - priceOfDrink);
cout << "You have: " << w << " Needed for purchase: " << amtNeeded << endl;
Sleep(3000);
}
}
// If there are not enough drinks in machine
else {
cout << "Not enough of " << soda.getDrinkName << " in machine. \n";
Sleep(3000);
}
}
void sodaMachine() {
// Starts the drink select loop
isChoosing = true;
while (isChoosing) {
// gets user input
cout << "Enter the number of the soda you would like: \n";
cout << "Or to quit, press escape \n";
switch (userChoice) {
case 1:
cout << "Dispensing Cola \n";
cout << "Cost is .75 \n";
cin >> pricePaid;
checkValidPurchase(*Cola, wallet, pricePaid);
isChoosing = false;
break;
case 2:
cout << "Dispensing Root Beer \n";
cout << "Cost is .75 \n";
cin >> pricePaid;
checkValidPurchase(*RootBeer, wallet, pricePaid);
isChoosing = false;
break;
case 3:
cout << "Dispensing Lemon Lime \n";
cout << "Cost is .75 \n";
cin >> pricePaid;
checkValidPurchase(*LemonLime, wallet, pricePaid);
isChoosing = false;
break;
case 4:
cout << "Dispensing Grape \n";
cout << "Cost is .80 \n";
cin >> pricePaid;
checkValidPurchase(*Grape, wallet, pricePaid);
isChoosing = false;
break;
case 5:
cout << "Dispensing Cream \n";
cout << "Cost is .80 \n";
cin >> pricePaid;
checkValidPurchase(*Cream, wallet, pricePaid);
isChoosing = false;
break;
case WM_CHAR:
if (GetAsyncKeyState(VK_ESCAPE)) {
cout << "Exiting program \n";
isRunning = false;
break;
}
default:
isChoosing = false;
break;
}
}
}
int main() {
// Sets class values
Cola->setDrinkName("1. Cola");
RootBeer->setDrinkName("2. RootBeer");
LemonLime->setDrinkName("3. LemonLime");
Grape->setDrinkName("4. Grape");
Grape->setDrinkPrice(.80);
Cream->setDrinkName("5. Cream");
Cream->setDrinkPrice(.80);
isRunning = true;
while (isRunning) {
// Run Program
init();
sodaMachine();
system("cls");
}
return 0;
}
*Cola->getDrinkName is not correct.
Use either Cola->getDrinkName() or (*Cola).getDrinkName() instead; same for all other calls of the same kind in your code.