### Changing Boolean

Ok, so I made a simple program to calculate pay for employees, but booleans behave strangely, as they somehow automatically change values.

This problem is found in these two else if statements:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` ``````else if (10<=location && location<=19) { if (8<=tdif) { tdif=tdif-8; ex=1; } if (ex=0) { temppay=rate2_1*tdif*.5; } else if (ex=1) { temppay=(rate2_1*8+rate2_2*tdif)*.5; } switch (index) { case 1: pay1=temppay; case 2: pay2=temppay; case 3: pay3=temppay; case 4: pay4=temppay; } } else if (20<=location && location<=29) { if (8<=tdif) { tdif=tdif-8; ex=1; } if (ex=0) { temppay=rate3_1*tdif*.5; } else if (ex=1) { temppay=(rate3_1*8+rate3_2*tdif)*.5; } switch (index) { case 1: pay1=temppay; case 2: pay2=temppay; case 3: pay3=temppay; case 4: pay4=temppay; }``````

Here is the main code:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256`` ``````#include #include #include #include #include using namespace std; int main (int argc, char *argv[]) //Main (Layer 1) Start { //Definition string getkey; int location=0; int tsvar=0; int tevar=0; int tdif=0; int rate1_1=10; int rate2_1=8; int rate2_2=12; int rate3_1=12; int rate3_2=24; int index=0; double temppay=0; double pay1=0; double pay2=0; double pay3=0; double pay4=0; double paytotal=0; char timestart='/'; char timeend='/'; bool ex=0; cout << "Please maximize your window, and press [Enter] to continue." << endl; getline (cin, getkey); system("cls"); cout << "Welcome to the ACSL Amusement Park Paycheck Calculator." << endl; cout << endl; cout << "Please enter your codes in this format: '(location) (start time) (end time)'." << endl; cout << "In other words, separated by spaces. For example '5 9 H'." << endl; cout << endl; cout << "Press [Enter] to continue." << endl; getline (cin, getkey); system("cls"); cout << "Please enter your codes." << endl; for (index=1; index<=4; index++) //Input (Layer 2) Start { ex=0; cin >> location >> timestart >> timeend; switch (timestart) //Calculation (Layer 3) Start { case '1': tsvar=1; break; case '2': tsvar=2; break; case '3': tsvar=3; break; case '4': tsvar=4; break; case '5': tsvar=5; break; case '6': tsvar=6; break; case '7': tsvar=7; break; case '8': tsvar=8; break; case '9': tsvar=9; break; case 'A': tsvar=10; break; case 'B': tsvar=11; break; case 'C': tsvar=12; break; case 'D': tsvar=13; break; case 'E': tsvar=14; break; case 'F': tsvar=15; break; case 'G': tsvar=16; break; case 'H': tsvar=17; break; } switch (timeend) { case '1': tevar=1; break; case '2': tevar=2; break; case '3': tevar=3; break; case '4': tevar=4; break; case '5': tevar=5; break; case '6': tevar=6; break; case '7': tevar=7; break; case '8': tevar=8; break; case '9': tevar=9; break; case 'A': tevar=10; break; case 'B': tevar=11; break; case 'C': tevar=12; break; case 'D': tevar=13; break; case 'E': tevar=14; break; case 'F': tevar=15; break; case 'G': tevar=16; break; case 'H': tevar=17; break; } tdif=tevar-tsvar; if (1<=location && location<=9) { temppay=rate1_1*tdif*.5; switch (index) { case 1: pay1=temppay; case 2: pay2=temppay; case 3: pay3=temppay; case 4: pay4=temppay; } } else if (10<=location && location<=19) { if (8<=tdif) { tdif=tdif-8; ex=1; } if (ex=0) { temppay=rate2_1*tdif*.5; } else if (ex=1) { temppay=(rate2_1*8+rate2_2*tdif)*.5; } switch (index) { case 1: pay1=temppay; case 2: pay2=temppay; case 3: pay3=temppay; case 4: pay4=temppay; } } else if (20<=location && location<=29) { if (8<=tdif) { tdif=tdif-8; ex=1; } if (ex=0) { temppay=rate3_1*tdif*.5; } else if (ex=1) { temppay=(rate3_1*8+rate3_2*tdif)*.5; } switch (index) { case 1: pay1=temppay; case 2: pay2=temppay; case 3: pay3=temppay; case 4: pay4=temppay; } } //Calculation (Layer 3) End } //Input (Layer 2) End system("cls"); paytotal=pay1+pay2+pay3+pay4; cout << "1. \$" << pay1 << endl; cout << "2. \$" << pay2 << endl; cout << "3. \$" << pay3 << endl; cout << "4. \$" << pay4 << endl; cout << "5. \$" << paytotal << endl; getline (cin, getkey); getline (cin, getkey); return 0; } //Main (Layer 1) End ``````

If you find a solution, please tell me, and thank you.
 ``1234567`` `````` if (ex=0) { temppay=rate2_1*tdif*.5; } else if (ex=1) { temppay=(rate2_1*8+rate2_2*tdif)*.5; }``````

Those if statements are assignment operators. I believe you want comparision operators. Same here:

 ``1234567`` `````` if (ex=0) { temppay=rate3_1*tdif*.5; } else if (ex=1) { temppay=(rate3_1*8+rate3_2*tdif)*.5; }``````
 ``12345`` ``````if (ex=0) { temppay=rate2_1*tdif*.5; } else if (ex=1) {``````

Problem in the conditions of the if statements. If you want to check for equality, you have to use the == operator. The operator you use here (=) assigns a value to the variable. Simply change the ='s to =='s.
Last edited on
Ah, right, thank you all, I'm getting rusty...
Topic archived. No new replies allowed.