Thread: help with cin.get() program trying to use cin.get instead of getch()

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    32

    Exclamation help with cin.get() program trying to use cin.get instead of getch()

    Hi ! everyone I was trying to use cin.get() like we used to use getch() .
    By which I mean using getch()/cin.get() to pause screen till a character is entered.

    Here is the code

    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
         
        cout<<"hi";
       
        cin.get();
    }
    but when I tried it with this code it failed in first try

    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
        char a;
        int i;
        
        cout<<"hi";
        
        a=i=cin.get();
        
        cout<<a<<endl<<i;
    
        
        cin.get();
        
    }
    Then I tried by adding one more cin.get() and it worked

    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
        char a;
        int i;
        
        cout<<"hi";
        
        a=i=cin.get();
        
        cout<<a<<endl<<i;
    
        
        cin.get();
            
        cin.get();
    }
    Then I thought what value is this second last cin.get() is taking so I tried to save its value in another variable.

    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
        char a;
        int i,j;
        
        cout<<"hi";
        
        a=i=cin.get();
        
        cout<<a<<endl<<i;
    
        
        j=cin.get();
        cout<<endl<<j;
        
        cin.get();
    }
    \
    And to my wonder it was always 10 which by ASCII convention means line feed. So can anyone guide me why is this last cin.get() is taking value automatically

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    And to my wonder it was always 10 which by ASCII convention means line feed. So can anyone guide me why is this last cin.get() is taking value automatically
    Because it remains stranded in the input buffer.
    If it is only one character, use
    Code:
     cin.ignore();
    to discard it.
    And in situaions, where you're not sure how many left-overs are there,
    use
    Code:
    cin.ignore(numeric_limits<int>::man(),'\n');

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    32
    Thanks buddy .I really appreciate your concern and thank you again for helping me.
    If you may, can you explain the cin.ignore or can you give me website referrence from where I can understand it better.
    And the is the numeric_limit is any integer.
    Regds
    Xperience

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Well.... as the name says.. it ignores !
    Also, I just found out that there was two little errors in my previous post ...one being a typo
    It should be...
    Code:
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    [15] Input/output via <iostream> and <cstdio>, C++ FAQ

    And numeric limits:
    numeric_limits - C++ Reference
    You could use any integer, but this way gives the maximum possible value.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    32
    Thank You

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getch Refresh? Simple Problems with Getch()
    By justin777 in forum C Programming
    Replies: 7
    Last Post: 10-26-2011, 01:21 AM
  2. Pause a C program without getch()
    By swgh in forum C Programming
    Replies: 4
    Last Post: 02-20-2006, 11:24 AM
  3. Help with getch()
    By boontune in forum C++ Programming
    Replies: 1
    Last Post: 01-20-2003, 08:01 AM
  4. Using getch()
    By pr0ficient in forum Linux Programming
    Replies: 2
    Last Post: 07-26-2002, 05:59 AM
  5. Problems with Getch() program execution order
    By napkin111 in forum C++ Programming
    Replies: 4
    Last Post: 05-08-2002, 01:44 PM