Thread: nested for loop

  1. #1
    Registered User
    Join Date
    Feb 2010

    nested for loop

    my code is supposed to find 1 match and break but it keeps going and finds them all. it searches my 3 words in my string array and makes sure that they are not already replaced.

    for (int i=0; i < m; i++){
    	for (int x = 0; x <= 2; x++){
    		if (curWord[0][x][i] != letterguess){
    			if (xword[0][x][i] == letterguess){
    			curWord[0][x][i] = letterguess;
    			cout << xword[0][x] << endl;
    			xcount = 0;
    Last edited by altf4thc; 02-18-2010 at 12:54 AM. Reason: forgot code tags

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    The break only breaks out of the inner for loop. If you want to jump out of the second one as well you need to do something else.
    goto is one option.
    But what I prefer is to put both loops inside of another function and return from it.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Feb 2010
    if i add another break then it breaks out of my do loop that runs the whole code and ends the game. should i put it in its own class?

  4. #4
    Registered User
    Join Date
    Mar 2007
    Do not use goto. It is horrible to maintain if used too much, and is not good practice.

    You could do something simple. Add a bool flag that is initially false, then in the inner loop set it to true, and at the end of the outter loop check if it's true. If it's true, break the outter loop as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested array vs. tree
    By KONI in forum Tech Board
    Replies: 1
    Last Post: 06-07-2007, 04:43 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM
  4. Displaying Data from a Nested Structure
    By shazg2000 in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2005, 10:16 AM
  5. Nested Classes
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2004, 11:57 AM

Tags for this Thread