How to quit using 'Enter'. Advise please...

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

    How to quit using 'Enter'. Advise please...

    Hello All
    Part of the code I am working on requires that a loop exits upon the 'ENTER'
    key being pressed.
    I would have thought its possible by doing something like the following:

    while(input != '\n')
    ...do something;

    I tried this but it obviously does not work. So it seems I must use the
    Ascii
    value of 'ENTER' key to quit in the while loop. How do i do this? Thanks
    much.


  • Kevin Goodsell

    #2
    Re: How to quit using 'Enter'. Advise please...

    jelly wrote:
    [color=blue]
    > Hello All
    > Part of the code I am working on requires that a loop exits upon the 'ENTER'
    > key being pressed.
    > I would have thought its possible by doing something like the following:
    >
    > while(input != '\n')
    > ...do something;
    >
    > I tried this but it obviously does not work. So it seems I must use the
    > Ascii
    > value of 'ENTER' key to quit in the while loop. How do i do this? Thanks
    > much.
    >
    >[/color]

    It has nothing to do with ASCII. ASCII is not required to be supported
    in any way by a C++ implementation. We need real code to tell you what's
    wrong.



    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.

    Comment

    • Mike Wahler

      #3
      Re: How to quit using 'Enter'. Advise please...


      "jelly" <jelly@yahoo.co m> wrote in message
      news:Mnofb.7190 $sr1.620@newssv r29.news.prodig y.com...[color=blue]
      > Hello All
      > Part of the code I am working on requires that a loop exits upon the[/color]
      'ENTER'[color=blue]
      > key being pressed.
      > I would have thought its possible by doing something like the following:
      >
      > while(input != '\n')
      > ...do something;
      >
      > I tried this but it obviously does not work. So it seems I must use the
      > Ascii
      > value of 'ENTER' key to quit in the while loop. How do i do this? Thanks
      > much.[/color]

      We cannot diagnose code we cannot see.
      However, here is one of many possible approaches:

      std::string& getinput()
      {
      static std::string s;
      std::getline(st d::cin, s);
      }

      while(!getinput ().empty())
      {
      /* whatever */
      }

      -Mike



      Comment

      • Jelly

        #4
        Re: How to quit using 'Enter'. Advise please...

        Thanks guys. I did not post the whole code because in the past I have been
        told to only post the specific piece ie the while loop.

        Thanks again.


        "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
        news:Unpfb.581$ Qy2.528@newsrea d4.news.pas.ear thlink.net...[color=blue]
        >
        > "jelly" <jelly@yahoo.co m> wrote in message
        > news:Mnofb.7190 $sr1.620@newssv r29.news.prodig y.com...[color=green]
        > > Hello All
        > > Part of the code I am working on requires that a loop exits upon the[/color]
        > 'ENTER'[color=green]
        > > key being pressed.
        > > I would have thought its possible by doing something like the following:
        > >
        > > while(input != '\n')
        > > ...do something;
        > >
        > > I tried this but it obviously does not work. So it seems I must use the
        > > Ascii
        > > value of 'ENTER' key to quit in the while loop. How do i do this? Thanks
        > > much.[/color]
        >
        > We cannot diagnose code we cannot see.
        > However, here is one of many possible approaches:
        >
        > std::string& getinput()
        > {
        > static std::string s;
        > std::getline(st d::cin, s);
        > }
        >
        > while(!getinput ().empty())
        > {
        > /* whatever */
        > }
        >
        > -Mike
        >
        >
        >[/color]


        Comment

        • Mike Wahler

          #5
          Re: How to quit using 'Enter'. Advise please...


          "Jelly" <Jelly@yahoo.co m> wrote in message
          news:YsFfb.7349 $Su1.5273@newss vr29.news.prodi gy.com...[color=blue]
          > Thanks guys. I did not post the whole code because in the past I have been
          > told to only post the specific piece ie the while loop.[/color]

          [Please don't top-post.]

          When asking a question about your code, you need to post
          enough of it to provide sufficient context to those reading.

          All you originally posted was:

          while(input != '\n')
          ...do something;

          ... which could only be answered if context were provided.

          A better code fragment would have been a complete small
          program which showed how/where the 'input' was obtained,
          what the type of 'input' is, how you attempted to evaluate
          and act upon it, etc.

          -Mike


          Comment

          Working...