0

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.

Nick Chapman
  • 4,402
  • 1
  • 27
  • 41
  • Doesn't this sound a lot like [How do I flush the cin buffer?](http://stackoverflow.com/questions/257091/how-do-i-flush-the-cin-buffer?rq=1) ? – Bo Persson Oct 01 '15 at 19:57
  • @BoPersson which references the use of `cin.ignore` – Nick Chapman Oct 01 '15 at 19:57
  • You probably misunderstand how input from `cin` works. How you differentiate that user entered 2 symbols instead of one? – Slava Oct 01 '15 at 20:04

3 Answers3

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.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
0

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.

Slava
  • 43,454
  • 1
  • 47
  • 90
0

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;
    }
Santosderek
  • 106
  • 6