Small bug

Hello when I start the program little bug is occurring.
Some ideas?
Thank you in advance!
By the way I use code::blocks as a platform and this program is a simple game.

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
  
# include <iostream>
# include <stdlib.h>
# include <windows.h>
using namespace std;
int main(){
    long long attack, defence, eattack, edefence, life, elife, rest, erest;
    long long attackp, t[2], money, moneya, account, price[2];
    char attacktype, shop, weapon;
    bool boughtweapon[2];
    life = 100;
    elife = 100;
    cout << "press (s) to start:\n";
    cin >> attacktype;
    t[0, 1, 2] = 0;
    money = 0;
    price[0, 1, 2] = 30;
    cout << "\nNEW GAME\n\n";
    boughtweapon[0, 1, 2] = false;
while(true){
        moneya = money;
        if ( attacktype != 's' and attacktype != 'h' and attacktype != 'i' ){
            cout << "\nUNABLE TO READ COMMAND";
            break;
        }
        if (attacktype == 's'){
            attack = rand () % 10;
            attackp = attack;
            if ( boughtweapon[0] = true){
                erest = elife - (attackp + t [0]);
            }
            if ( boughtweapon[1] = true){
                erest = elife - (attackp + t [1]);
            }
            if ( boughtweapon[2] = true){
                erest = elife - (attackp + t [2]);
            }
            if ( boughtweapon[0, 1, 2] = false ){
                erest = elife - attackp;
            }
            elife = erest;
            eattack = rand () % 8;
            rest = life - eattack;
            life = rest;
            cout << "Choose what kind of attack to use: stab(s), hack(h) or instantkill(i):\n";
            cin >> attacktype;
            cout << "You take the enemy  " << attackp << " damage | " << erest << " enemy life\n";
            cout << "The enemy takes you " << eattack << " damage | " << rest << " your life\n\n";

        }
        if (attacktype == 'h'){
            attack = rand () % 15;
            attackp = attack ;
            if ( boughtweapon[0] = true){
                erest = elife - (attackp + t [0]);
            }
            if ( boughtweapon[1] = true){
                erest = elife - (attackp + t [1]);
            }
            if ( boughtweapon[2] = true){
                erest = elife - (attackp + t [2]);
            }
            if ( boughtweapon[0, 1, 2] = false ){
                erest = elife - attackp;
            }
            elife = erest;
            eattack = rand () % 20;
            rest = life - eattack;
            life = rest;
            cout << "stab, hack, instantkill:\n";
            cin >> attacktype;
            cout << "You take the enemy  " << attackp << " damage | " << erest << " enemy life\n";
            cout << "The enemy takes you " << eattack << " damage | " << rest << " your life\n";
        }
        if (attacktype == 'i'){
            attack = rand () % (60 - 10);
            attackp = attack;
            if ( boughtweapon[0] = true){
                erest = elife - (attackp + t [0]);
            }
            if ( boughtweapon[1] = true){
                erest = elife - (attackp + t [1]);
            }
            if ( boughtweapon[2] = true){
                erest = elife - (attackp + t [2]);
            }
            if ( boughtweapon[0, 1, 2] = false ){
                erest = elife - attackp;
            }
            elife = erest;
            eattack = rand() %( 60 - 10 );
            rest = life - eattack;
            life = rest;
            cout << "stab(s), hack(h), instantkill(i) (do not use 'i' again or instant dead for YOU!!!):\n";
            cin >> attacktype;
            cout << "You take the enemy  " << attackp << " damage | " <<  erest << " enemy life\n";
            cout << "The enemy takes you " << eattack << " damage | " << rest << " your life\n";
        }
        if ( life <= 0 ){
            cout << "you lost\n";
            cout << "do you want to go to shop(press (S)) or pres any other button to play again\n";
            cin >> shop;
            money = rand () % (20 - 5) - 10;
            account = money + moneya;
            cout << account << "$ your money\n";
            if ( shop == 'S' ){
                cout << "What do you want to buy sword(+3), bow(+2) or axe(+3)\n";
                cin >> weapon;
                if( account < price[0] and account < price[1] and account < price[2] ){
                    cout << "You don't have enough money";
                    continue;
                }
                if (  account >= price[0] and account >= price[1] and account >= price[2]  ) {
                    t[0] = 3;
                    t[1] = 2;
                    t[2] = 3;
                    cin >> weapon;
                    switch(weapon){
                        case 's' : attackp == attack + t[0];
                        boughtweapon[0] = true;
                        case 'b' : attackp == attack + t[1];
                        boughtweapon[1] = true;
                        case 'a' : attackp == attack + t[2];
                        boughtweapon[2] = true;
                    }
                }
            }
            else {
                elife = 100;
                life = 100;
                continue;
                cout << "NEW GAME\n\n";
                boughtweapon[0,1,2] = false;
            }
            elife = 100;
            life = 100;
            cout << endl;
        }
        if ( elife <= 0 ){
            cout << "you win\n";
            cout << "do you want to go to shop(press (S)) or press any other button to play again\n";
            money = rand () % (20 - 5) + 10;
            account = money + moneya;
            cout << account << "$ your money\n";
            cin >> shop;
            if ( shop == 'S' ){
                cout << "What do you want to buy sword(+3damage), bow(+2damage) or axe(+3damage)\n";
                if( account < price[0] and account < price[1] and account < price[2] ){
                    cout << "You don't have enough money\n";
                    continue;
                }
                if ( account >= price[0] and account >= price[1] and account >= price[2] ) {
                    t[0] = 3;
                    t[1] = 2;
                    t[2] = 3;
                    cin >> weapon;
                    switch( weapon ){
                        case 's' : attackp == attack + t[0];
                        boughtweapon[0] = true;
                        case 'b' : attackp == attack + t[1];
                        boughtweapon[1] = true;
                        case 'a' : attackp == attack + t[2];
                        boughtweapon[2] = true;
                    }
                }
            }
            else {
                elife = 100;
                life = 100;
                continue;
                cout << "NEW GAME\n\n";
                boughtweapon[0,1,2] = false;
            }
            elife = 100;
            life = 100;
            cout << endl;
        }
    if ( attacktype == 'i' ){
        cout << "I told you so!\nYou lost! -10$ \n";
        account = account - 10;
        cout << account << "$ your money\n\n";
        cout << "NEW GAME\n\n";
        continue;
    }
}
return 0;
}
Describe the bug. Describe the inputs you're using when the bug occurs. Describe the expected behavior.
the bug is that when I start the game I win it after two terms
the enemy has got 100 at the beginning after that I take it 1 or 2 and it is what I expect but on the second time I enter my attack type I always win
Ok, there are a lot of things here...
This line price[0, 1, 2] = 30; and others like it are not doing what you probably think they're doing. It compiles because C++ maintains a comma operator. What you have inside the square brackets is a series of expressions that are evaluated and yield the value of the final expression, which in this case, is 2. That means this line evaluates to price[2] = 30; which means that only the last element in the array is taking on the value of 30.

Also, these lines are probably not what you want: if ( boughtweapon[1] = true). I think you want to use == instead of =. == is a test for equality, = is an assignment.
Last edited on
Yes you are right,
Thank you very much!
Will it work if I do:
 
boughtweapon[0 and 1 and 2] == true;
If it compiles, it still wouldn't work the way you think it does ('and' is an alternate spelling for operator '&&' : http://www.cplusplus.com/reference/ciso646/)

You can write a loop that sets each element in the array, or you can use memset (http://www.cplusplus.com/reference/cstring/memset/). However, I would encourage you to look into vectors : http://www.cplusplus.com/reference/vector/vector/vector/

You'll notice that std::vector provides a 'fill' constructor to do what you want to do.
Thank you!
Topic archived. No new replies allowed.