Unable to update initialized value.

Hi,I am having an issue with updating the initialized value MoneyTotal.

The initial value of MoneyTotal is 1000, which is displayed as desired. The issue, however, is once the user inputs their char value and the amount they wish to bet, the amount that is supposed to result from this is not updated into double MoneyTotal.
Also, when I was checking to see if the EvenBet was subtracted correctly from MoneyTotal with cout << MoneyTotal << endl; nothing was displayed on the screen.

What may be the issue? Why is MoneyTotal not being overridden by the new value?
Have I set up the do while and case statements incorrectly?

Any suggestions? :/

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



#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
#include <iomanip> 


using namespace std;


	const char QUIT = 'q';
	const int MAX_NUMBER = 37;
    const double DOUBLE_ZERO = MAX_NUMBER;
	const int DEFAULT_PLAYER_NUMBER = MAX_NUMBER + 1;
	const double EVEN_ODD_PAYOUT = 1;
	const double STRAIGHT_UP_PAYOUT = 35;
	char LetterChoice;

int SUBRouletteNumSelection;
int seed;

float EvenBet;
float StraightUpBet;
    string NameInput;

int main()
{

    cout << "What is your name? ";
    getline (cin, NameInput);

	do {
        double MoneyTotal;

        cout<< fixed << setprecision(2) << endl;

        cout << endl << endl;
        cout << "Roulette 1.0 " << endl << endl;
        MoneyTotal = 1000;
	    cout << NameInput << ", you have $" << MoneyTotal << "." << endl << endl;
		// print out the menu
		cout << "Select from the following options: " << endl << endl;
                cout << "(a) Even Bet" << endl;
                cout << "(b) Odd Bet" << endl;
                cout << "(c) Straight Up Bet" << endl;
                cout << "(d) Spin the Wheel" << endl;
                cout << "(q) Quit" << endl << endl;

		cout << "Enter the letter of your choice: ";
		// get the player's menu option
		cin >> LetterChoice;
		cout << endl;

		int even = 0;

		int OddBet = 0;
		int odd = 0;

		switch(LetterChoice){

		   case 'a':
			   cout << "Enter an even bet:  ";
			   cin >> EvenBet; 
			   cout << endl;
                    if((static_cast<int>(EvenBet) % 2 == 0) && MoneyTotal>EvenBet>0) 
                        {MoneyTotal -= EvenBet ; 
                        cout << "$" << MoneyTotal << endl;}

                        else if ((static_cast<int>(EvenBet) % 2) !=0 or EvenBet <0)
                            {cout << "Invalid bet! Try Again.\n";}
                            else if (MoneyTotal<EvenBet)
                            {
                                cout << "Cannot Bet that much! Try again."<<endl;
                            }
                    break;

		   case 'b':
			   cout << "Enter and odd bet: ";
			   cin >> OddBet;
			   cout << endl;
                    if((OddBet % 2) == 'b' && MoneyTotal>OddBet>0)
                        {odd = odd + OddBet;}
                        else if ((OddBet % 2) ==0 or OddBet <0)
                            {cout << "Invalid bet! Try Again. \n";}

                    break;

          case 'c':
			   cout << "Enter a straight up bet:  ";
			   cin >> StraightUpBet;
                    if(MoneyTotal>EvenBet>0)
                        {MoneyTotal -= StraightUpBet ;
                        cout << "$" << MoneyTotal << endl;}
                        else if (MoneyTotal<StraightUpBet)
                            {
                                cout << "Cannot Bet that much! Try again."<<endl;
                            }
			   cout << "Enter a number to bet on (Enter \"37\" for \"00\"): ";
			   cin >> SUBRouletteNumSelection;
                    if (SUBRouletteNumSelection > DOUBLE_ZERO or SUBRouletteNumSelection<0)
                        cout << "You entered an invalid number! Enter another number: ";
                        cin >> SUBRouletteNumSelection;

                    break;
		   case 'd':
		     // seed = static_cast<int>(time(NULL));
		      // srand(seed);

             
               //int n = 1.0*rand() / MAX_NUMBER;
              // cout << "The number is " << (rand()%37)+1 << endl;
			   cout << endl;

                           break;
		   case 'q':
			   // do nothing as the condition (option != QUIT) will now be false
			   break;
		   default:
		  	  cout << "Invalid letter choice! Try again.";
			  break;

		}
   }
   while(LetterChoice != QUIT);

   cout << "Thanks for using the sample menu!" << endl << endl;

   return 0;
}

Last edited on
1
2
if((EvenBet % 2) == 'a' && EvenBet>0)
    {MoneyTotal -= EvenBet ;


EvenBet % 2) == 'a' is always false, so MoneyTotal will never be decreased.
The reason for the "mysterious" behavior is due to the fact that the if-control structure on line 58 never executes. What do you think this does : if((EvenBet % 2) == 'a' && EvenBet>0)?
Would it be correct to state
1
2
3
if((EvenBet % 2 == 0) && EvenBet>0)
                        {MoneyTotal -= EvenBet ;
                        cout << "$" << MoneyTotal << endl;} 
?
A value was finally returned after subtracting the EvenBet from MoneyTotal, however, the new amount did not replace the initialized value at
1
2
MoneyTotal = 1000;
	    cout << NameInput << ", you have $" << MoneyTotal << "." << endl << endl
.

I want the program to display their monetary loss instead of displaying the same initial amount.
Last edited on
Even after modifying MoneyTotal, why won't my program forget the old value and remember the new value?
[code]MoneyTotal = 1000;
	    cout << NameInput << ", you have $" << MoneyTotal << "." << endl << endl[/code]
Don't you see the problem here? You are setting MoneyTotal at the beginning of each iteration to 1000. So all previous changes would be lost
I initialized MoneyTotal under type double with value 1000. double MoneyTotal = 1000;
Why is it that in other programs a variable's value can be overwritten? Forgive me if this was a foolish question, I am in the process of learning. :/
I thought not declaring MoneyTotal as a const would allow for an update at each iteration.

However, if setting MoneyTotal at the beginning really is the issue, what can i do to fix this error? I want to start at an initial value of 1000.

And, thank you for the response!


MoneyTotal is loop local variable. That means that at the end of each iteration in is destroyed and created again at the beginning. Simplified example:
1
2
3
4
5
while(true) {
    double money = 1000;
    std::cout << money;
    money = 1;
}
I got! Miini, you're awesome! Thank you so much!!

Thanks everyone!
Topic archived. No new replies allowed.