How do you flush the contents of `std::cin` before an additional read from it?

戏子无情 提交于 2019-12-25 04:12:06

问题


I am trying to read a single character multiple times. The catch is that I need to prevent user errors. So for example:

char arr[10];
for(int i = 0; i < 10; i++)
{
    cin.get(arr[i]);
} 

Where the inputs should be something like a, b, c, d, .... But if someone were to enter ab for the first entry I want to capture the a and then ignore the b. I know about cin.ignore however I don't know how I would go about ignoring an arbitrary number of alphanumeric characters or symbols considering that I want to ignore a potentially unlimited number of characters and then stop ignoring and read again.

How can I either ignore an arbitrary number of characters and then stop ignoring or how can I actually flush the buffer for cin.


回答1:


Most input is line feed so if you want to ignore all characters in the input stream until you hit a newline then you could use:

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')

Since we ignore up to the streamsize there should not be an extra content in the input buffer.




回答2:


If you want user to hit enter after each symbol, then code could be as simple as this:

char arr[10];
for(int i = 0; i < 10; )
{
    std::string line;
    std::getline( std::cin, line );
    // check that line is not empty
    if( line.empty() ) {
         std::cout << "missing input" << std::endl;
         continue;
    }
    arr[i++] = line[0]; // get only first symbol and ignore the rest
} 

if you have something else in mind, I am afraid that will not work with std::cin - you do not see any input until user presses enter. In that case you would have to use OS specific functions to get unbuffered input.




回答3:


The following is the code that you want, if your inputing like this a 'enter' b 'enter' c 'enter' etc...

#include <iostream>
#include <string>

using namespace std;

int main() {


    char arr[10];
    string line;

    for (int i = 0; i < 10; i++)
    {
        getline(cin, line);
        arr[i] = line[0];

        cout << endl << "Here is the Char: " << arr[i] << endl;
    }



    return 0;
}

BUT if you enter input like this in one line: a,b,c,d,e,f,g,h,i,j 'enter' then you want the following code:

#include <iostream>
#include <string>

using namespace std;

int main() {


    char arr[10];
    string line;
    int i = 0;
    size_t  end;



        getline(cin, line);

        end = 0;
        int counter = 0;



            if (line != "") {

                while (end != string::npos && counter < 10) {

                    if (counter == 0) {
                        arr[counter] = line[0];
                    }
                    else {
                        end = line.find(",", end + 1);
                        arr[counter] = line[end + 1];
                    }

                    counter++;

                }
            }


            for (int i = 0; i < 10; i++) {
            cout << endl << "Here is the Char: " << arr[i] << endl;
        }







    return 0;
    }


来源:https://stackoverflow.com/questions/32895730/how-do-you-flush-the-contents-of-stdcin-before-an-additional-read-from-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!