1

I'm new to C++ and have been staring at my (probably abysmal) code for a while and can't figure out what's off about it.

I'm trying to loop through a few iterations of if and else statements and must be doing something grammatically incorrect - as it shows compiler errors of 'else without a previous if'

This is for a class and I'm trying to work it out, but if you see something obvious that I am overlooking I would love to know.

Thank you!

for (i = 0; i < 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 is the 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 is the 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 is the Winner!\n";
        }
continue;
congusbongus
  • 13,359
  • 7
  • 71
  • 99
stringgy
  • 85
  • 1
  • 1
  • 10
  • the ones after the else statements? I wasn't sure if they were needed. I'll try and edit it and run it again. – stringgy Feb 21 '13 at 05:53

3 Answers3

6

else doesn' take any condition, but you've written this:

else (charlieAlive == 1 && bobAlive == 0);  //else : (notice semicolon)

which doesn't do what you intend it to do.

You want to do thos:

else if (charlieAlive == 1 && bobAlive == 0)  //else if : (semicolon removed)

Notice the difference.

Also, there can be at most one else block, associated with an if block Or a chain of if, else-if, else-if blocks. That is, you can write this:

if (condition) {}
else {}

Or,

if (condition0) {}
else if (condition1) {}
else if (condition2) {}
else if (condition3) {}
else if (condition4) {}
else {}

In any case, else block is always the last block. After that if you write another else block, that would be an error.

Apart from that you also have a semicolon at wrong place. Fixed that also:

else (charlieAlive == 1 && bobAlive == 0); <---- remove this semicolon!

Hope that helps.


Pick a good Introductory C++ Book. Here are few recommendations for all levels.

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • Thank you so much! It's those simple grammar things that I can't find answers to in my text book (when exactly you need a semi for example). – stringgy Feb 21 '13 at 06:03
  • 1
    @user1865183: [Pick a good Introductory C++ Book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Nawaz Feb 21 '13 at 06:04
3

There are several problems I see here:

  1. There are semicolons in your else statements - these aren't supposed to be there
  2. You have multiple else clauses for a single if. Use 'else if' when you are evaluating another condition - else is the catch-all for when no conditions are met
  3. I highly recommend proper indenting and consistent brace usage - not doing this isn't necessarily an error, but it will make it much easier to notice errors.
Krease
  • 15,805
  • 8
  • 54
  • 86
1

you cant put condition statement in else statement

correct for all else statements

like in else (charlieAlive == 1 && bobAlive == 0);

else is simply the alternative flow of if - i.e.

if(condition)  // if this fails go to else part
  {
--- // if condition true execute this
}
else{
 --- // will run when condition in if fails
}

so you don't have to put condition for else statement

Edit
where as else if takes condition as well seems you wanted to do this
else if(your condition statements) // Note: no semicolon at the end

exexzian
  • 7,782
  • 6
  • 41
  • 52