Original Post
hi guys i am creating a gang based text game to work on until my new book comes. but i have come against a problem. 1) i can't seem to stop a player from purchasing items if the cost is greater than what his money is. here is the main file could you please comment on my coding style as well. and tell me how i could improve :D thanks alot!
#include <iostream>
class game
{
public:
void MainMenu();
void intro();
void BuyEquipement();
void Henchmen();
void items();
void Protection();
private:
void SetStartEquip();
void CheckMoney();
int m_Money;
int m_Dope, m_Heroin, m_Coke;
int m_Henchmen;
int m_Transport;
int m_Weapons;
int m_Business, m_Rate;
int m_Transaction;
int m_Cost;
};
void game::MainMenu()
{
using std::cout;
using std::cin;
using std::endl;
int MainChoice;
SetStartEquip();
cout << "Welcome to streets of NY! " << endl;
cout << endl << " MAIN MENU ";
cout << endl << " 1 - play game ";
cout << endl << " 2 - about ";
cout << endl << " 3 - quit ";
cout << endl;
cin >> MainChoice;
switch (MainChoice)
{
case 1:
intro();
break;
case 2:
cout << "This text based game is a test of my coding ability" << endl;
break;
case 3:
cin.get();
break;
default:
cout << MainChoice << ": is an invalid option! ";
break;
}
}
void game::SetStartEquip()
{
m_Money = 10000;
m_Dope = 0;
m_Heroin = 0;
m_Coke = 0;
m_Henchmen = 0;
m_Transport = 0;
m_Weapons = 0;
m_Rate = 150;
m_Business = 0;
}
void game::intro()
{
using std::cout;
using std::cin;
using std::endl;
int GameMenuChoice;
cout << endl;
cout << "Game Menu! " << endl;
cout << " 1 - buy equimepent" << endl;
cout << " 2 - henchmen " << endl;
cout << " 3 - Protection money " << endl;
cout << " 4 - show items " << endl;
cin >> GameMenuChoice;
switch (GameMenuChoice)
{
case 1:
BuyEquipement();
break;
case 2:
Henchmen();
break;
case 3:
Protection();
break;
case 4:
items();
break;
default:
cout << GameMenuChoice << ": is not a valid option! ";
break;
}
}
void game::BuyEquipement()
{
using std::cout;
using std::cin;
using std::endl;
int DrugChoice;
int DrugAmount;
int GunAmount;
int BuyMenuChoice;
int NoOfPlanes;
cout << "Buy Equipement menu! " << endl;
cout << "1 - Buy Drugs" << endl;
cout << "2 - Buy Weapons " << endl;
cout << "3 - Buy Planes " << endl;
cin >> BuyMenuChoice;
switch(BuyMenuChoice)
{
case 1:
cout << endl << "Please enter the type of drugs you wish to buy! " << endl;
cout << "1 - Dope" << endl << "2 - Heroin " << endl << "3 - cocaine" << endl;
cin >> DrugChoice;
switch (DrugChoice)
{
case 1:
cout << "How many units of Dope do you wish to buy? " << endl;
cin >> DrugAmount;
m_Cost = 100 * DrugAmount;
CheckMoney();
if(m_Transaction = 1)
{
m_Dope += DrugAmount;
m_Money -= m_Cost;
}
else if(m_Transaction = 0)
{
cout << "Transaction Unsuccesful : Invalid funds";
}
break;
case 2:
cout << "How many units of Heroin do you wish to buy? " << endl;
cin >> DrugAmount;
m_Cost = 200 * DrugAmount;
CheckMoney();
if(m_Transaction = 1)
{
m_Heroin += DrugAmount;
m_Money -= m_Cost;
}
else if(m_Transaction = 0)
{
cout << "Transaction Unsuccesful : Invalid funds";
}
break;
case 3:
cout << "How many units of Cocaine do you wish to buy? " << endl;
cin >> DrugAmount;
m_Cost = 150 * DrugAmount;
CheckMoney();
if(m_Transaction = 1)
{
m_Coke += DrugAmount;
m_Money -= m_Cost;
}
else if(m_Transaction = 0)
{
cout << "Transaction Unsuccesful : Invalid funds";
}
break;
default:
cout << DrugChoice << ": is an invalid option!"<< endl;
break;
}
break;
case 2:
cout << endl << "Enter how many weapons you wish to purchase: ";
cin >> GunAmount;
m_Cost = (100 * GunAmount);
CheckMoney();
if(m_Transaction = 1)
{
m_Weapons += GunAmount;
m_Money -= m_Cost;
}
else if(m_Transaction = 0)
{
cout << "Transaction Unsuccesful : Invalid funds";
}
break;
case 3:
cout << "Enter how many planes you wish to purchase: ";
cin >> NoOfPlanes;
m_Cost = (5000 * NoOfPlanes);
CheckMoney();
if(m_Transaction = 1)
{
m_Transport += NoOfPlanes;
m_Money -= m_Cost;
}
else if(m_Transaction = 0)
{
cout << "Transaction Unsuccesful : Invalid funds";
}
break;
default:
cout << "Invalid selection";
break;
}
intro();
}
void game::Henchmen()
{
using std::cout;
using std::cin;
using std::endl;
int NoOfHench;
int HenchmenMenu;
cout << "Henchmen Menu" << endl;
cout << "1 - hire henchmen" << endl;
cout << "2 - collect from henchmen" << endl;
cin >> HenchmenMenu;
switch(HenchmenMenu)
{
case 1:
cout << "Henchmen cost $30 to hire each" << endl;
cout << "Enter how many henchmen you wish to hire: ";
cin >> NoOfHench;
m_Cost = (30 * NoOfHench);
CheckMoney();
if(m_Transaction = 1)
{
m_Henchmen += NoOfHench;
m_Money -= m_Cost;
}
else if(m_Transaction = 0)
{
cout << "Transaction Unsuccesful : Invalid funds";
}
break;
case 2:
cout << "You collected: " << (m_Henchmen * 10);
break;
default:
cout << "you entered an invalid option";
break;
}
intro();
}
void game::items()
{
using std::cout;
using std::cin;
using std::endl;
cout << endl;
cout << "Money: " << m_Money << endl;
cout << "Cocaine units: " << m_Coke << endl;
cout << "Heroin units: " << m_Heroin << endl;
cout << "Dope units: " << m_Dope << endl;
cout << "Number of Henchmen: " << m_Henchmen << endl;
cout << "Number of Planes: " << m_Transport << endl;
cout << "Number of Weapons: " << m_Weapons << endl;
intro();
}
void game::Protection()
{
using std::cout;
using std::cin;
using std::endl;
int ProtectionChoice;
int NoOfBusinesses;
int money;
cout << "Protection Menu " << endl;
cout << "1 - Increase amount of protected businesses" << endl;
cout << "2 - Collect money from protected businesses" << endl;
cout << "3 - change protection rates" << endl;
cin >> ProtectionChoice;
switch (ProtectionChoice)
{
case 1:
cout << "Enter how many new businesses you wish to extort: ";
cin >> NoOfBusinesses;
m_Business += NoOfBusinesses;
cout << "Businesses extorted: " << m_Business << endl;
break;
case 2:
cout << "You collected: " << (m_Business * m_Rate) << endl;
m_Money += (m_Business * m_Rate);
break;
case 3:
cout << "Current rate: " << m_Rate << endl;
cout << "Enter New Rate: ";
cin >> m_Rate;
break;
default:
cout << "Invalid option" << endl;
break;
}
intro();
}
void game::CheckMoney()
{
using std::cout;
if (m_Money < m_Cost)
{
m_Transaction = 0;
}
else if (m_Money >= m_Cost)
{
m_Transaction = 1;
}
}