my problem subracting from a global integer

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.





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#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. 




using namespace 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;
    const int 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;
    
}










Last edited on
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.
1
2
cash -= 20;
cout << cash;


Please use [code][/code] tags for your code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
Topic archived. No new replies allowed.