I’m attempting to learn C++ and one exercise is to build a command line tool that accepts user input and stores it in a char array until the user enters a blank line. I think I have the skeleton right but for whatever reason my while runs continuously. My code is as follows:
char a[256];
//while the first character isn't a new line
while (a[0] != '\n') {
//get the char array
cin >> a;
cout << a;
}
Any help would be most appreciated.
You can’t detect newlines with
operator>>. For most types, it uses whitespace as a delimiter, and it doesn’t differentiate between spaces, tabs or newlines. Use getline instead: