Thread: Nested for loop

  1. #1
    C++ beginner
    Join Date
    Sep 2005
    Posts
    4

    Nested for loop

    New to c++ so might word this incorrect.
    I have a nested for loop that is comparing an array to see if there are any that match.
    I am only outputing match, i, & j for testing purposes only.

    Code:
     for(int i = 0; i <= 3; ++i)
           {
                   for(int j = i + 1; j <= 4; ++j)
                   {
                           if(hand[i][0] == hand[j][0])
                             { cout << "Match" << " " << i << " " << j;
                              ++ matches;
                             }
                   }
           }
    Sample ouput
    out of the 3 listed
    1. pair "1"
    2. nothing "0"
    3. 3 of a kind "3"
    What I am wanting is a count for each array. If I cout matches I get a large number.

    Thanks,

    GJ
    Code:
    Match 0 3   9H,KS,5C,9C,7C
    
    
       KC,3S,7D,5S,6D
    
    
    
    Match 0 1Match 0 3Match 1 3   TC,TH,QH,TH,8C

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    there is a difference between
    Code:
    ++i
    which increments before the operation
    and
    [code]i++[code]
    which increments after the operation.
    You might want to rethink which one you want to use.
    Don't quote me on that... ...seriously

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    New to c++ so might word this incorrect.
    How about stating in english:

    1) what your input is

    2) what you want your program to do

    3) what output you want

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> If I cout matches I get a large number.
    Did you remember to initialize matches to 0?

    >> there is a difference
    There would be no difference between ++i and i++ (or ++j and j++ or ++matches and matches++) in that code.
    Last edited by Daved; 10-25-2005 at 10:10 PM.

  5. #5
    C++ beginner
    Join Date
    Sep 2005
    Posts
    4
    That was my issue, I was initializing mathches back to '0' in the wrong place. Once I moved it my program runs.

    7stud I will be more clear next time on my question.

    Thanks for the help

    GJ

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 switch issue
    By fsu_altek in forum C Programming
    Replies: 3
    Last Post: 02-15-2006, 10:29 PM
  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