String search and print

Please help me,

The program should work in a way where when you input a word it will search the file and prints out the whole paragraph that will stop if a white space that separates a paragraph from each other but the program that I made just searches the word and only print out the line that contains it not the whole paragraph.

Here's the code that I've got:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void searchemployee() {
	system("cls");
	ifstream infile;
	string employnum;
	string paragraph;
	infile.open("Employee.txt");
	if (infile.fail()){
		cout << "The file does not exist!";
		system("pause");
	}
	cin.ignore();
	cout << "SEARCH EMPLOYEE\n\n";
	cout << "Employee Number: ";
	getline(cin, employnum);
	while (getline(infile, paragraph)) {
		if (paragraph.find(employnum) != string::npos) {
			cout << paragraph << endl;
		}
	}
	system("pause");
}


I'm really sorry if I did wrong asking questions and for hard to understand English. I'm sorry but I really need help.
Last edited on
1
2
3
4
if (paragraph.find(employnum) != string::npos) {
   while(getline(infile paragraph) && !paragraph.empty())
      cout << paragraph << endl;
}


Assuming paragraphs are separated by leaving a line ("\n\n" to computers).
Create a function to read an entire paragraph and use that in place of getline at line 15:

1
2
3
4
istream &getParagraph(instream &is, std::string &para)
{
    // you write the code here.
}

Then line 15 becomes:
while (getParagraph(infile, paragraph)) {

While you're developing get Paragraph, add some temporary code to print the paragraph that it creates. This will you debug it.
I dont know how to reply here for Im new but anyways thanks for the help it really helped alot.
Topic archived. No new replies allowed.