Thread: IF error

  1. #16
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    Code:
    if(myname == "Me")
    {cout << "Hello ME";}
    else {
    if(myname == "You")
    {cout << "Hello YOU";}
    }
    is fine as you have closed your 'else' statement.

    But in your program you aren't closing them, not until the end, which doesn't work, at least not to my knowledge. It certainly isn't something that is commonly done.

    As here
    Code:
    else {
    if(player.exp < 7000)
    {getmonster = 7;}
    else {
    You haven't closed the first 'else' before starting the second.
    Last edited by minesweeper; 01-17-2003 at 06:16 AM.

  2. #17
    DarkElfe
    Guest
    Hello,

    I copied/pasted your code in vc++6 and it compiled fine (so yes, you can close all your else statements at the end, but that's not pretty. Maybe your parse error is at another place in your program.

  3. #18
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    It's not a function... What I want is so that

    if your exp is less than 2000
    you can get monsters whose level is 2

    or

    if your exp is less than 3000
    you can get monsters whose level is 3

    or

    if your exp is less than 4000
    you can get monsters whose level is 4



    Because otherwise, if it's less than 3000 for example, int getmonster can be 2 OR 3 and that can cause some error.

  4. #19
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    so yes, you can close all your else statements at the end, but that's not pretty
    hmmm, i stand corrected. Though it does appear to be a silly that you can, and hideous to read.

  5. #20
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Korhedron, there are other ways to solve you problem.

    One for example is to divide exp by 1000 and add 1 up. If the result is smaller than 2 you can make it 2 and if it's larger than 10 you can make it 10:
    Code:
     getmonster = player.exp / 1000 + 1;
    
    if(getmonster < 2) 
       getmonster = 2;
    else if(getmonster > 10) 
       getmonster = 10;
    Cheers, Monster

  6. #21
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    Will this end the debate

    Code:
    if(condition1)
    {
         do this1;
    }
    else if(condition2)
    {
         do this 2;
    }
    else
    {
        do this 3;
    }
    
    is equivalent to
    
    if(condition1)
    {    
         do this1;
    }
    else
    {
        if(condition2)
        {
            do this 2;
         }
        else
        {
            do this3;
         }
    }
    The whole point is be it an "if" or an "else", they can have one statement without "{" "}"


    when you say else if
    what you are stating is that the else has got only one statement and that is another if statement
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  7. #22
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Kohedran are you doing these if statements outside of main? If so, then I think that is your problem. They need to be in a function whether it be main, a member function of mnstr or whatever.

    On a side note, my C++ teacher actually specifically told us to never use else if's. He said all of our programs for him had to be done using the nested if else's like what Kohedran has. Ugh, quite the eyesore.

  8. #23
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Two points.

    Korhedron:

    Look at the way shiv has formatted his code. Instantly obvious what belongs to what, clear, professional. That style of formatting, (called Allman format), is the most widely used now, numerous studies have shown that the number of errors reading this format is lower than any other, including K+R, Stroustrop and Whitesmith. Look at the confusion your format has created!

    I would recomend you adopt the Allman style of coding.

    PJ:

    >>>
    He said all of our programs for him had to be done using the nested if else's like what Kohedran has.
    <<<

    Well then he is an idiot. However, a point worth bearing in mind, is that when joining an organisation or project group, invariably they will have a set of "coding standards" which will, also invariably, contain some things you won't like. These coding standards will, however, need to be followed - often, the QA department will nit pick over trivial formatting/variable naming/structure usage transgressions, it gives them a feeling of power over a process they can never hope to understand.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #24
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I agree, which is why I did it in his class but haven't done it since. On a related topic, he also had us write void main() *shudders*

  10. #25
    just because it compiles doesn't mean it's right

  11. #26
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Thanks for your posts, but it doesn't solve my problem.

    This being one of my first programs, I didn't know about nested ifs and if elses. Now I'll use the if else.

    However, I still get this error. My IFs are in a function, and I get an error on that line, and I can't see anything wrong with it. I can't get the code to compile. I think I'll have to retype everything by hand... any ideas before I start that messy job ?

  12. #27
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by Korhedron
    any ideas before I start that messy job ?
    Yes, post your code or attach your C/C++ file so we can take a look at it.

  13. #28
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    Is the wait to see the code.... everlasting??

    Common buddy.....

    We can't wait for ever on this
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

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