CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2018
    Posts
    158

    to Read from cin only a char

    I wish to read from standard input only a char which user will type.
    If user wrongs and types more character, only the first one will be stored to variable.
    My code is:

    Code:
    int main()
    {
        char cod;
        do {
            cout << "\n\nTypes the code : ";
            cin.get(cod);               // get char
            cin.ignore();               //flush buffer from multiple typings
            if ( cod=='|' || cod=='-' || cod=='/' )   
                cout << "ok";
        }    while (cod != 'q');    //permit to close main
    }
    Where did I make mistakes?!

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: to Read from cin only a char

    Text input works on input terminated by new-line ('\n') (or the enter key). .get() will wait until a new-line has been entered then return the next available character and subsequent calls to .get() will return chars until there are none available at which point it will wait until another new-line has been entered. So the first point is that even if you just want a single char, the user has to also enter new-line. If you then want to discard the remaining chars from the entered text then ignore() is used. However, the default values for .ignore() are not suitable for keyboard input as the default is to .ignore is EOF whereas for keyboard input this needs to be new-line and the max number of chars to ignore is 1. To ignore all the remaining chars from keyboard input you need

    Code:
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    So the code becomes

    Code:
    #include <iostream>
    #include <limits>
    using namespace std;
    
    int main()
    {
    	char cod;
    	do {
    		cout << "\n\nTypes the code : ";
    		cin.get(cod);               // get char
    		cin.ignore(numeric_limits<streamsize>::max(), '\n');               //flush buffer from multiple typings
    		if (cod == '|' || cod == '-' || cod == '/')
    			cout << "ok";
    	} while (cod != 'q');    //permit to close main
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    May 2018
    Posts
    158

    Re: to Read from cin only a char

    Quote Originally Posted by 2kaud View Post
    To ignore all the remaining chars from keyboard input you need
    Code:
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    It's wonderful, it works fine!
    If wanted to check inside limits library to see this function "numeric_limits<streamsize>::max()", where I can find it?
    I'm using Debian 3.2 with G++ 4.7.2.

    Thanks

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: to Read from cin only a char

    Quote Originally Posted by zio_mangrovia View Post
    It's wonderful, it works fine!
    If wanted to check inside limits library to see this function "numeric_limits<streamsize>::max()", where I can find it?
    I'm using Debian 3.2 with G++ 4.7.2.

    Thanks
    This is implemented in the file limits, which is included in the code. You can open this file and see the code. Note that this is advanced c++ meta-programming templated code.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured