CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Jun 2009
    Posts
    33

    Question If Statement && Help

    I need help with something, probably simple...

    I want to have an "If (something AND something) then....."
    but when I try using && I get errors...

    Here's what I tried (basically)
    Code:
    if (GetAsyncKeyState(VK_F8) && x > 1) {
         action... }
    Thanks for any help.

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: If Statement && Help

    My crystalball broke down last week... ordered a new one, but I'm still waiting for it. So, while I'm waiting for my new crystalball, maybe you could tell us which errors you get, because right now, I can't read your mind.

  3. #3
    Join Date
    Jun 2009
    Posts
    33

    Re: If Statement && Help

    Quote Originally Posted by Skizmo View Post
    My crystalball broke down last week... ordered a new one, but I'm still waiting for it. So, while I'm waiting for my new crystalball, maybe you could tell us which errors you get, because right now, I can't read your mind.
    Lol, well I figured that someone would be able to give an example of what the if statement would look like...

    I'm getting errors for different things because of how I wrote the code out, kind'a badly, but either way, could you give a small example of using an if statement with the "&&" and I can use that to make my own. I don't want someone to do it for me, like tell me what code I need, I'm trying to learn how to do everything by myself so I wont need a lot of help in the future...

    Thanks.

  4. #4
    Join Date
    Jul 2002
    Location
    Seattle Area, WA
    Posts
    241

    Re: If Statement && Help

    if (GetAsyncKeyState(VK_F8) && x > 1)
    is this supposed to be:

    if ( (GetAsyncKeyState(VK_F8) && x ) > 1)

    or

    if (GetAsyncKeyState(VK_F8) && (x > 1) )

    Compiler probably doesn't know? You have two evaluations there.

  5. #5
    Join Date
    Jun 2009
    Posts
    33

    Re: If Statement && Help

    Quote Originally Posted by KingTermite View Post
    is this supposed to be:

    if ( (GetAsyncKeyState(VK_F8) && x ) > 1)

    or

    if (GetAsyncKeyState(VK_F8) && (x > 1) )

    Compiler probably doesn't know? You have two evaluations there.
    Thanks to you, I got it... I needed an extra pair of () around it...

    Here's what worked.
    [code]if ((GetAsyncKeyState(VK_F8)) && z > 1)]/code]

    Thanks for helping.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: If Statement && Help

    Other than you changed x to z, there's no reason adding the extra parenthesis would have any effect on compilation or execution. They're superfluous in that case. Something else changed as well.

  7. #7
    Join Date
    Jun 2009
    Posts
    33

    Re: If Statement && Help

    Quote Originally Posted by GCDEF View Post
    Other than you changed x to z, there's no reason adding the extra parenthesis would have any effect on compilation or execution. They're superfluous in that case. Something else changed as well.
    Idk, I'm using Visual C++ Express 2008 and that's all I had to change for it to work. And I just copied and pasted that time, because the variable in there was z, I just used x when I typed it here. Anyways, I don't know why if that ain't the reason, but I did it in 10 different spots, and having the extra () around it got rid of all the errors.

  8. #8
    Join Date
    Jul 2002
    Location
    Seattle Area, WA
    Posts
    241

    Re: If Statement && Help

    Quote Originally Posted by GCDEF View Post
    Other than you changed x to z, there's no reason adding the extra parenthesis would have any effect on compilation or execution. They're superfluous in that case. Something else changed as well.
    I agree. I don't understand why they would have made a difference where you put them.

  9. #9
    Join Date
    Jun 2009
    Posts
    33

    Re: If Statement && Help

    Quote Originally Posted by KingTermite View Post
    I agree. I don't understand why they would have made a difference where you put them.
    Maybe it's because I have so many if's inside one another? Not sure, but it says I don't have an if for an else if I don't have the extra (). Either way, as long as it works lol.

  10. #10
    Join Date
    Jul 2002
    Location
    Seattle Area, WA
    Posts
    241

    Re: If Statement && Help

    Quote Originally Posted by Tesujin View Post
    Either way, as long as it works lol.
    Not knowing "why" it works is almost as bad as it not working, IMO.

  11. #11
    Join Date
    Jun 2009
    Posts
    33

    Re: If Statement && Help

    Quote Originally Posted by KingTermite View Post
    Not knowing "why" it works is almost as bad as it not working, IMO.
    Yeah but it seems like it makes sense to me... i mean..

    ((getkey(vkf8)) && (z > 1))

    1. beginning of whole thing
    2. beginning of getasynckey
    3. beginning of key
    4. close key
    5. close getasynckey
    6. open z>1
    7. close z>1
    8. close whole thing

    thats what it seemed like to me... sorry if im wrong..

  12. #12
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: If Statement && Help

    Quote Originally Posted by Tesujin View Post
    Maybe it's because I have so many if's inside one another? Not sure, but it says I don't have an if for an else if I don't have the extra (). Either way, as long as it works lol.
    May be while you were adding those unneeded parenthesis, you accidentally fixed some mismatched ones?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  13. #13
    Join Date
    Jun 2009
    Posts
    33

    Re: If Statement && Help

    Quote Originally Posted by VladimirF View Post
    May be while you were adding those unneeded parenthesis, you accidentally fixed some mismatched ones?
    But if that was the case, I would have gotten errors before adding the extra &&'s to the if statement. Also, I added opening and closing parenthesis, so wouldn't the unmatched ones still be there?

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: If Statement && Help

    Quote Originally Posted by Tesujin View Post
    But if that was the case, I would have gotten errors before adding the extra &&'s to the if statement. Also, I added opening and closing parenthesis, so wouldn't the unmatched ones still be there?
    I know you think that fixed it, but it didn't. Those extra parenthesis don't do anything in this case. There was something else.

  15. #15
    Join Date
    Jun 2005
    Posts
    315

    Re: If Statement && Help

    This statement in post #11
    Code:
    ((getkey(vkf8)) && (z > 1))
    is not the same as this one in post #5
    Code:
    if((GetAsyncKeyState(VK_F8)) && z > 1)
    I'm guessing you went with the former?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured