Thread: Cin.get()

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    Cin.get()

    I have one question using cin.get(). I have posted the code below. The problem is, when I run the program it terminates right away, and doesnt wait for the user to input the information. Why?
    Code:
    void get_info()
    	{
    		cout << "Enter the blocker hand (L or R)";
    		cin.get(blocker_hand, HAND);	
    		cout << endl;
    		cout << "Enter the glove hand (L or R)";
    		cin.get(glove_hand, HAND);
    	}
    // declerations
    private:
    char glove_hand[HAND];
    char blocker_hand[HAND];
    and HAND is a constant of length 1.

    There is more code to the actual program, but this is the only part causing the problem.

    Thanks
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    There are two problems. First, the minimum length for a string is 2 chars, period. That accomadates a single char and the null-terminating zero. Second, cin.get() does not flush the newline character from stdin. Try instead:

    Code:
    const int HAND = 2;
    char blocker_hand[2];
    char glove_hand[2];
    //....
            cout << "Enter the blocker hand (L or R)";
    		cin.get(blocker_hand, HAND);	
    		cout << endl;
                                    fflush(stdin);
    		cout << "Enter the glove hand (L or R)";
    		cin.get(glove_hand, HAND);
    //...
    Cheers.
    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;
    }

  3. #3
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Thanks, I forgot obout the terminating character. It still, however, does not let me get the second input.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Did you forget to use fflush()? It should be placed between the two inputs as my post described. If all else fails, use the time - honored fgets()!
    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;
    }

  5. #5
    elad
    Guest
    i've read several knowledgeable sources that recommend avoiding use of C style buffer handling (fflush) and C++ style buffer handling (get()) in the same program. My advice is to use getline() in place of get(). getline() will remove the terminating char from the buffer, whereas get() and >> will not. Alternatively, use ignore() with reasonable parameters.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    const int HAND = 2;
    char blocker_hand[2];
    char glove_hand[2];
    //....
        cout << "Enter the blocker hand (L or R)";
        cin.get(blocker_hand, HAND);	
        cout << endl;
        fflush(stdin);
        cout << "Enter the glove hand (L or R)";
        cin.get(glove_hand, HAND);
    //...
    You've been here long enough to know better than that Sebastiani. fflush ( stdin ) is undefined and there are better ways to do this in C++. Look into cin.ignore() for a good way to discard extraneous characters in the input stream.

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

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Contrary to what everyone seems to say about fflush(), it works fine on my compilers, though again, I never use fflush() since I am comfortable with fgets() and hence never need it, (nor cin or scanf() for that matter). But you are right. No sense in teaching others bad habits
    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;
    }

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Contrary to what everyone seems to say about fflush(), it works fine on my compilers
    It works peachy on mine as well, but I'd rather put my trust in the C++ standard than in Microsoft.

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

  10. #10
    foniks munkee
    Guest
    The problem is that Microsoft actually have examples in the VC++ help using fflush(stdin). But then again, they also have "void main()"..

  11. #11
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784

    Re: Cin.get()

    Originally posted by MethodMan
    I have one question using cin.get(). I have posted the code below. The problem is, when I run the program it terminates right away, and doesnt wait for the user to input the information. Why?
    Code:
    void get_info()
    	{
    		cout << "Enter the blocker hand (L or R)";
    		cin.get(blocker_hand, HAND);	
    		cout << endl;
    		cout << "Enter the glove hand (L or R)";
    		cin.get(glove_hand, HAND);
    	}
    // declerations
    private:
    char glove_hand[HAND];
    char blocker_hand[HAND];
    and HAND is a constant of length 1.

    There is more code to the actual program, but this is the only part causing the problem.

    Thanks
    Not too sure because I have not used this method, but if you just want character input than:

    char a, b;

    cin >> a;
    cin >> b;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.get(); not working with my switch statement
    By tenor_jazz13 in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2008, 10:33 PM
  2. cin.get() problem
    By Cilius in forum C++ Programming
    Replies: 20
    Last Post: 07-28-2005, 05:32 PM
  3. Confused about cin.get(); and classes.
    By RaccoonKing in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2005, 11:44 AM
  4. cin.get();
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2005, 07:51 AM
  5. curiosity about cin.get() and cin.getline()
    By ssjnamek in forum C++ Programming
    Replies: 18
    Last Post: 11-30-2003, 01:26 AM