Help creating a while loop

Guys,

I wrote a program that search for substrings and prints the line starting with the substring to the end of the line. I need to add a loop that will continue to check for a occurence of the string until the complete line has been checked.

I wrote the program and also I was able to write a line that will check for the next occurence after the first but I can't figure out how to create a loop that will keep going until the entire line of text has been checked.

here is the code I have:

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 <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>


using namespace std;

int main()
{

	char firststring[80];
	char search [80];

	char *searchPtr;
	char *searchPtr2;

	cout << "Enter the first sentence: " << endl;
	cin.getline(firststring, 80, '\n');

	cout << "Enter the string you are looking for: " << endl;
	cin.getline(search, 80, '\n');

	searchPtr = strstr(firststring, search);

	cout << "String1 = " << firststring << "\nstring2 = " << search
		 << "\n\nThe remainder of string1 beginning with the\n"
		 << "first occurrence of string2 is: \n"
		 << searchPtr << endl;

//I need the while loop here

		cout << "There is another occurrence of the search string that starts here: \n"
			 << strstr(searchPtr + 1, search) << endl;

	return 0;

}


I using a couple of arrays so I tried to create a while loop using the size of the array but my array is 80 and the while loop keeps printing out the same last line 80 times.

Any advice?
Last edited on
A for loop would be easier to use to traverse an array.
Topic archived. No new replies allowed.