Program to read input one line at a time.

Feb 25, 2019 at 4:22pm
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;
}
Feb 25, 2019 at 4:32pm
If your goal is to process each word in the file, then just read each word individually like you have in your example.

Otherwise, use a stringstream, which lets you access a copy of a string through the stream interface: http://www.cplusplus.com/reference/sstream/istringstream/:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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.


Feb 25, 2019 at 5:02pm
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?
Feb 25, 2019 at 5:17pm
closed account (E0p9LyTq)
Quick and dirty:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#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!
Feb 25, 2019 at 5:20pm
how would I tell the user to enter a line?

Just do a cout of "Please enter a sentence".

Note that using dhayden's example you would need to do so in two locations.
One before line 13, and again after line 19.

how do I get the program to terminate?

You need to test a condition to determine if the user is done. Perhaps if they enter an empty line, or if they enter the word "quit".

After line 13:
1
2
  if (line == "quit")
    return 0;  // Exits the program  


Note that if the user enters an EOF (value varies by OS), your while loop at line 13 will exit.
Last edited on Feb 25, 2019 at 5:21pm
Feb 25, 2019 at 5:34pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#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.
Feb 25, 2019 at 5:37pm
closed account (E0p9LyTq)
Move lines 16-19 to AFTER your while statement currently at line 20.
Feb 25, 2019 at 5:45pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#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
Feb 25, 2019 at 6:08pm
Your { at line 25 is misplaced. It should be after line 17.
Feb 25, 2019 at 6:47pm
Simple mistake. Thank you so much for helping me.
Feb 26, 2019 at 2:04pm
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)) {
Topic archived. No new replies allowed.