Cannot find the missing ';'

Pages: 12
IMO, when dealing with the console either is acceptable. But you need to realize that endl does an additional operation that the '\n' doesn't. The endl writes the end of line characters and also causes the flushing of the buffer causing the information to be written to the console "immediately". The '\n' writes the new line character to the stream, but doesn't flush the buffer. The buffer will be flushed when the buffer if full, or some other event requires the flushing of the buffer.

However when using streams other than the console streams continually flushing the buffer can be very time consuming.

Last edited on
So in the long run, endl; is better. Its std::endl; right?
Btw a Grue! Bet you have played Zork too!
closed account (E0p9LyTq)
Regarding std::endl vs. "\n" I refer you to the "C++ Core Guidelines", section SL.50: "Avoid endl".
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#S-stdlib

Reason
The endl manipulator is mostly equivalent to '\\n' and "\\n"; as most commonly used it simply slows down output by doing redundant flush()s. This slowdown can be significant compared to printf-style output.

Example
1
2
cout << "Hello, World!" << endl;    // two output operations and a flush
cout << "hello, World!\n";          // one output operation and no flush 


Note
For cin/cout (and equivalent) interaction, there is no reason to flush; that's done automatically. For writing to a file, there is rarely a need to flush.

Note
Apart from the (occasionally important) issue of performance, the choice between "\\n" and endl is almost completely aestetic.
Hi,

In your code you had if statements testing various combinations of case with "yes" and "ceramic". The reason this is a bad idea is that happens when the user types in some combination you didn't have in your code? Answer: the code breaks. So the solution is to restrict the input to one char, use the std::toupper function on it, then you only have one test: 'C' for ceramic say. If there are multiple things you want to select from, then use a switch statement.

There are minor irritations from having :

1
2
using std::cout;
using std::cin; //etcetera 


The first is that it is not hard to accumulate lots of these: for example if one uses various containers and algorithms, and modifiers such as std::left and std::right. There are lots of these associated with the iostream header alone, never mind any thing else one might include.

The second one is that if I send you a function I wrote, and I use std:: everywhere, then you don't have to do anything to it to make it work.

Another thing to realise is that all the experienced and expert coders do std::, so if you learn to do that now, you will be ahead of the game.

Also, it is a good idea to put your own code in your own namespace/s.

Good Luck !!
@FurryGuy
Thanks! But I know "\n" but what is "\\n"? Are they the same?
@TheIdeasMan
I would surely take that into consideration! I'm rewriting the program with the changes you suggested! Thanks for them!

And also I would be writing it cleanly this time!
closed account (E0p9LyTq)
theLoneWolf wrote:
I know "\n" but what is "\\n"? Are they the same?


Read up on escape code string literal constants
http://www.cplusplus.com/doc/oldtutorial/constants/
Thanks! But I know "\n" but what is "\\n"? Are they the same?

