Vyff
Vyff

Reputation: 83

How does one buffer output in C++ with new lines

I have some code that takes in a paragraph and starts a new line after every sentence.

I would like to buffer the output in the terminal with a new line, but adding "std::cout << endl;" outside of the loop does not seem to work. Any help in separating the input from the output if it is typed into a terminal.

I have commented out the code I expected to work, but which does not.

#include <iostream>
#include <string>
using namespace std;

int main() {

  // std::cout << std::endl;

  for (;;) {
    string line;
    getline(std::cin, line);
    if (!cin) {
      break;
    }
    for (unsigned int i = 0; i < line.length(); ++i) {
      const string text = line.substr(i, 1);
      if (text == "." || text == "?" || text == "!") {
          std::cout << text;
          std::cout << std::endl;
      }else{
          std::cout << text;
      }
    }
  }

  // std::cout << std::endl;

  return 0;
}

Upvotes: 0

Views: 319

Answers (1)

Coding Pig
Coding Pig

Reputation: 66

The commented line will never work since it will never be called. You have an endless loop for(;;) before it.

Upvotes: -1

Related Questions