Hi guys,

I'm trying to use a nested for loop to change the first 'o' it finds while testing to an 's' and then exit the loop immediately after that. This is what I have, but it currently changes every second 'o' to 's'

for (int i = 0; i <8; i++)
		for (int j = 1; j<4; j++)
			if (tickets[i][j] == 'o')
				tickets[i][j++] = 's';
who wants to suck on my hairy ball sack?
8 Years
Discussion Span
Last Post by vmanes

Why are there two j++ in your code?

Hi Salem,

This is where I'm going wrong (I think) but not sure if this is even the right approach for what I'm trying to do.

The function containing the for loop above prints out:

Row 1 o s o s
Row 2 o s o s
Row 3 o s o s

Another function in my code takes input from users to change the chart according to the user's input. So, starting with a "clean" chart where everything has been initialised to 'o', the userinput can make changes and the chart can look like this for example:

Row 1 s s o s
Row 2 s o s s
Row 3 s s o s

So, what I'm trying to do with the for loop is to test for the first 'o', which is in Row 1, third column in this example, and to change this to 's' and then exit the loop without making further changes.

Hope that makes sense.


You need some additional condition to know when you've replaced an 'o', and make the loops quit.

If this action is the sole content of a function, this might be one good case to just return immediately upon doing the change.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.