I suspect that's an artifact of the markup used on the site. It should read "\n".
I will submit one with tabs
Last edited on
Here it is

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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//E.L.I.T.E.S. corps
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main ()
{
    string firstn;
    string lastn;
    string choice;
    int damage;
    int chance;
    int echance;
    int d = 0;
    int goondamage;
    int bdmg;
    int x = 5;
    bool weapon;
    bool dodge;
    int goonhp = 150;
    int myhp;
    int hp = 500;
    srand (time (0));
    chance = rand () % (10-1) + 1;
    echance = rand ()%(8-1)+1;
    int bchance = rand () % (7-1) + 1;
    goondamage = rand () % (20-1) + 1;
    bdmg = rand () % (40-20) + 20;
    cout << "ENTER YOUR FIRST NAME: ";
    cin>> firstn;
    cout<<"\nENTER YOUR LAST NAME: ";
    cin>>lastn;
    cout<<"\nSO MR. "<< firstn<< " "<< lastn << " !";
    cout<<"\nREADY TO GET YOUR MUSH IN PIECES? BUT HERE IS THE CATCH! IF YOU LOSE, I TRAP YOUR PC IN AN ETERNAL LOOP! IF YOU WIN, THEN YOU GO FREE! DEAL?";
    cout<<"\n-Please enter your answer (yes/no) : ";
    cin>>choice;
    if (choice == "yes" || choice == "Yes" || choice == "YES" || choice == "Yes!") 
    {
        cout<<"\nSO BE IT!";
    }
    else 
    {
        cout<<"\nRUN TO YOUR MOMMY, KID!";
        cin.get();
        return 0;
        }
    cout<<"\nWelcome to the E.L.I.T.E.S. corps, sir!\n";
    cin.get();
    cout<<"You must be our new trainee Agent "<< lastn<<"!...\n";
    cout<<"CODE YELLOW! ARMED SUSPECTS SPOTTED! THIS IS NOT A DRILL! THIS IS NOT A DRILL!\n";
    cout<<"Time to prove your worth! Grab your equipment\n";
    cout<<"PICK YOUR ARM: \n";
    cout<<"GLOCK is a weapon with low damage but high accuracy. DEAGLE is a weapon with extremely high damage but a higher chance to miss. Pick by typing their name. The name are given in upper case. Pick one: ";
    cin>>choice;
    if (choice=="GLOCK"||choice =="Glock"||choice=="glock")
    {
        bool weapon= true;
    }
    else if (choice=="DEAGLE"||choice=="Deagle"||choice== "deagle"||choice== "DEagle")
    {
        bool weapon=false;
    }
    else 
    {
        cout<<"WRONG CHOICE! RESTART!";
    cin.get();
    return 0;
    }
    if (weapon==true)
    {
        damage = rand() % (50-10) + 10;
    }
    else if (weapon== false)
    {
        damage = rand() % (100-80) + 80;
    }
    cout<<"PICK ARMOUR: \n";
    cout<<"KEVLAR is a light armour that gives some protection and boosts attack. CERAMIC is a heavy armour that gives increased protection but does not boost attack. PICK ONE: \n";
    cin>>choice;
    if (choice=="KEVLAR" || choice=="kevlar"||choice =="Kevlar")
    {
        myhp=500;
    damage+=20;
    }
    else if (choice=="CERAMIC" || choice=="ceramic" || choice=="Ceramic")
    {
        myhp = 800;
    }
    else 
    {
        cout<<"WRONG CHOICE";
    cin.get();
    return 0;
    }
    cout<< "FATE PROVIDES YOU ANOTHER CHANCE TO LEAVE... Press Y to leave or N to stay\n";
    cin>>choice;
    if(choice=="Y" || choice=="y")
    {
        return 0;
    }
    else{
        cout<<"GREAT!";
    }
    cout<<"\nYou run to the site. A goon approaches...";
    
    do
    {
        do
        {
            cout<<"HP: "<<myhp;
            cout<<"\nPress 1 to fire or 2 to dodge.";
            cin>>choice;
            if(choice=="1")
            {
                if(weapon==true)
                {
                    if(chance==3 || chance==6)
                    {
                        cout<<"YOU MISS!";
                    }
                    else if(chance==5)
                    {
                        cout<<"CRITICAL HIT!";
                        goonhp-=(damage*2);
                    }
                    else
                    {
                        goonhp-=damage;
                    }
                }
                else if (weapon==false)
                {  
                    if (chance==3 || chance==6 || chance==9)
                    {
                        cout<<"YOU MISS!";
                    }
                    else if(chance==5 || chance==4)
                    {
                        cout<<"CRITICAL HIT!";
                        goonhp-=(damage*2);
                    }
                    else
                    {
                        goonhp-=damage;
                    }
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                dodge=true;
            }
            cout<<"\nENEMY'S TURN!";
            cin.get();
            if (echance == 2 || echance == 4 && dodge == false)
            {
                cout<<"THE ENEMY HITS!";
                myhp -= goondamage;
            }
            else if (echance == 5 && dodge == false)
            {
                cout <<"THE ENEMY PLANTS A CRITICAL HIT!";
                myhp -= (goondamage*2);
            }
            else if (echance == echance && dodge == true)
            {  
                cout <<"THE ENEMY MISSES!";
            }
            else 
            {
                cout<<"THE ENEMY MISSES!";
            }
            cout << "\nTHE TURN ENDS!";
        } while(goonhp > 0 || myhp > 0)    ;
        if (myhp < 0)
        {
            cout<<"ETERNAL LOOP!";
            x+=5;
            do 
            {
                cout <<d<<" "; 
            }
            while (x>5);
        }
        else 
        {
            ++d;
        }
    } while (d!=1);
    cout<<"\nTHE ENEMY LEADER! THELONEWOLF APPROACHES!";
    do
    {
        cout<<"HP: "<<myhp;
        cout<<"\nPress 1 to fire or 2 to dodge.";
        cin>>choice;
        if(choice=="1")
        {
            if(weapon==true)
            {
                if(chance==3 || chance==6)
                {
                    cout<<"YOU MISS!";
                }
                else if(chance==5)
                {
                    cout<<"CRITICAL HIT!";
                    goonhp-=(damage*2);
                }
                else
                {
                    hp-=damage;
                }
            }
            else if(weapon==false)
            {
                if(chance==3 || chance==6 || chance==9)
                {
                    cout<<"YOU MISS!";
                }
                else if(chance==5 || chance==4)
                {
                    cout<<"CRITICAL HIT!";
                    hp-=(damage*2);
                }
                else
                {
                    hp-=damage;
                }
            }
            else
            {
                return 0;
            }
        }
        else
        {
            dodge=true;
        }
        cout<<"\nENEMY'S TURN!";
        cin.get();
        if (bchance == 2 || bchance == 4 && dodge == false)
        {
            cout<<"THE ENEMY HITS!";
            myhp -= bdmg;
        }
        else if (bchance == 5 && dodge == false)
        {
            cout <<"THE ENEMY PLANTS A CRITICAL HIT!";
            myhp -= (bdmg*2);
        }
        else if (bchance == bchance && dodge == true)
        {
            cout <<"THE ENEMY MISSES!";
        }
        else 
        {
            cout<<"THE ENEMY MISSES!";
        }
        cout << "\nTHE TURN ENDS!";
    } while (hp > 0 || myhp > 0) ;
    if (hp < 0)
    {
        cout<<"\nYOU DIE! Eternal loop!";
        x+=5;
        do 
        {
            cout <<x<<" ";
        }
        while (x>5);   
    }
    else 
    {
        cout << "\nYOU WIN THIS TIME! WATCH OUT THE NEXT!";
    }
    cin.get();
    return 0;
}
}
Last edited on
Topic archived. No new replies allowed.
Pages: 12