wrong calculation outputting

closed account (zT4NhbRD)
The wrong calculation is being outputted in my program. It's giving me negative values subtracting from the initialBalance.

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
#include <iostream>
#include <iomanip>

using namespace std;

//Initialize enum variables
enum itemChoice { ITEM_ONE = 1, ITEM_TWO = 2, ITEM_THREE = 3, ITEM_FOUR = 4 };

//Initialize constant character
const char YES = 'Y';

int main ()
{
         cout.precision(2);
         
         double ITEM_ONE_PRICE = 2.00;
         double ITEM_TWO_PRICE = 2.50;
         double ITEM_THREE_PRICE = 1.50;
         double ITEM_FOUR_PRICE = 3.00;  
         
         int counter=0; 
         
         double itemQuantity = 0;
         double finalBalance = 0;
         double choice = 0;       
         double initialBalance = 0;
         double currentBalance = 0;

         char isContinue = YES;
         
         cout << "Please enter your current balance: ";
         cin >> initialBalance;

         do 
         {      
             cout << "Item 1: $2.00\n"
                  << "Item 2: $2.50\n"
                  << "Item 3: $1.50\n"
                  << "Item 4: $3.00\n" 
                  << "\nPlease choose an item from the menu above: ";
             cin >> itemChoice;
                     
             switch (itemChoice)
             {
                 case ITEM_ONE:
                      choice = ITEM_ONE_PRICE;
                      counter++;
                      break;
                 case ITEM_TWO:
                      choice = ITEM_TWO_PRICE;
                      counter++;
                      break;
                 case ITEM_THREE:
                      choice = ITEM_THREE_PRICE;
                      counter++;
                      break;
                 case ITEM_FOUR:
                      choice = ITEM_FOUR_PRICE;
                      counter++;
                      break;
                 default: cout << "That is not a valid option. Please re-enter a choice from the menu above.\n" << endl;
             }                     
             
             cout << "How many would you like to buy? ";
             cin >> itemQuantity;
             
             if (initialBalance < 0 && currentBalance < 0)
             {
                 cout << "Sorry! You do not have enough, goodbye.\n";
                 break;
             }
             else if (counter==1)
             {
                 cout << "\nYour new balance is: $" << fixed << currentBalance << "\n";
             }
             else if (counter >=2)
             {
                  currentBalance = currentBalance - (itemQuantity*choice);
                  cout << "\nYour new balance is: $" << fixed << currentBalance << "\n";
             }
             
             cout << "Woud you like to purchase another item? [Y] for yes and [N] for no: ";
             cin >> isContinue;
            
          } while (tolower(isContinue) == tolower(YES));
          
          finalBalance = currentBalance;
          cout << "\nYour beginning balance was: $" << initialBalance << "\n";
          cout << "You final balance is: $" << setprecision(2) << finalBalance << endl;
             
          system("pause");
          return 0; 
}
Last edited on
Could you please post your entire program, your Do-while loop is not complete.
hi,

Your choice of variable names is confusing you :+)

You prompt for current balance, but store that value into initialBalance. On line 67, you use currentBalance which is zero, so that is why the answer is negative.

Also, choice stores the Item Price, which is misleading.

Line 60 is suspect, it should use an OR operator not AND.

If counter == 1, currentBalance is not updated (assigned a new value)

Why is counter incremented?
What is different about counter >= 2 ? Maybe you meant to calc with initial balance with 1, and current balance with 2 or greater ?

Investigate the -= , += etc operators:

69
70
71
72
73
            else if (counter >=2)
             {
                  currentBalance -=  (itemQuantity*choice);
                  cout << "\nYour new balance is: $" << fixed << currentBalance << "\n";
             }


Investigate using a bool value when the user wants to quit, use this in conjunction with a while loop, instead of a do loop.

Hope all is well :+)


closed account (zT4NhbRD)
@TheIdeasMan

That was the problem, got the variables mixed up. Thanks very much!
Topic archived. No new replies allowed.