Search code examples
c++cycle

How does cin.get() works in the next code?(C++)


I got this example, but I can't figure out "what is what", please help

#include iostream

using namespace std;

void main() {
    char a[10],
         car,
         i = 0;
    while (i < 10)
        if(car = cin.get() != '\n')
            a[i++] = car;
    cout << a << endl;

    cin.get();
}

Solution

  • cin.get() returns a character which is stored in car, and if that character is != newline, then the while loop continues ( of course, as long as i < 10 ). So basically, your code waits for characters to be introduced in the console and stores them in your array, then displays the whole array.