Reading a key inside a loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hstagni

    Reading a key inside a loop

    When i read a key using getchar() inside a loop, the program stops and
    wait for a key to be pressed. I actually want the program to continue
    its execution until a key is pressed. Look at this sample:
    ------
    while(1=1)
    {
    printf("oi");
    ch=getchar();
    if (ch=='q') break;
    }

    I want the program to print 'hi' forever(somethi ng like
    "hihihihihihihi hihihihihihi" until 'q' is pressed. Is there an easy
    way to do this?
    (this program actually stop the printing of 'hi' and waits for a key
    to be pressed. I dont want this!)


    Sorry about the bad english

  • mark_bluemel@pobox.com

    #2
    Re: Reading a key inside a loop

    On 29 Mar, 16:55, "hstagni" <sta...@gmail.c omwrote:
    When i read a key using getchar() inside a loop, the program stops and
    wait for a key to be pressed. I actually want the program to continue
    its execution until a key is pressed.
    FAQ 19.1 - see http://c-faq.com/ or more particularly http://c-faq.com/osdep/cbreak.html
    Look at this sample:
    ------
    while(1=1)
    What's wrong with for(;;) or while(1) ?
    {
    printf("oi");
    ch=getchar();
    if (ch=='q') break;
    >
    }
    >
    I want the program to print 'hi' forever
    Well it won't do so when you've coded "oi"...

    Comment

    • Army1987

      #3
      Re: Reading a key inside a loop


      "hstagni" <stagni@gmail.c omha scritto nel messaggio
      news:1175183746 .537768.67330@n 76g2000hsh.goog legroups.com...
      When i read a key using getchar() inside a loop, the program stops and
      wait for a key to be pressed. I actually want the program to continue
      its execution until a key is pressed. Look at this sample:
      ------
      while(1=1)
      {
      printf("oi");
      ch=getchar();
      if (ch=='q') break;
      }
      >
      I want the program to print 'hi' forever(somethi ng like
      "hihihihihihihi hihihihihihi" until 'q' is pressed. Is there an easy
      way to do this?
      (this program actually stop the printing of 'hi' and waits for a key
      to be pressed. I dont want this!)
      Why do you want that?
      Anyway, there is no portable way of doing that. It depends on what system
      you're working on.



      Comment

      • Army1987

        #4
        Re: Reading a key inside a loop

        "hstagni" <stagni@gmail.c omha scritto nel messaggio
        news:1175183746 .537768.67330@n 76g2000hsh.goog legroups.com...
        while(1=1)
        This tries to assign 1 to 1. But you can't assign to a decimal constant.
        You meant while (1==1), or, as mark_bluemel pointed out, while (1).


        Comment

        • Mark McIntyre

          #5
          Re: Reading a key inside a loop

          On 29 Mar 2007 08:55:46 -0700, in comp.lang.c , "hstagni"
          <stagni@gmail.c omwrote:
          >I want the program to print 'hi' forever(somethi ng like
          >"hihihihihihih ihihihihihihi" until 'q' is pressed. Is there an easy
          >way to do this?
          This is a FAQ. - 19.1

          Quick answer: you can't, C's standard IO functions all require you to
          press a key to tell the programme you've finished typing.
          Longer answer: your OS probably has a lower level function which can
          do this. You may need to find out how to multithread your code.
          --
          Mark McIntyre

          "Debugging is twice as hard as writing the code in the first place.
          Therefore, if you write the code as cleverly as possible, you are,
          by definition, not smart enough to debug it."
          --Brian Kernighan

          Comment

          • hstagni

            #6
            Re: Reading a key inside a loop

            OK,,,THANK YOU ALL
            (I was trying to write while(1==1), cause for(;;) is kind of dirty to
            me :P )
            >>>Why do you want that?
            I am trying to convert my Pascal programs to C. But I made a Tetris
            in Pascal, and the main loop of the program has a function that keeps
            redrawing the screen with a delay(To make an animation) and this same
            main loop reads a key from the keyboard that moves the objects on the
            screen. On C, if i use getchar(), it would stop the animation and wait
            for a key to be pressed: the game would suck :P
            >>>>About the FAQ
            This FAQ you sent me does not applie(?) to my question(sorry if im
            wrong). Of course I will have to use something like ncurses getchar().
            However, this function has the same 'problem' i mentioned before: it
            WAITS for a key, stopping my tetris animation :(

            So, how can i do it?
            In Pascal a simple "ch=readkey ;" would solve my problem :(




            Comment

            • Keith Thompson

              #7
              Re: Reading a key inside a loop

              "hstagni" <stagni@gmail.c omwrites:
              OK,,,THANK YOU ALL
              (I was trying to write while(1==1), cause for(;;) is kind of dirty to
              me :P )
              "while (1)" and "for (;;)" are the most common idiomatic ways to write
              a loop in C. "while (1==1)" will just cause your readers to scratch
              their heads.

              [...]
              >>>>>About the FAQ
              This FAQ you sent me does not applie(?) to my question(sorry if im
              wrong). Of course I will have to use something like ncurses getchar().
              However, this function has the same 'problem' i mentioned before: it
              WAITS for a key, stopping my tetris animation :(
              Mark pointed you to question 19.1. Question 19.2 is actually more
              applicable to what you're trying to do. But the conclusion is the
              same: there's no way to do what you're trying to do in standard C, but
              there's likely to be a system-specific way to do it (which you'll have
              to ask about elsewhere).

              And please learn to quote properly. The Google Groups interface does
              this for you. You may find the following links useful:





              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
              "We must do something. This is something. Therefore, we must do this."
              -- Antony Jay and Jonathan Lynn, "Yes Minister"

              Comment

              • J. J. Farrell

                #8
                Re: Reading a key inside a loop

                On Mar 29, 4:58 pm, Keith Thompson <k...@mib.orgwr ote:
                "hstagni" <sta...@gmail.c omwrites:
                OK,,,THANK YOU ALL
                (I was trying to write while(1==1), cause for(;;) is kind of dirty to
                me :P )
                >
                "while (1)" and "for (;;)" are the most common idiomatic ways to write
                a loop in C. "while (1==1)" will just cause your readers to scratch
                their heads.
                Hardly. To someone who knows C its meaning is immediately clear. Since
                it's an unusual form it might take a bit longer to parse, but even if
                it takes as much as an extra hundredth of a second it's not that big a
                deal. To people who haven't yet got a firm grasp of C's concept of
                truth, it's meaning is probably far more obvious than the idiomatic
                form.

                Comment

                • Anthony Irwin

                  #9
                  Re: Reading a key inside a loop

                  hstagni wrote:
                  OK,,,THANK YOU ALL
                  (I was trying to write while(1==1), cause for(;;) is kind of dirty to
                  me :P )
                  >
                  >>>Why do you want that?
                  I am trying to convert my Pascal programs to C. But I made a Tetris
                  in Pascal, and the main loop of the program has a function that keeps
                  redrawing the screen with a delay(To make an animation) and this same
                  main loop reads a key from the keyboard that moves the objects on the
                  screen. On C, if i use getchar(), it would stop the animation and wait
                  for a key to be pressed: the game would suck :P
                  >
                  >>>>>About the FAQ
                  This FAQ you sent me does not applie(?) to my question(sorry if im
                  wrong). Of course I will have to use something like ncurses getchar().
                  However, this function has the same 'problem' i mentioned before: it
                  WAITS for a key, stopping my tetris animation :(
                  >
                  So, how can i do it?
                  In Pascal a simple "ch=readkey ;" would solve my problem :(
                  >
                  <OT>
                  Have you looked at the NCurses howto it explains that by default it
                  will buffer the characters but by using raw() or cbreak() it will not
                  buffer the characters and send the key press directly to the program.



                  There is even a code example of how to use it.
                  </OT>

                  Kind Regards,
                  Anthony Irwin

                  Comment

                  • Keith Thompson

                    #10
                    Re: Reading a key inside a loop

                    "J. J. Farrell" <jjf@bcs.org.uk writes:
                    On Mar 29, 4:58 pm, Keith Thompson <k...@mib.orgwr ote:
                    >"hstagni" <sta...@gmail.c omwrites:
                    OK,,,THANK YOU ALL
                    (I was trying to write while(1==1), cause for(;;) is kind of dirty to
                    me :P )
                    >>
                    >"while (1)" and "for (;;)" are the most common idiomatic ways to write
                    >a loop in C. "while (1==1)" will just cause your readers to scratch
                    >their heads.
                    >
                    Hardly. To someone who knows C its meaning is immediately clear. Since
                    it's an unusual form it might take a bit longer to parse, but even if
                    it takes as much as an extra hundredth of a second it's not that big a
                    deal. To people who haven't yet got a firm grasp of C's concept of
                    truth, it's meaning is probably far more obvious than the idiomatic
                    form.
                    I should have been clearer. Someone who knows C won't scratch his
                    head wondering "What does that mean?". He'll more likely scratch his
                    head wondering "Why didn't the author just write while (1)?".

                    Attempting to write "while (1==1)" also creates a vulnerability to the
                    error of writing "while (1=1)", which is exactly what the OP initially
                    did.

                    --
                    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                    San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                    "We must do something. This is something. Therefore, we must do this."
                    -- Antony Jay and Jonathan Lynn, "Yes Minister"

                    Comment

                    • Anthony Irwin

                      #11
                      Re: Reading a key inside a loop

                      mark_bluemel@po box.com wrote:
                      >
                      What's wrong with for(;;) or while(1) ?
                      Is there any advantages over using a for or a while or are they
                      essentially the same? I know they perform the same task but is there
                      any technical benefits of one over the other e.g. one faster then other.

                      I normally use while(1) and never really considered using for(;;)


                      Kind Regards,
                      Anthony Irwin

                      Comment

                      • CBFalconer

                        #12
                        Re: Reading a key inside a loop

                        hstagni wrote:
                        >
                        .... snip ...
                        >
                        So, how can i do it?
                        In Pascal a simple "ch=readkey ;" would solve my problem :(
                        No it didn't. It may have on your particular Pascal
                        implementation, using some sort of extension. Same applies to C.

                        --
                        Chuck F (cbfalconer at maineline dot net)
                        Available for consulting/temporary embedded and systems.
                        <http://cbfalconer.home .att.net>



                        --
                        Posted via a free Usenet account from http://www.teranews.com

                        Comment

                        • J. J. Farrell

                          #13
                          Re: Reading a key inside a loop

                          On Mar 29, 6:50 pm, Keith Thompson <k...@mib.orgwr ote:
                          "J. J. Farrell" <j...@bcs.org.u kwrites:
                          On Mar 29, 4:58 pm, Keith Thompson <k...@mib.orgwr ote:
                          "hstagni" <sta...@gmail.c omwrites:
                          OK,,,THANK YOU ALL
                          (I was trying to write while(1==1), cause for(;;) is kind of dirty to
                          me :P )
                          >
                          "while (1)" and "for (;;)" are the most common idiomatic ways to write
                          a loop in C. "while (1==1)" will just cause your readers to scratch
                          their heads.
                          >
                          Hardly. To someone who knows C its meaning is immediately clear. Since
                          it's an unusual form it might take a bit longer to parse, but even if
                          it takes as much as an extra hundredth of a second it's not that big a
                          deal. To people who haven't yet got a firm grasp of C's concept of
                          truth, it's meaning is probably far more obvious than the idiomatic
                          form.
                          >
                          I should have been clearer. Someone who knows C won't scratch his
                          head wondering "What does that mean?". He'll more likely scratch his
                          head wondering "Why didn't the author just write while (1)?".
                          If it used integer constants as in this case, and especially if it
                          appeared all over the code, I'd just read it as useful documentation
                          meaning one of two things: "the author didn't really understand
                          'truth' in C and might be a newbie - so watch out" or "the author is
                          someone who likes doing things in his own idiosyncratic ways - so
                          watch out".

                          If he were comparing a variable to itself, or if this odd construct
                          only occurred once among lots of "normal" cases, I would scratch my
                          head worrying about its being a typo.
                          Attempting to write "while (1==1)" also creates a vulnerability to the
                          error of writing "while (1=1)", which is exactly what the OP initially
                          did.
                          True, though the compiler would slap his wrist hard for that so others
                          shouldn't end up having to read it.

                          Just to be clear - I'm not advocating using this style, but I think it
                          accidentally gives useful clues to subsequent readers rather than
                          puzzling them.

                          Comment

                          • Richard Heathfield

                            #14
                            Re: Reading a key inside a loop

                            Keith Thompson said:
                            "hstagni" <stagni@gmail.c omwrites:
                            >OK,,,THANK YOU ALL
                            >(I was trying to write while(1==1), cause for(;;) is kind of dirty
                            >to me :P )
                            >
                            "while (1)" and "for (;;)" are the most common idiomatic ways to write
                            a loop in C.
                            I disagree. I for one consider both these forms to be aberrations rather
                            than idioms. The most common idiomatic ways to write a loop in C are
                            surely:

                            for(i = start; i < stop; i += inc)

                            and

                            while(condition al_expression)


                            "while (1==1)" will just cause your readers to scratch
                            their heads.
                            No more so than the other two.

                            --
                            Richard Heathfield
                            "Usenet is a strange place" - dmr 29/7/1999

                            email: rjh at the above domain, - www.

                            Comment

                            • santosh

                              #15
                              Re: Reading a key inside a loop


                              Anthony Irwin wrote:
                              hstagni wrote:
                              OK,,,THANK YOU ALL
                              (I was trying to write while(1==1), cause for(;;) is kind of dirty to
                              me :P )
                              >>Why do you want that?
                              I am trying to convert my Pascal programs to C. But I made a Tetris
                              in Pascal, and the main loop of the program has a function that keeps
                              redrawing the screen with a delay(To make an animation) and this same
                              main loop reads a key from the keyboard that moves the objects on the
                              screen. On C, if i use getchar(), it would stop the animation and wait
                              for a key to be pressed: the game would suck :P
                              >>>>About the FAQ
                              This FAQ you sent me does not applie(?) to my question(sorry if im
                              wrong). Of course I will have to use something like ncurses getchar().
                              However, this function has the same 'problem' i mentioned before: it
                              WAITS for a key, stopping my tetris animation :(

                              So, how can i do it?
                              In Pascal a simple "ch=readkey ;" would solve my problem :(
                              >
                              <OT>
                              Have you looked at the NCurses howto it explains that by default it
                              will buffer the characters but by using raw() or cbreak() it will not
                              buffer the characters and send the key press directly to the program.
                              >

                              >
                              There is even a code example of how to use it.
                              </OT>
                              But it'd still block for input, something the OP doesn't want. He
                              wants an asynchronous notification, on a key press.

                              He might have to look into signals, or as Mark McIntyre notes,
                              multithread his program.

                              Comment

                              Working...