So I am still very new to C++ and I am trying to write a code that reads the standard input one line at a time and then reads one word at a time. This is all that I have so far.
1 2 3 4 5 6 7
int main()
{
string word;
while (cin >> word)
cout << word << endl;
return 0;
}
#include <iostream>
#include <sstream>
#include <string>
using std::cin;
using std::cout;
using std::istringstream;
using std::string;
int
main()
{
string line;
while (getline(cin, line)) {
cout << "\n------ LINE IS " << line << '\n';
istringstream is(line);
string word;
while (is >> word) {
cout << "word: " << word << '\n';
}
}
return 0;
}
Input:
This is a test.
This is only a test.
Output:
------ LINE IS This is a test.
word: This
word: is
word: a
word: test.
------ LINE IS This is only a test.
word: This
word: is
word: only
word: a
word: test.
That was very helpful. However, how would I tell the user to enter a line? Like have it say "Please enter a sentence." and how do I get the program to terminate?
#include <iostream>
#include <sstream>
#include <string>
int main()
{
std::string line;
std::cout << "Enter lines of text, enter END to exit:\n\n";
while (std::getline(std::cin, line))
{
if (line == "END")
{
break;
}
std::cout << "\n------ LINE IS: " << line << '\n';
std::istringstream is(line);
std::string word;
while (is >> word)
{
std::cout << "word: " << word << '\n';
}
std::cout << '\n';
}
std::cout << "Good-bye!\n";
}
Enter lines of text, enter END to exit:
some text
------ LINE IS: some text
word: some
word: text
more text
------ LINE IS: more text
word: more
word: text
even more text
------ LINE IS: even more text
word: even
word: more
word: text
END
Good-bye!
#include <iostream>
#include <sstream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::istringstream;
using std::string;
int main()
{
string line;
cout << "Please enter your sentence: " << endl;
if (line == "FINISHED")
{
return 0;
}
while (getline(cin, line))
{
cout << "The line entered is: " << line << endl;
istringstream is(line);
string word;
while (is >> word)
{
cout << "The word is: " << word << endl;
}
}
return 0;
}
Please enter your sentence:
This is a test
The line entered is: This is a test
The word is: This
The word is: is
The word is: a
The word is: test
FINISHED
The line entered is: FINISHED
The word is: FINISHED
Can someone help me modify this? I have tried AbstractionAnon way and it stopped allowing program to read the sentence word by word.
#include <iostream>
#include <sstream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::istringstream;
using std::string;
int main()
{
string line;
cout << "Please enter your sentence: " << endl;
while (getline(cin, line))
if (line == "FINISHED")
{
return 0;
}
{
cout << "The line entered is: " << line << endl;
istringstream is(line);
string word;
while (is >> word)
{
cout << "The word is: " << word << endl;
}
}
return 0;
}
Output:
Please enter your sentence:
This is a test
What is wrong
FINISHED
Note that using dhayden's example you would need to do so in two locations.
Here's a great example of the use of (and reason for) the comma operator. It lets you avoid printing the prompt twice. while (cout << "Please enter a line: ", getline(cin, line)) {