Using while loop for a word search in text

Hello,

I am needing to write a word search program that will take the user's input and let them know how many times the word or phrase input is found in the passage below (assigned to "john3"). I am having great difficulty with my while loop, and am not quite sure how to set the parameters. Additionally, how do I use the find function to output number of times found rather than the indexes at which the user input is found? Here are the explicit directions for this program:

"Write a word search and word count program.

1) Assign the following text to a string constant.

For God so loved the world that he gave his one and only Son, that whoever believes in him shall not perish but have eternal life. For God did not send his Son into the world to condemn the world, but to save the world through him. Whoever believes in him is not condemned, but whoever does not believe stands condemned already because they have not believed in the name of God’s one and only Son. This is the verdict: Light has come into the world, but people loved darkness instead of light because their deeds were evil. Everyone who does evil hates the light, and will not come into the light for fear that their deeds will be exposed. 21 But whoever lives by the truth comes into the light, so that it may be seen plainly that what they have done has been done in the sight of God.

2) Prompt the user to enter a word or phrase

3) Search the string given by the user from the text above and inform how many times the word/phrase was found in the text.

(Hint: You need to use while loop, .find and .length function from string library. Also, you will need to use string::npos.)"

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
#include <iostream>
#include <string>
using namespace std;
int main() {
	string john3 = "For God so loved the world that he gave his one and only Son, that whoever believes in him shall not perish but have eternal life. For God did not send his Son into the world to condemn the world, but to save the world through him. Whoever believes in him is not condemned, but whoever does not believe stands condemned already because they have not believed in the name of God’s one and only Son. This is the verdict: Light has come into the world, but people loved darkness instead of light because their deeds were evil. Everyone who does evil hates the light, and will not come into the light for fear that their deeds will be exposed. But whoever lives by the truth comes into the light, so that it may be seen plainly that what they have done has been done in the sight of God.";
	string userInput;
	
	cout << "Enter a word or phrase: ";
	getline(cin, userInput);

	while () {
		if (string::npos == john3.find(userInput)) {
			cout << john3.find(userInput) << endl;
			userInput++;
		}

		else {
			cout << "Unfortunately, your word or phrase was not found within John 3:16-21." << endl;
		}
}
	cout << "The word or phrase '" << userInput << "' has been found " << " times in John 3:16-21." << endl;

	system("pause");
	return 0;
}
Topic archived. No new replies allowed.