IF statements inside FOR loop

I'm trying to write a program that reads in a users input and iterates through it and finds a certain word. If that word is found, do something. Quite simple actually.

1
2
3
4
5
  FOR (i; i <string; i++)
     IF string contains "example"
        //do something
     ELSE 
        //do something 


I was wondering how in the //do something, I could put another function that I've written BUT that would just cause my program to run that function i amount of times instead of just once. How do I get around this? Cant seem to figure it out.

Thanks!
You use break.

for example:
1
2
for(i=0; i<value; ++i)
   if(my_array[i]==10) break; //goes out of loop 
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string>

void my_function() {
	//...
}

int main() {

	std::string string_input = "this is an example.";
	std::string string_search = "example";

	std::string::size_type pos = string_input.find(string_search);
	if (pos != std::string::npos) {
		
		const int number_of_iterations = 10;
		for (int i = 0; i < number_of_iterations; ++i) {
			my_function();
		}

	}

	return 0;
}
Cool. That seemed to work. Another issue though. I currently have:

1
2
3
4
5
6
7
8
9
10
11

 FOR (i; i <string; i++)
     IF string contains "example"
        FOR (i; i <string; i++)
             IF string contains "2nd example"
                  //do something
             ELSE 
                  //do something
      ELSE
           //do something


Even if the example or 2nd example is found, the "ELSE" is always executed. Why?
Because you wrote the same for loop and your string value is the same as long as for loop will be working. You are not writting the code well I think.

Another thing you have not wrote well is that , an int can not be compared with a string type .
 
for(int i =0; i < string; i++)


Secondly, you have to write the example code if you want to get an answer.
Last edited on
Topic archived. No new replies allowed.