Variable not being subtracted properly. (Very Serious)

OK, well, I have a big big BIG problem here. A very big one. The problem is, whenever I try to subtract a variable with the same number of it's own (a = a - 100 and a has been set to be a = 100), it subtracts only one number.
The code for the program I'm developing is too huge, I can't show it here.
Still, if we use the 'a' example, here is how it would look on final display:
"999"
The exact value I used was 10 - 10 and a 9 is all that comes. Seriously, SOMEBODY HELP!!! I HAVEN'T GOT A SINGLE IDEA WHY THIS IS HAPPENING!!!
You could at least post the code that has the statement you're inquiring about.

1
2
3
int a = 100;

a -= 100;


^is that what you had in mind?

Last edited on
How is a defined? is it int, float, double, etc?
You could at least post the code that has the statement you're inquiring about.

1
2
3
int a = 100;

a -= 100;


^is that what you had in mind?


Probably. And no, I can't post the code. IT'S LIKE 1100 lines. The char limit would not fit in.
How is a defined? is it int, float, double, etc?


As an integer.
We don't mean posting your ENTIRE code. Just the line/statement/chunk/function/section that has what you're asking about.

Otherwise, the community would just be guessing at what you're trying to do or what you've done so far.

If a is used throughout the program and you can't isolate it to a shorter chunk of code that can be posted here, try using a debugger and tracking the value of a as the program progresses. Maybe it's getting changed somewhere unexpectedly.

We don't mean posting your ENTIRE code. Just the line/statement/chunk/function/section that has what you're asking about.

Otherwise, the community would just be guessing at what you're trying to do or what you've done so far.


Oh well, though I am not sure if it's caused by the section:
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
//The stuff used in this area:
int credit;
double food;
double water;
int material;

int incredit;
int infood;
int inwater;
int inmaterial;

/////////////////////////////////////

struct building{
    int cost;
    int consumption;

    int prfood;
    int prwater;
    int prcredit;
    int prmaterial;
    int prpower;
};

