using the newline character to break a loop in c

Reading with >> by default skips whitespace, and a newline is whitespace. I suggest using getline() instead:

for(int i = 0; i < 80; i++) {
    if (!getline(std::cin, userInput) || userInput.empty())
    lineStorage[lineLength] = userInput;

If your lineStorage is really supposed to store individual words, you can split userInput on spaces before storing the words.

Edit: now that you’ve shown that userInput is a single character, I think you should just use std::cin.get(userInput) to read one character at a time. That will let you get the newlines in the style of your original code.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top