In C++, my program is not accepting user input when prompted. What is causing this issue?
#define INVENTORY
#include
#include
#include "Console.h"
#include "Utility.h"
#ifdef INVENTORY
#include "Inventory.cpp"
#endif
#include "ShopUtils.cpp"
using namespace std;
int main()
{
#ifdef INVENTORY
Item HealthPotion;
HealthPotion.SetName("Health Potion");
HealthPotion.SetCost(10);
Item MagicPotion("Magic Potion", 15);
Item IronDagger("Iron Dagger", 30);
Item WoodenClub("Wooden Club", 20);
Item DaedricHelm("Daedric Helm", 120);
Item MageRobe("Mage Robe", 50);
Item AkaviriKatana("Akaviri Katana", 200);
Item WabbaJack("Wabbajack", 500);
vector PlayerStarterItems = { HealthPotion, HealthPotion, MagicPotion, WoodenClub };
vector StoreStartingItems = { HealthPotion, MagicPotion, MagicPotion, IronDagger,
DaedricHelm, MageRobe, AkaviriKatana, WabbaJack };
#endif
// TODO: Define an Inventory object for the player, and an Inventory object for the store.
Inventory playerInventory;
Inventory storeInventory;
/* TODO: Prompt the user for their name
* ======================================
* Ask the user for their name and define a string for the
* name. Store their input in the string. If they entered
* an empty string (e.g. "") assign them a default name.
*/
string name;
cout << "Please enter your name: ";
cin >> name;
if (name.empty()) {
name = "Default Name";
}
// TODO: Use the SetGold method to give the player inventory 200 Gold.
playerInventory.SetGold(200);
/* TODO: Call the AddItem method
* ================================
* Use the AddItem method to add each item in the
* PlayerStartingItems array to the player's inventory.
*/
bool isItemAdded = false;
for (size_t i = 0; i < PlayerStarterItems.size(); ++i) {
isItemAdded = playerInventory.AddItem(PlayerStarterItems[i]);
if (!isItemAdded) {
break;
}
}
// TODO: Use the SetGold method to give the store inventory 350 Gold.
storeInventory.SetGold(350);
/* TODO: Call the AddItem method
* ================================
* Use the AddItem method to add each item in the
* StoreStartingItems array to the store's inventory.
*/
while (true)
{
int sel = 0;
bool validInput = false;
while (!validInput)
{
Console::Clear();
/* TODO: Call the ShopUtils::ShowInventories method
* =================================================
* Call the ShowInventories method located within the
* ShopUtils class and pass in the player's name,
* the player's inventory and the store's inventory.
*/
ShopUtils::ShowInventories(name, playerInventory, storeInventory);
Console::SetCursorPosition(5, 18);
const std::string menu = "What would you like to do?
1) Buy
2) Sell
3) Leave
_";
Console::Write(menu);
sel = Utility::ReadInt();
} while (!Utility::IsReadGood() || (sel < 1 || sel > 3));
if (3 == sel)
break;
bool doBuy = false;
if (1 == sel)
{
doBuy = true;
}
/* TODO: Call the ShopUtils::DoTransaction method
* =============================================
* Call the DoTransaction method locate within the
* ShopUtils class and pass it the player's name,
* the player's inventory, the store's inventory
* and the doBuy variable.
*/
ShopUtils::DoTransaction(name, playerInventory, storeInventory, doBuy);
}
Console::Clear();
Utility::WriteCentered("Thanks! Come back again!");
Utility::WaitForEnterKey();
}
CUsersethanDownloadsPi X
e's inventory:
The Store's inventory:
Item Name Health Potion Health Potion Magic Potion Wooden Club
Item Cost 10 10 15 20
Item Name
Item Cost
Gold on hand:
350
Gold on hand:
200
What would you like to do? 1 Buy 2) Sell 3) Leave