Thread: IF error

  1. #1
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    IF error

    Code:
    // Structure : MONSTER //////////////////////////////////////////
    struct mnstr
    {
    char* name;
    int strength;
    int constitution;
    int magiclevel;
    };
    
    mnstr monster;
    
    // Monster Statistics //////////////////////////////////////////
    if(player.exp < 2000)
    {getmonster = 2;}
    else {
    if(player.exp < 3000)
    {getmonster = 3;}

    I get a parse error before the first IF in Monster Statistics... Anyone got an idea ?

  2. #2
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    Code:
    else {
    if(player.exp < 3000)
    {getmonster = 3;}
    you need another '}'

    Code:
    else {
    if(player.exp < 3000)
    {getmonster = 3;}
    }
    or better

    Code:
    else if (player.exp < 3000){
    getmonster = 3;
    }

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    You could try this

    Just remove all your "{" && "}" and that should do the trick,

    As pointed above, u just need an additional "}"

    Code:
    if(player.exp < 2000)
         getmonster = 2;
    else if(player.exp < 3000)
         getmonster = 3;
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  4. #4
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Actually no, the code goes like this :

    Code:
    // Structure : MONSTER //////////////////////////////////////////
    struct mnstr
    {
    char* name;
    int strength;
    int constitution;
    int magiclevel;
    };
    
    mnstr monster;
    
    // Monster Statistics //////////////////////////////////////////
    if(player.exp < 2000)
    {getmonster = 2;}
    else {
    if(player.exp < 3000)
    {getmonster = 3;}
    else {
    if(player.exp < 4000)
    {getmonster = 4;}
    else {
    if(player.exp < 5000)
    {getmonster = 5;}
    else {
    if(player.exp < 6000)
    {getmonster = 6;}
    else {
    if(player.exp < 7000)
    {getmonster = 7;}
    else {
    if(player.exp < 8000)
    {getmonster = 8;}
    else {
    if(player.exp < 9000)
    {getmonster = 9;}
    else {
    if(player.exp < 10000)
    {getmonster = 10;}
    }}}}}}}}
    So I don't need another }. Besides, the parse error is before the IF. Any ideas ?

  5. #5
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    Code:
    if(player.exp < 2000)
    {getmonster = 2;}
    else {
    if(player.exp < 3000)
    {getmonster = 3;}
    else {
    if(player.exp < 4000)
    {getmonster = 4;}
    else {
    if(player.exp < 5000)
    {getmonster = 5;}
    else {
    if(player.exp < 6000)
    {getmonster = 6;}
    else {
    if(player.exp < 7000)
    {getmonster = 7;}
    else {
    if(player.exp < 8000)
    {getmonster = 8;}
    else {
    if(player.exp < 9000)
    {getmonster = 9;}
    else {
    if(player.exp < 10000)
    {getmonster = 10;}
    }}}}}}}}
    no you can't do that.

    Code:
    if(player.exp < 2000)
    {getmonster = 2;}
    else if(player.exp < 3000){
    getmonster = 3;}
    else if(player.exp < 4000){
    getmonster = 4;}
    else if(player.exp < 5000)
    {getmonster = 5;}
    else if(player.exp < 6000)
    {getmonster = 6;}
    else if(player.exp < 7000)
    {getmonster = 7;}
    else if(player.exp < 8000)
    {getmonster = 8;}
    else if(player.exp < 9000)
    {getmonster = 9;}
    else if(player.exp < 10000)
    {getmonster = 10;}
    Each 'if' or 'else if' needs it's own open and closed set of parentheses. You can't close them all at the end like you did.

  6. #6
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Yes you can, I've done it before. I just can't see anything wrong with this.

  7. #7
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    Ok then, you can

  8. #8
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    Unhappy

    Pffff ! So I guess you don't know what's wrong with it ?

    Anyone else ?

  9. #9
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    Did you attempt the changes that myself and shiv_tech_quest provided you?

    This is ok

    Code:
    if (condition1)
    {
    if (condition2)
    {
    
    }
    }
    but not

    Code:
    if (condition1)
    {
    else 
    {
    
    }
    }
    Last edited by minesweeper; 01-17-2003 at 05:47 AM.

  10. #10
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    My code says :

    Code:
    if(condition1)
    {dothis;}
    else {
    if(condition2)
    {do that;}}

  11. #11
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    Code:
    if(condition1)
    {dothis;
    else {do that;}}
    This is the same as

    Code:
    if (condition1)
    {
    do this;
    else
    {
    do this;
    }
    }
    and you can't do it

  12. #12
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    NO ! Look :

    There is another IF in my ELSE statement :

    Code:
    if(myname == "Me")
    {cout << "Hello ME";}
    else {
    if(myname == "You")
    {cout << "Hello YOU";}
    }

  13. #13
    Registered User
    Join Date
    Aug 2002
    Posts
    87

    ehhhhhh......

    sorry to tell you this dude but that aint's right.....

    if you wish to uise if else then you will have to do it like this:


    if(condition1)
    {
    do this
    }
    else if(condition2)
    {
    do this
    }
    else
    {
    do this
    }

    Also if you have only one operation that needs to be executed after the if you have no need for the {} e.g.

    if(condition1)
    do this;
    else
    do this;

    and what you were doin does not work.....

    if(condition1)
    {
    do this;
    else if(condition2)
    {
    do this;
    else
    {
    do this;
    }
    }
    }

    shouldn't work because the else does not know which if it should be using...

  14. #14
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Actually DirX it works. You can do this :

    Code:
    if(integer == 1)
    {cout << "1";}
    
    else {
    
    if(integer == 2)
    {cout << "2";}
    
    else {
    
    if(integer == 3)
    {cout << "3";}
    
    } }

    I think this may be the same as using ELSE IF, but I'm not sure. Either way, this works, I've tested it in every possible way.

  15. #15
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    hmm actually now you said it it does work but it is far from what any person with knowledge of c++ would do..... basically what you are doing here is nesting... which would mean that you would have to tab it in before the following if.

    using this:

    if(condition1)
    {
    do this;
    }
    else if(condition2)
    {
    do this;
    }
    else
    {
    do this;
    }

    is a lot more convetionel and easier to use, read, and understand


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM