Okay so im using a global integer variable to display the amount of cash you have in this "shopkeeper program" im workn on. when i subtract a certain amount off of the amount of cash you actually have, the console will display the answer along side with the original amount, when in fact i just want the remainding answer to display. Is this because im using a global and changing it within a void method? The below is the entire code so far, somebody show me where im going wrong please.
#include <iostream>
#include <string>
//The program will demonstrate the use of arrays by simulating a bartering experience.
//This will begin with a menu with three simple choices, allowing the player to buy and sell
//items and also allowing you to talk to the shop keeper. The player will then input a character
//variable of A,B or C if otherwise an error message is displayed, stating you must input a valid choice.
//Upon selecting buy items you will be displayed with an array of items that are available for purchase.
//These items will all be given numbers to identify them for when you input which item number you wish to
//purchase and add to your items. To sell your items, you will be displayed with all your current items
//in your array, also with numbers. You, the player will then input that numbered item you wish to sell.
//Both items you wish to buy and sell will come with prices you can purchase or sell. There will also
//be a global variable for the amount of cash you, the player has.
usingnamespace std;
int cash = 100; //Global integer
void cmd_text();
void cmd_code();
void buy_items();
void sell_items();
int main()
{
cout << "\n\t\t\t\tShopkeeper Program\n\t\t\t\t-------------------\n";
cmd_text();
int pause;
cin >> pause;
return 0;
}
void cmd_text()
{
cout << "\n\t\t\t ___________________________";
cout << "\n\t\t\t | |";
cout << "\n\t\t\t |___________________________|";
cout << "\n\t\t\t | |";
cout << "\n\t\t\t | [A] Buy Items |";
cout << "\n\t\t\t | |";
cout << "\n\t\t\t | [B] Sell Items |";
cout << "\n\t\t\t | |";
cout << "\n\t\t\t | [C] Talk |";
cout << "\n\t\t\t |___________________________|";
cout << "\n\n\t\t\t [Select]: ";
cmd_code();
}
void cmd_code()
{
char options; //Pay close attention here! not using integer variable types in the switch!
cin >> options;
switch(options)
{
case'A':
buy_items();
break;
case'B':
sell_items();
break;
case'C':
break;
default:
cout << "\n\n\t\t\t Invalid Choice! Please enter an actual option!\n\n\t\t\t [Select]: ";
cmd_code();
break;
}
}
void buy_items()
{
cout << "\n\n\n\t\t\t |--------------------|\n\t\t";
cout << "\t BUY\n\t\t";
cout << "\t |--------------------|\n\t\t";
cout << "\n\t\t\t______________________________\n";
string shopInventory[5];
int shopItem = 0;
shopInventory[shopItem++] = "\n\t\t\tShoes\t\t$20\t[1]\n";
shopInventory[shopItem++] = "\t\t\tSocks\t\t$5\t[2]\n";
shopInventory[shopItem++] = "\t\t\tGuitar\t\t$75\t[3]\n";
shopInventory[shopItem++] = "\t\t\tHat\t\t$12\t[4]\n";
shopInventory[shopItem++] = "\t\t\tPlant\t\t$8\t[5]\n";
for (int i = 0; i < shopItem; ++i)
{
cout << shopInventory[i]; // The for loop actually display's the arrays elements.
}
cout << "\n\t\t\t______________________________\n";
cout << "\n\n\t\t\tWhich item would you like to purchase?";
cout << "\n\n\t\t\tCash: $" << cash << "";
cout << "\n\t\t\tEnter: ";
int choice;
cin >> choice;
if(choice == 1)
{
cout << cash - 20 << cash;
}
}
void sell_items()
{
int item = 0;
constint MAX_ITEMS = 10;
string inventory[MAX_ITEMS];
inventory[item++] = "\n\t\t\tKnife\t\t$35\t[1]\n"; //array element 0
inventory[item++] = "\t\t\tMarbles\t\t$3\t[2]\n"; //array element 1
cout << "\n\n\n\t\t\t |--------------------|\n\t\t";
cout << "\t SELL\n\t\t";
cout << "\t |--------------------|\n\t\t";
cout << "\n\t\t\t______________________________\n";
for (int i = 0; i < item; ++i)
{
cout << inventory[i];
}
cout << "\n\t\t\t______________________________\n";
cout << "\n\n\t\t\tWhich item would you like to sell?";
cout << "\n\n\t\t\tCash: $" << cash << "";
cout << "\n\t\t\tEnter: ";
int choice;
cin >> choice;
}
You have cout << cash - 20 << cash;
This displays the value of (cash-20) but it doesn't change the value of cash and then you display the cash again.
Make it two lines. One that substracts 20 from the cash and another that prints the cash.
for (int i = 0; i < shopItem; ++i)
{
cout << shopInventory[i]; // The for loop actually display's the arrays elements.
}
cout << "\n\t\t\t______________________________\n";
cout << "\n\n\t\t\tWhich item would you like to purchase?";
cout << "\n\n\t\t\tCash: $" << cash << ""; //**Print the current cash status (global variable)
cout << "\n\t\t\tEnter: ";
int choice;
cin >> choice;
if(choice == 1)
{
cout << cash - 20 << cash ;//** MISTAKE IS HERE **
}
You have not actually changed the value of the cash variable. What you have done is calculated a new value which is cash - 20, but you have not saved it.
To change the value of cash - here are some ways to do it:
1 2 3 4 5
if(choice == 1)
{
cash = cash - 20; //subtract 20 from current cash value and resave the new cash value
cout << cash ; //cash remaining
}
OR
1 2 3 4 5
if(choice == 1)
{
cout << ( cash = cash-20);
}
Note you can also use the -= operator
so instead of cash = cash - 20 you can say cash -= 20