c++ - Why am I getting an 'Else without previous if' error within a for loop? -
i'm new c++ , have been staring @ (probably abysmal) code while , can't figure out what's off it.
i'm trying loop through few iterations of if , else statements , must doing grammatically wrong - shows compiler errors of 'else without previous if'
this class , i'm trying work out, if see obvious overlooking love know.
thank you!
for (i = 0; < iterationsnum; i++){ if (charliealive == 0) // aarron's shot { if (aaronshot() == 1) charliealive = 1; } else (charliealive == 1 && bobalive == 0);{ if (aaronshot() == 1) bobalive = 1; } else (charliealive == 1 && bobalive == 1 && aaronalive == 0);{ cout << "aaron winner!\n"; totalshot++; aaroncounter++; } continue; if (charliealive == 0 && aaronalive ==0) // bob's shot { if (bobshot() == 1) charliealive = 1; } else (charliealive == 1 && aaronalive == 0);{ if (bobshot() == 1) aaronalive = 1; } else (charliealive == 1 && aaronalive == 1 && bobalive == 0);{ cout << "bob winner!\n"; bobcounter++; totalshot++; } continue; if (charliealive == 0 && bobalive == 0) // charlie's shot { bobalive = 1; } else (charliealive == 0 && bobalive == 1 && aaronalive == 0);{ aaronalive = 1; totalshot++; } else (charliealive == 0 && bobalive == 1 && aaronalive == 1);{ cout << "charlie winner!\n"; } continue;
else
doesn' take condition, you've written this:
else (charliealive == 1 && bobalive == 0); //else : (notice semicolon)
which doesn't intend do.
you want thos:
else if (charliealive == 1 && bobalive == 0) //else if : (semicolon removed)
notice difference.
also, there can @ 1 else
block, associated if
block or chain of if, else-if, else-if
blocks. is, can write this:
if (condition) {} else {}
or,
if (condition0) {} else if (condition1) {} else if (condition2) {} else if (condition3) {} else if (condition4) {} else {}
in case, else
block always lastly block. after if write else
block, error.
apart have semicolon @ wrong place. fixed also:
else (charliealive == 1 && bobalive == 0); <---- remove semicolon!
hope helps.
pick introductory c++ book. here few recommendations levels.
the definitive c++ book guide , list c++ syntax if-statement
No comments:
Post a Comment