Problem looping through an array

This is a discussion on Problem looping through an array within the C++ Programming forums, part of the General Programming Boards category; I'm trying to loop through an array, but get the errror "error C2109: subscript requires array or pointer type". I'm ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    80

    Problem looping through an array

    I'm trying to loop through an array, but get the errror "error C2109: subscript requires array or pointer type". I'm not sure why this doesn't work. The error happens when I try to
    set CFtemp2 = bnext[i][md]: sprintf(CFtemp2,"%i",bnext[i][md]);:

    Code:
    			for (i = 0 ; i <= MAXATM + 1 ; i++) {
    			
    				
    				sprintf(CFtemp,"%i",bnext[i]);
    
    				for (md = 0 ; md <= MAXDEG + 1 ; md++) {
    				
    					int iRow = pQuery->AddRow() ;
    
    					sprintf(CFtemp2,"%i",bnext[i][md]);				
    
    					pQuery->SetData( iRow, iCol1, CFtemp2 ) ;
    					pQuery->SetData( iRow, iCol2, CFtemp ) ;	
    				
    
    					CFtemp2[0] = '\0';
    				
    				}
    
    				CFtemp[0] = '\0';
    
    		
    			}
    Last edited by jamez05; 10-31-2005 at 11:37 AM.

  2. #2
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    what is bnext?
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    80
    bnext is an array declared in an include file and populated prior to the function I'm trying to read it in:

    Code:
    #define MAXATM 60
    
    #define MAXDEG 6
    
    int    bnext[MAXDEG+1][MAXATM+1];
    
    char CFtemp[1000];//used for temparary storage variable when converting ints to chars
    char CFtemp2[1000];//Secondary used for temparary storage variable when converting ints to chars for arrays

  4. #4
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    hmm, it seems to me that this line should be giving you the error:

    sprintf(CFtemp,"%i",bnext[i]);

    If bnext was defined as having 2 dimensions then this would of course give you an error. A fix would be to add the second substring like:

    sprintf(CFtemp,"%i",bnext[i][i]);

    Other than that, I don't see a problem with your syntax.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    80
    I tried the fix, but for some reason, I'm still getting the error.

    Here's the updated code. I'll keep digging into it and post a answer if I
    find one:

    Code:
    			CFtemp2[0] = '\0';
    			
    			for (i = 0 ; i <= MAXATM + 1 ; i++) {
    			
    				
    				
    
    
    
    				for (md = 0 ; md <= MAXDEG + 1 ; md++) {
    				
    					int iRow = pQuery->AddRow() ;
    
    					sprintf(CFtemp2,"%i",bnext[i][md]);				
    
    					pQuery->SetData( iRow, iCol1, CFtemp2 ) ;
    					;	
    				
    
    					CFtemp2[0] = '\0';
    				
    				}
    
    				CFtemp[0] = '\0';
    
    		
    			}

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    80
    Figured it out, a very dumb error on my part. I named the query the same as the
    variable I was trying to access - duh! sorry about that.

    James

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  2. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 12:35 PM
  3. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 01:04 PM
  4. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  5. Problem Putting INTs Into a CHAR Array
    By cram in forum C++ Programming
    Replies: 13
    Last Post: 10-13-2004, 08:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21