Help! Reading multiple lines w/ getline?

Hey y'all I am new to c++ and i needed a little help with this code. I am suppose modify my program to read an arbitrary number of lines from the console until a blank line is encountered. Then append the lines together, and output them to the console. Heres what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

using namespace std;

int main(void)
{
    string line, line2, jLines;
	
        cout << "Please enter your first line of text: " << endl;
	getline(cin, line);
	cout << "Please enter your second line of text: " << endl;
	getline(cin, line2);
	jLines = line + " " + line2;
	cout << jLines << endl;
	
	return 0;
}


Any help at all is greatly appreciated!
Last edited on
try getline(line); instead at line 11
and similarly in line 13
Last edited on
Nah that doesn't work. Its an error when I do that. I dont know to make it to read multiply line using getline
You'll need to use a loop.

In pseudo code:
1
2
3
4
string result
while ( we extract a non-empty line )
    add the line to result
display result
Last edited on
so how would i go about doing that. Like what would I put into the while loop?
okay I got that to work: but

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

using namespace std;

int main(void)
{
    string line = "a";
	string result;
	while (line.length() != 0)
	{
		cout << "Enter text line: " << endl;	
		getline(cin, line);
	    result = result + " " + line;
	
	}
	cout << result << endl;
	return 0;
}


but now I need to modify the program to count the number of words in the input (use isspace( ) to find breaks between words), and the number of phrases (as delineated by punctuation marks), and the number of sentences (delineated by a period)?
Last edited on
I would've written the loop like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

int main()
{
    std::string result ;
    std::string line ;
 
    while ( std::getline(std::cin, line) && !line.empty() )
        result += line + " " ;

    std::cout << result << '\n' ;
}


but now I need to modify the program to count the number of words in the input (use isspace( ) to find breaks between words), and the number of phrases (as delineated by punctuation marks), and the number of sentences (delineated by a period)?


And what are you having trouble with?
I dont get which other commands besides isspace() that you would use and where would you put them?
Okay, I fixed my code but how do i go about counting the number of words for the input. I need to use isspace() to find the breaks?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <cctype>

int main()
{
    std::string result ;
    std::string line ;
    std::cout << "Enter text: " << std::endl;
    while ( std::getline(std::cin, line) && !line.empty() )
		{
			result += line + " " ;
	    }
    std::cout << result << '\n' ;
}
Topic archived. No new replies allowed.