void structmanage2()
{
    ///LV-1 STRUCTURES///
    ///FARM///
    building farm;
    building *farmptr;
    farmptr = &farm;

    farm.prfood = 5;
    farm.prwater = 0;
    farm.prcredit = 0;
    farm.prmaterial = 1;
    farm.prpower = 0;

    farm.cost = 500;
    farm.consumption = 0;

    ///STARDOCK///
    building stardock;
    building *stardockptr;
    stardockptr = &stardock;

    stardock.prfood = 2;
    stardock.prwater = 2;
    stardock.prcredit = 75;
    stardock.prmaterial = 10;
    stardock.prpower = 0;

    stardock.cost = 850;
    stardock.consumption = 10;

    ///MINE///
    building mine;
    building *mineptr;
    mineptr = &mine;

    mine.prfood = 0;
    mine.prwater = 0;
    mine.prcredit = 10;
    mine.prmaterial = 50;
    mine.prpower = 0;

    mine.cost = 465;
    mine.consumption = 1;

    ///DESALINATION PLANT///
    building desalination;
    building *desalinationptr;
    desalinationptr = &desalination;

    desalination.prfood = 0;
    desalination.prwater = 10;
    desalination.prcredit = 0;
    desalination.prmaterial = 3;
    desalination.prpower = 0;

    desalination.cost = 500;
    desalination.consumption = 4;

    ///MARKET///
    building market;
    building *marketptr;
    marketptr = &market;

    market.prfood = 1;
    market.prwater = 0;
    market.prcredit = 100;
    market.prmaterial = 1;
    market.prpower = 0;

    market.cost = 675;
    market.consumption = 0;

    ///SMALL HYPER-WINDMILL///
    building smallwindmill;
    building *smallwindmillptr;
    smallwindmillptr = &smallwindmill;

    smallwindmill.prfood = 0;
    smallwindmill.prwater = 0;
    smallwindmill.prcredit = 0;
    smallwindmill.prmaterial = 0;
    smallwindmill.prpower = 10;

    smallwindmill.cost = 1000;
    smallwindmill.consumption = 0;

    cout<<"\n";
    cout<<"[BUY BUILDINGS][<-69][96-> (UD)]\n";
    cout<<" [1][Buy Farm](-500C)(+15F +1M)\n";
    cout<<" [2][Buy Stardock](-850C -5E)(+2F +2W +75C +10M)\n";
    cout<<" [3][Buy Mine](-465C -1E)(+10C +50M)\n";
    cout<<" [4][Buy Desalination Plant](-500C)(+10W +3M)\n";
    cout<<" [5][Buy Market](-675C)(+1F +100C +1M)\n";
    cout<<" [6][Buy Small Hyper-WindMill](-1000C)(+10E)\n";
    cout<<">>> ";
    cin>>input;
    switch (input){
case 1:
    cout<<"\n";
    if (credit < farmptr->cost){
        cout<<"[!] You don't have enough credits to buy a farm!\n";
        cin.get();
        cin.get();
        structmanage2();
        break;
    }
    else if (credit == farmptr->cost or credit > farmptr->cost){
        cout<<"[$] You have now bought a farm.\n";
        credit = credit - farmptr->cost;
        infood = infood + farmptr->prfood;
        inmaterial = inmaterial + farmptr->prmaterial;
        cin.get();
        cin.get();
        structmanage2();
        break;
    }
case 2:
    cout<<"\n";
    if (credit < stardockptr->cost and power < stardockptr->consumption or credit < stardockptr->cost or power < stardockptr->consumption){
        cout<< "[!] You don't have enough electricity or credits to buy a stardock!\n";
        cin.get();
        cin.get();
        structmanage2();
        break;
    }
    else if (credit > stardockptr->cost and power > stardockptr->consumption or credit == stardockptr->cost and power == stardockptr->consumption){
        cout<<"[$] You have now bought a stardock.\n";
        credit = credit - stardockptr->cost;
        power = power - stardockptr->consumption;
        infood = infood + stardockptr->prfood;
        inwater = inwater + stardockptr->prwater;
        incredit = incredit + stardockptr->prcredit;
        inmaterial = inmaterial + stardockptr->prmaterial;
        cin.get();
        cin.get();
        structmanage2();
        break;
    }
case 3:
    cout<<"\n";
    if (credit < mineptr->cost and power < mineptr->consumption or credit < mineptr->cost or power < mineptr->consumption){
        cout<<"[!] You don't have enough electricity or credits to buy a mine!\n";
        cin.get();
        cin.get();
        structmanage2();
        break;
    }
    else if (credit == mineptr->cost or credit > mineptr->cost){
        cout<<"[$] You have now bought a stardock.\n";
        credit = credit - mineptr->cost;
        power = power - mineptr->consumption;
        incredit = incredit + mineptr->prcredit;
        inmaterial = inmaterial + mineptr->prmaterial;
        cin.get();
        cin.get();
        structmanage2();
        break;
    }
case 4:
    cout<<"\n";
    if (credit < desalinationptr->cost and power < desalinationptr->consumption or credit < desalinationptr->cost or power < desalinationptr->consumption){
        cout<<"[!] You don't have enough electricity or credits to buy a desalination plant!\n";
        cin.get();
        cin.get();
        structmanage2();
        break;
    }
    else if (credit > desalinationptr->cost and power > desalinationptr->consumption or credit == desalinationptr->cost and power == desalinationptr->consumption){
        cout<<"[$] You have now bought a desalination plant.\n";
        credit = credit - desalinationptr->cost;
        power = power - desalinationptr->consumption;
        inwater = inwater + desalinationptr->prwater;
        inmaterial = inmaterial + desalinationptr->prmaterial;
        cin.get();
        cin.get();
        structmanage2();
        break;
    }
case 5:
    cout<<"\n";
    if (credit < marketptr->cost){
        cout<<"[!] You don't have enough credits to buy a market!\n";
        cin.get();
        cin.get();
        structmanage2();
        break;
    }
    else if (credit > marketptr->cost or credit == marketptr->cost){
        cout<<"[$] You have now bought a market.\n";
        credit = credit - marketptr->cost;
        infood = infood + marketptr->prfood;
        incredit = incredit + marketptr->prcredit;
        inmaterial = inmaterial + marketptr->prmaterial;
        cin.get();
        cin.get();
        structmanage2();
        break;
    }
case 6:
    cout<<"\n";
    if (credit < smallwindmillptr->cost){
        cout<<"[!] You don't have enough credits to buy a small hyper windmill!\n";
        cin.get();
        cin.get();
        structmanage2();
        break;
    }
    else if (credit > smallwindmillptr->cost or credit == smallwindmillptr->cost){
        cout<<"[$] You have now bought a small hyper windmill.\n";
        credit = credit - smallwindmillptr->cost;
        power = power + smallwindmillptr->prpower;
        cin.get();
        cin.get();
        structmanage2();
        break;
    }
case 69:
    system ("cls");
    structmanage();
    break;
default:
    cout<<"<<<ERROR! COMMAND NOT FOUND!>>>\n";
    cin.get();
    cin.get();
    structmanage2();
    break;
    }
}


