-1

Hellow guys i want to add nine integers to array at once time without pressing enter key in run time. please guys tell me how to add nine integers to array simultaneously in C++. Thanks!

Jeewantha
  • 13
  • 4
  • `std::cin` a `std::string` instead and parse it with some rule for deleminators. – George Nov 03 '17 at 08:02
  • See https://stackoverflow.com/a/1321154/3754223 – MABVT Nov 03 '17 at 08:03
  • 3
    Possible duplicate of [Convert String containing several numbers into integers](https://stackoverflow.com/questions/1321137/convert-string-containing-several-numbers-into-integers) – MABVT Nov 03 '17 at 08:04

1 Answers1

1

If you want to process each integer value right after its input in console is complete (e.g. in that a blank indicates that the next integer value shall begin), you are in a bad position.

The reason is that terminal input (beyond of what your C++ program can influence) often is buffered, and even cin might not receive any character until Enter or EOF is pressed in the terminal.

There may exist workarounds like conio.h or ncurses, but the are not standard and probably not worth the effort in your situation unless you really need to implement integer scanning for a production environment tightly connected to console input.

Try it out and compare input taken directly from console to input from a stream that is already "filled" with enough input:

int main() {

    stringstream ss("12 34 56 78 90 10 11 12 13");
    //istream &in = ss;   // would output each integer immediately.

    istream &in = cin;  // will probably wait for enter before processing begins.

    int value = 0;
    for (int i=0; i<9; i++) {
        if (! (in >> value))
            break;
        cout << value << "; ";
    }   
}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58