Reputation: 83
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
Reputation: 66
The commented line will never work since it will never be called. You have an endless loop for(;;)
before it.
Upvotes: -1