Changing Boolean
Dec 8, 2012 at 6:39pm UTC
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:
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
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:
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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
#include <iostream>
#include <String>
#include <cstdlib>
#include <ctime>
#include <limits>
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.
Dec 8, 2012 at 6:47pm UTC
1 2 3 4 5 6 7
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:
1 2 3 4 5 6 7
if (ex=0) {
temppay=rate3_1*tdif*.5;
}
else if (ex=1) {
temppay=(rate3_1*8+rate3_2*tdif)*.5;
}
Dec 8, 2012 at 6:48pm UTC
1 2 3 4 5
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 Dec 8, 2012 at 6:48pm UTC
Dec 8, 2012 at 6:49pm UTC
Ah, right, thank you all, I'm getting rusty...
Topic archived. No new replies allowed.