| | |
2D array and nested for loop
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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'
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'
C++ Syntax (Toggle Plain Text)
for (int i = 0; i <8; i++) { for (int j = 1; j<4; j++) if (tickets[i][j] == 'o') tickets[i][j++] = 's'; } }
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
etc
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.
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
etc
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.
If this action is the sole content of a function, this might be one good case to just return immediately upon doing the change.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Validate A String That Has 2 Letters and Then 6 Numbers
- Next Thread: Converting From Pointer/Address to String for Validation
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy directshow dll download dynamic dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






