Updating the original value

Hey guys,

I have been stuck on this problem I have been getting on my small little project.

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
#include <iostream>
#define OP_1 "You have selected a Cola"
#define OP_2 "You have selected a Fanta"
#define OP_3 "You have selected a Tango"
#define OP_FIN "That drink is not on the list. Pick between 1-3"
using namespace std;

int main(){
int drink;
int nBalance;
float nPrices[3];
//prices of drinks
nPrices[0] = 0.25; // Coke
nPrices[1] = 0.35; // Fanta
nPrices[2] = 0.20; // Tango
//currency held
nBalance = 2;

do
{
  cout << "What drink would you like to select? (zero to stop)" << endl;
  cout << "1 = Cola" << endl << "2 = Fanta" << endl << "3 = Tango" << endl;
  cout << "Your current balance is " << " $ " << nBalance << endl;

  cin >> drink;
  if (drink > 0)
  {
    switch(drink)
    {
    case 1: cout << OP_1 << endl << endl << (nBalance-nPrices[0]) << endl;
    break;
    case 2: cout << OP_2 << endl << endl << (nBalance-nPrices[1]) << endl;
    break;
    case 3: cout << OP_3 << endl << endl << (nBalance-nPrices[2]) << endl;
    break;
    default: cout << OP_FIN << endl;


    }
  }
}
while (drink > 0);

return 0;
}


What is the problem? Well I am trying to deduct the nBalance from the nPrices and update the the remaining value. However it keeps outputting 2 everytime. I have managed to get the argument to work it will deduct and print the remaining value out but then selecting another drink it won't update.

If anyone can help me out it is much appreciated.

Thanks
Here is how to set a value:
x = 3;
You use the =

I don't see anywhere in your code that you're setting the value of anything except right at the start.
int nBalance;

change it to

float nBalance;

integer numbers don't have decimal places, floats and doubles do.
nBalance = 2

is the value I am trying to work with. Sorry if I sound noobish only been learning c++ quite recently.
So here is how to set nBalance to 2.
nBalance = 2;

Here is how to set nBalance to, say, nBalance minus 5
nBalance = nBalance - 5;


Is that enough help? :)
The point Moschops was trying to make is that there is no place in your code where you change the value of nBalance. If you want nBalance to have a different value the next time through the loop, you need to change it. And as TheIdeasMan pointed out if you subtract a fraction (e.g. 0.25) from in int, that won't change the value of the int.
Thank you ever so much! I have now fixed the problem. And I understand where I have gone wrong now.

Last edited on
Topic archived. No new replies allowed.