If you ask, I could write the section used in this part, by part.
But from here, I'm telling you, at case 2 (stardock), power goes to 9 even though it is subtracted by, 10???
Have you tried printing out the values of power and stardockptr->consumption before this line with the assignment?

power = power - stardockptr->consumption;
Have you tried printing out the values of power and stardockptr->consumption before this line with the assignment?

power = power - stardockptr->consumption;


No... But I'll see.
Have you tried printing out the values of power and stardockptr->consumption before this line with the assignment?

power = power - stardockptr->consumption;


No... But I'll see.


OK, I just did, and I found the actual problem. There's some odd glitch in the code. I press 2 and write it, and it all of a sudden goes to case 3! And it only happens when power is = to stardock->consumption.

Just tried a lot of stuff, even changed switch to if commands. But nope, stardock seems to be getting denied. Now it seems there's something wrong with case 2 or stardock. Do you suggest that I should delete it?
Last edited on
For switch statements, if you don't have a break statement at the end of the case statement, it will go to the next case statement. So, for example the following code will print "abcd".

1
2
3
4
5
6
7
8
9
10
11
switch (myVariable)
{
case 1:
    cout << "a";
case 2:
    cout << "b";
case 3:
    cout << "c";
case 4:
    cout << "d";
}


The following code would only print out "a" if myVariable is 1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
switch (myVariable)
{
case 1:
    cout << "a";
    break;
case 2:
    cout << "b";
    break;
case 3:
    cout << "c";
    break;
case 4:
    cout << "d";
    break;
}


So in your code, you have "if" and "else if" statements in most of your case statements. If the if is false, and the else is false (which will happen if power == stardock->consumption), you will not hit a break statement, and you will go to the next case statement.

I bet you need a "break;" statement after all the else if statements in your case statements.
For switch statements, if you don't have a break statement at the end of the case statement, it will go to the next case statement. So, for example the following code will print "abcd".

switch (myVariable)
{
case 1:
cout << "a";
case 2:
cout << "b";
case 3:
cout << "c";
case 4:
cout << "d";
}


The following code would only print out "a" if myVariable is 1.

switch (myVariable)
{
case 1:
cout << "a";
break;
case 2:
cout << "b";
break;
case 3:
cout << "c";
break;
case 4:
cout << "d";
break;
}


So in your code, you have "if" and "else if" statements in most of your case statements. If the if is false, and the else is false (which will happen if power == stardock->consumption), you will not hit a break statement, and you will go to the next case statement.

I bet you need a "break;" statement after all the else if statements in your case statements.


I tried that, but then, it just well, goes back to a command function I had assigned before (not shown) and just, well ****s off. Seriously, I think I should rather change what's in the if instead of break after else if. What do you think?
Hey! Finally solved it! Thanks guys for all the help you gave, and seriously, thanks tscott8706, wildblue and RandomPersonWorking!

The solution has been found and the problem is gone.

And for the second time, THANKS!!! :P
Topic archived. No new replies allowed.