Cannot find the missing ';'

Pages: 12
Hey guys so after the loads of questions I asked and some tips on writing clean code I got, I made this program. codepad.org/W7F9DWlw
but am still facing an error I couldn't eradicate even after writing the particular block of code 3 times. So any help? Thanks in advance!
Last edited on
I suggest you post your code inside your post. I for one can't access that link you provided.
Btw I fixed the link.
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
#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";
cin.get();
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!";
do {cout << x+=5;} 
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!";
do {cout <<  x+=5;}
while (x>5)}
else {cout << "\nYOU WIN THIS TIME! WATCH OUT THE NEXT!";}
cin.get();
return 0;
}

In function 'int main()':
Line 120: error: expected `;' before 'if'
compilation terminated due to -Wfatal-errors
closed account (E0p9LyTq)
You have several things wrong with your code:

1. Every do....while loop must have a ; at the end of the while statement.
http://www.cplusplus.com/doc/tutorial/control/

2. cout << x+=5; results in unexpected behavior, and with GCC 4.9.2 or VS2015 it won't compile. cout doesn't know how to deal with the assignment properly.

Split the assignment and the output into two separate lines:
1
2
x+=5;
cout << x;


After making those changes I can get the code to compile, I can't say if the code works in the manner you intend.
@FurryGuy
Thanks for reply! Didnt know that one! So thats why it wasnt compiling :o
Please explain the 'x' bit to me. I was planning on creating an endless do loop there.
It would be much easier for you and us to find the errors in your code if you indented it sensibly.
Is there a tuitorial on how to write clean code? I mean that seems right to me!
Really? Does the layout make it easy to see the flow of control? Does it make it easy to see which lines are in a conditional block? Or in a loop? Does it make it easy to see whether you have mismatched braces, or a missing else statement, or a missing semicolon?

Does that look to you like it's been laid out the same way as any other C++ code you've seen, in your textbook, or in tutorials, or here on this forum?

http://lmgtfy.com/?q=C%2B%2B+indentation
Hmmm... Yup. It is cluttered. Way cluttered. The thing is that my PC passed away after serving 12 years. So I'm working on an old-as-hell Nokia 206. It has a word limit of 4K and it is really difficult to type clean code in it! I'm really sorry for the inconvenience my codes are causing to you guys! But then I don't have anyone else to ask questions to.
closed account (E0p9LyTq)
TheLoneWolf wrote:
Please explain the 'x' bit to me. I was planning on creating an endless do loop there.

Do what I suggested and you still have an endless loop that will print out an increasing value for x.

Do you really want that?

If I were to create an endless loop I use a while(true) loop instead of a do...while loop. Example:

1
2
3
4
while(true)
{
    // do something ridiculously stupid endlessly
}


Personal suggestions:

I don't like obscure variable names, like x. What does x represent? Why not use a descriptive and self-documenting name, such as (for example) creatureHitPoints?

The only time I use one character variable names is in a small for loop, and for readability I consider still using a descriptive name. The time "wasted" typing long variable names makes for much less time doing code reviews.

About readability, PLEASE learn to use whitespace. Your code is far too dense/compact to make debugging easy. It is a real nightmare to look at.

I quite emphatically suggest to never use using namespace std;. Get in the habit of specifying the namespace explicitly. std::cout, not cout.

When you start using 3rd party libraries, such as Boost, being lazy with using namespace std; can and will make finding compile-time errors a real pain.

If you use certain std library objects/functions a lot, std::cout for example, then consider using std::cout; as a better alternative to using namespace std;.
Last edited on
cout << x+=5; results in unexpected behavior, and with GCC 4.9.2 or VS2015 it won't compile.


If "unexpected behavior" means it doesn't compile.

cout << x += 5; is interpreted as (cout << x) += 5.

Be explicit: cout << (x+=5);





Oh! So whats better?
1
2
3
std::cout
//or
using std::cout;

btw next time the code will be cleaner. I promise.
And the program compiled, though with an error.
This one:

cc1plus: warnings being treated as errors
In function 'int main()':
Line 58: warning: 'weapon' is used uninitialized in this function

So must I let it be or try to resolve it? And how to resolve it?
closed account (E0p9LyTq)
@cire,

I tried that earlier, and still got compile errors. I just tried it again, and it worked.

Maybe my fat fingers didn't type it correctly the first time.

I personally still wouldn't combine the assignment with the output, I'd make them separate statements.
cc1plus: warnings being treated as errors
In function 'int main()':
Line 58: warning: 'weapon' is used uninitialized in this function

So must I let it be or try to resolve it?


If it's an error, then your code won't compile, so if you don't resolve it, you don't have a program to run.

Even if it were just a warning, don't you see a problem with using a variable before you've initialized it? What value would you expect that variable to have? How can you rely on if (weapon==true) behaving as you intend it, if you haven't assigned any value to weapon?

And how to resolve it?

If the error is that the variable is uninitialized, then it follows that you resolve it by making sure you initialize it.

EDIT: The problem seems to be that on lines 52 and 54, you're not assigning values to the variable that's being looked at on line 58. Instead, you're creating new variables that only exist within the scopes of those blocks, and assign values to those. Line 58 is looking at the variable that you declare on line 18, which never has any value assigned to it.
Last edited on
Until you start using a sane indentation style you'll never be able to see all that is wrong with your program. So I suggest you read the following link, select an indentation style, and use it consistently.

https://en.wikipedia.org/wiki/Indent_style

The way your "program" is presently presented makes it so hard to read that many people will just glance at the mess and then move on to a more sensible post.

Edit: And by the way it looks to me that you're typing way too much code before you try to compile your program. If you compile often, many times after adding only one line of text, you will be able to spot many of the problems in this code much easier. So compile often, fix any and all warnings and errors before you add anything else.
Last edited on
closed account (E0p9LyTq)
TheLoneWolf wrote:
Oh! So whats better?


There are technical, and personal, reasons for using either one.

I personally always explicitly specify the namespace, std::cout. Name clashes are non-events.

Which one you use is a matter of personal style.

"Why is “using namespace std;” considered bad practice?"
http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
Ok then! Ill first work upon my writing skills before posting any other code here again!
closed account (E0p9LyTq)
Which is easier to read?

1
2
3
4
5
6
#include <iostream>
int main(){
std::cout<<"Hello World!"<<std::endl;
for(int i=0;i<5;i++)std::cout<<"Hello!\t";
std::cout<<std::endl;
return 0;}


or

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
   std::cout << "Hello World!" << std::endl;

   for (int i = 0; i < 5; i++)
   {
      std::cout << "Hello!\t";
   }
   std::cout << std::endl;

   return 0;
}
The latter. The latter one is awesome. I will try to write like that!
Btw what is better?
1
2
cout << "This one?" << endl;
cout << "Or this one?\n";
closed account (E0p9LyTq)
Neither is better. I use both.

std::cout << "It is pitch black, you are being attacked by a Grue!\n;";

std::cout << "This is the character's hitpoints: " << heroHitPoints << std::endl;

There is some "overhead" using std::endl vs. '\n', but for most programs that is such a triviality as to be not worth worrying about.

C++: “std::endl” vs “\n”
http://stackoverflow.com/questions/213907/c-stdendl-vs-n
Pages: 12