Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How cin.get() works in loop ?

How cin.get() works in loop ?

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

    #include
    using namespace std;

    int main(){
    char c;
    cin.get(c); //get a charecter from keyboard and assigns it to c

    while(c != '\\n'){
        cout << c ;                 //displays the charecter on screen
        cin.get(c);                 //get another charecter
    }
    

    }

    int the code above shouldn't cout << c print every charecter i put in cin.get() in loop ?

    Quote:

    while(c != '\n'){ cout << c ; //displays the charecter on screen cin.get(c); //get another charecter }

    instead it print the string i enter collectively, for ex.

    Quote:

    input : "Jhon simon" output : "Jhon simon"

    please explain Thank you

    D L P J 4 Replies Last reply
    0
    • T Tarun Jha

      #include
      using namespace std;

      int main(){
      char c;
      cin.get(c); //get a charecter from keyboard and assigns it to c

      while(c != '\\n'){
          cout << c ;                 //displays the charecter on screen
          cin.get(c);                 //get another charecter
      }
      

      }

      int the code above shouldn't cout << c print every charecter i put in cin.get() in loop ?

      Quote:

      while(c != '\n'){ cout << c ; //displays the charecter on screen cin.get(c); //get another charecter }

      instead it print the string i enter collectively, for ex.

      Quote:

      input : "Jhon simon" output : "Jhon simon"

      please explain Thank you

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Your explanation is a bit unclear. If the "input:" and "output:" are the same, what exactly is the issue? With limited information, the only thing I can suggest is:

      cout << c << std::flush;

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      1 Reply Last reply
      0
      • T Tarun Jha

        #include
        using namespace std;

        int main(){
        char c;
        cin.get(c); //get a charecter from keyboard and assigns it to c

        while(c != '\\n'){
            cout << c ;                 //displays the charecter on screen
            cin.get(c);                 //get another charecter
        }
        

        }

        int the code above shouldn't cout << c print every charecter i put in cin.get() in loop ?

        Quote:

        while(c != '\n'){ cout << c ; //displays the charecter on screen cin.get(c); //get another charecter }

        instead it print the string i enter collectively, for ex.

        Quote:

        input : "Jhon simon" output : "Jhon simon"

        please explain Thank you

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        No, it prints each character as you type it.

        1 Reply Last reply
        0
        • T Tarun Jha

          #include
          using namespace std;

          int main(){
          char c;
          cin.get(c); //get a charecter from keyboard and assigns it to c

          while(c != '\\n'){
              cout << c ;                 //displays the charecter on screen
              cin.get(c);                 //get another charecter
          }
          

          }

          int the code above shouldn't cout << c print every charecter i put in cin.get() in loop ?

          Quote:

          while(c != '\n'){ cout << c ; //displays the charecter on screen cin.get(c); //get another charecter }

          instead it print the string i enter collectively, for ex.

          Quote:

          input : "Jhon simon" output : "Jhon simon"

          please explain Thank you

          P Offline
          P Offline
          Peter_in_2780
          wrote on last edited by
          #4

          Google "cout buffering" for an explanation.

          Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

          1 Reply Last reply
          0
          • T Tarun Jha

            #include
            using namespace std;

            int main(){
            char c;
            cin.get(c); //get a charecter from keyboard and assigns it to c

            while(c != '\\n'){
                cout << c ;                 //displays the charecter on screen
                cin.get(c);                 //get another charecter
            }
            

            }

            int the code above shouldn't cout << c print every charecter i put in cin.get() in loop ?

            Quote:

            while(c != '\n'){ cout << c ; //displays the charecter on screen cin.get(c); //get another charecter }

            instead it print the string i enter collectively, for ex.

            Quote:

            input : "Jhon simon" output : "Jhon simon"

            please explain Thank you

            J Offline
            J Offline
            jfbode1029
            wrote on last edited by
            #5

            cin.get() does not read individual keystrokes -- the input is buffered by the terminal until you hit Enter, then all of the input is sent to the program at once. To read individual keystrokes, you'll have to rely on a third-party tool like GNU's ncurses library.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups