Thread: contradictory if statements

  1. #1
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274

    Question contradictory if statements

    just wondering how to do this code, because i have 2 if statements in different functions, one says to do one thing if it gets left arrow, other one does something different when left arrow is it.

    code:

    void function1 ()
    {
    key=getch();

    if(key==KEY_LEFT) //already declared key left before
    printf("item one");
    }

    void function2 ()
    {
    key2=getch();

    if(key2==KEY_LEFT)
    printf("SUCCESS!!!");
    }



    this code makes it always say "item one" and doesn't work when it is in the second funcion. help would be appreciated
    Last edited by red_baron; 05-03-2002 at 08:34 AM.

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    3
    I DUNNO????

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    It works for me, could you be more specific?

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    More code would help so I could see what functions are being called. Probably function1 is being called or when function2 is called KEY_LEFT is not being pressed.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you're taking about the cursor keys, these arrive as pairs of bytes, which you do not appear to take care of with your use of getch

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You are probably calling it like this:

    f1();
    f2();

    ...the first getch() recieves the left key, and the second recieves the return key which was pressed in the first function! As Salem said, two bytes were sent when you pressed enter, and getch() grabs a single byte!

    If you did this instead:


    Code:
    void function1 () 
    { 
    int key=getch(); 
    int discard = getch();
    
    if(key==KEY_LEFT) //already declared key left before 
    printf("item one"); 
    } 
    
    void function2 () 
    { 
    int key2=getch(); 
    int discard = getch();
    
    if(key2==KEY_LEFT) 
    printf("SUCCESS!!!"); 
    }
    ...it would probably work as you expected...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    k i tried some of the stuff u guys wrote, but i wasn't able to get anything to work. The
    int keys=getch();
    wouldn't work but here is all of the code if u have time plz read it, i dont know what to filter out to still have enough info but not over crowd the place.


    oh yeah e-mail me if u can help out

    [email protected]

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Write a simple routine like so

    Code:
    int my_getch ( void ) {
      int res = getch();
      if ( res == 0 ) {
        res = 256 + getch();
      }
      return res;
    }
    Normal keys are 0..255, extended keys are 256 to 511
    This will change the values of K_LEFT

    > else if (getch()==K_LEFT || K_RIGHT || K_UP || K_DOWN)
    This doesn't mean what you think it means

    Try
    ch = my_getch()
    if ( ch == K_LEFT || ch == K_RIGHT || ch == K_UP || ch == K_DOWN )

    > level1[ylvl][xlvl][yold-2][xold-3]
    A 4D array - seems a bit large to me

  9. #9
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    what can i say? the routine does not work, and all i need help on is the if statements, the rest i can figure out myself.

    i've tried the double getch's but that doesn't work either, it keeps going to my first function and doing whats it's not suppose to do.
    help is still needed......................


    and here's something extra:

  10. #10
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274

    Lightbulb documented code

    here i documeneted the code and took some stuff out so it would be a little easier to understand.
    plz some help me find this problem (its for my exam/project), i only need to find out why the if statements for getch doesn't work properly

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Boy you sure make it hard for people to help you
    > if u want to see level.h e-mail me: .....

    Anyway, comment out all of the code in main() and replace it with this...
    Code:
    int main ( ) {
      printf( "Key tester - Q to quit\n" );
      while ( (c=my_getch()) != 'Q' ) {
        printf("key=%d\n", c );
      }	
      return 0;
    }
    Does this produce the answers you expect?
    Like 65 if you press A
    and 328 if you press cursor-up arrow
    ?

    If it doesn't, then something else is broke (good idea to say what numbers you do get)

    If you do get those numbers, the logic in your code is broke

    For instance, you still seem to have
    #define K_LEFT 75
    #define K_UP 72
    #define K_RIGHT 77
    #define K_DOWN 80

    You need to add 256 to all of these (using my_getch)
    #define K_LEFT (75+256)
    etc
    Yes, () are a good idea here

  12. #12
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    ok, i tried it and i get this:

    up:
    key=224
    key=72

    down:
    key=224
    key=80

    left:
    key=224
    key=75

    right:
    key=224
    key=77

    reminds me of a reply previously, i get 2 numbers for the arrow keys and 1 for the normal keys like q=113,
    i'm not sure how to set this up, i tried what u said and the keys wouldn't do anything.

  13. #13
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    well i tried to fix it using this code

    Code:
    int my_getch2 ()
    {
       int input1, input2;
       input1=getch();
       if (input1==224) 
       {
          input2=getch();
          input1+=input2;
       }
       return input1;   
    }
    but i still have the same mistake where if i go to my message function (tab key) the up key should clear the screen but it goes back to my game function and moves the smiley.
    here's the exe file: (esc=exit)

  14. #14
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274

    Talking eureka! i've got it !!!!!

    finally after many many many many many many many hours of my time experimenting with different ways, using one function, trying out what different ppl suggested before i was able to get it to work, thanx to all the ppl who helped out, and here is my function which works

    Code:
     
    int my_getch ()                                 
    {
       int input=getch();
       if (input==224) 
          input=getch();
       else if (input==0)
          input=256+getch();
       return input;   
    }
    boy talk about a lot of time for such a little simple thing, but i'm so happy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. C++ If Statements Help
    By moporho in forum C++ Programming
    Replies: 19
    Last Post: 01-18-2008, 08:40 AM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  5. Statements and Functions
    By GeekFreak in forum C++ Programming
    Replies: 5
    Last Post: 08-15-2002, 12:34 PM