Thread: Class Function Members and If Statements

  1. #1
    The Maverick Programmer
    Join Date
    Jan 2005
    Posts
    10

    Class Function Members and If Statements

    Hey, I'm trying to have a function that is a member of a large class. I'm having trouble with some if statements. Here's an example of what I'm doing.

    Code:
    int classa :: functiona () {
    if (x > y && y < z)
    num1 = 10;
    
    else if (y > x && y < z)
    num1 = 10;
    
    else if (y  > z && y > x)
    num1 = 10;
    }
    This is the sort of problem my compiler gets. On the first else if, it says there is a syntax error becuase there are two if statements, and they both have num1 equal to 10. This is an exaggeration of my problem, but it is still a major thorn in my hide. Does anyone know how I could sidestep this?

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    you'll need to return something.. as your function is prototyped to return an int type variable.


    It's ok to have several return statements listed in your function.. just so long as your if( ) logic will resolve to call only a single return.


    If you prototype your function as void, then you will not have to worry about returning anything.
    Last edited by The Brain; 02-02-2005 at 05:44 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    The Maverick Programmer
    Join Date
    Jan 2005
    Posts
    10
    Okay, thanks, I'll have to do that. I may or may not have to return an integer, depending on how my coding goes later on, so should that be the case, setting it to void would solve this problem as well. Thanks.

    EDIT: Okay, I did not need to return an integer and I have set all of the functions to void, yet my problem still persists. I have a couple of if statements that need very percise input, but a few of them will bounce back the same answers as some of the others and my compiler, Dev-C++, won't let me citing a syntax error. Any other advice?
    Last edited by Team Shadow V2; 02-02-2005 at 06:00 PM.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Can you post the exact compiler error? It doesn't sound like something the compiler should choke on.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    The Maverick Programmer
    Join Date
    Jan 2005
    Posts
    10
    Here's the exact cod that is giving me problems.

    Code:
    void abc::opget() {
    if ( strcmp (op1, "-") == 0 && strcmp (op2, "-") == 0 )
    num1 = num1 * -1;
    num2 = num2 * -1;
        
    else if ( strcmp (op1, "-") == 0 )
    num1 = num1 * -1;
    
    else if ( strcmp (op2, "-") == 0 )
    num2 = num2 * -1;
    }
    the compiler error is as follows. "syntax error before 'esle' " which it refers to the else if statements. If I swith the first loop to be the last one, it compiles just fine, but I need the function to check for that specific statement first. Else it doesn't work properly.

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    its because you didn't wrap your num2 = num2 * -1 in a brace use curly braces around your if statements and else statements it will make you life easier.
    Woop?

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ah, the problem is simply that you've forgotten to put the things in the if block in {}
    Code:
    void abc::opget()
    {
       if ( strcmp (op1, "-") == 0 && strcmp (op2, "-") == 0 )
       {
          num1 = num1 * -1;
          num2 = num2 * -1;
       }
       else if ( strcmp (op1, "-") == 0 )
          num1 = num1 * -1;
       else if ( strcmp (op2, "-") == 0 )
          num2 = num2 * -1;
    }
    If the code that you want associated with the if is 1 line long, then you don't need {}; but if it's more than one line then you need to enclose it in {}. The same goes for else if and else blocks.

    You might also like to consider indenting your code (for an example of one indenting scheme you might use, refer to the code I posted), as it makes code much much much easier to read.

    **EDIT**
    As a side note, you don't need to say num1 * -1; you can simply do:
    num1 = -num1;
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    The Maverick Programmer
    Join Date
    Jan 2005
    Posts
    10
    Thanks alot. This is really going to help me with a big project of mine. I thought, moreover my tutorials showed me, that for if statments in functions, the use of the {} wasn't always needed but sometimes needed. Didn't say when. Well, now I know. And about the num1 = num1 * -1, that's just something, an old habit, from another game programming language. And I will try to indent my code for the forums. Thanks again.

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    1
    you say its an old habit from another game programming language.
    what exactly is a "game programming language"? and what where you using? in every language ive ever used (ever ever ever) num1=-num1 works fine. Including BASIC. just wondering

  10. #10
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    you say its an old habit from another game programming language.
    what exactly is a "game programming language"?
    There are several game engines like Torque which have their own scripting languages.

  11. #11
    The Maverick Programmer
    Join Date
    Jan 2005
    Posts
    10
    I used to know alittle programming language that was an offshot of BASIC, BlitzBASIC, which was used specifically for making games. The only way you could ever increment a variable was by having the variable equal itself pluss what ever value you wanted to tag onto.

Popular pages Recent additions subscribe to a feed