CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2011
    Posts
    4

    A newbie question

    Hello everyone, i am new to c++ programming and I have a problem in a simple function I made.
    Code:
    void resetTable(void){
            //Limpa todas as variaveis e imprime uma table limpa
            for(x=0;x<=8;x+=1){
            A[x]=0;}
            for(x=0;x<=8;x+=1){
            B[x]=0;}
            for(x=0;x<=8;x+=1){
            C[x]=0;}
            for(x=0;x<=8;x+=1){
            D[x]=0;}
            for(x=0;x<=8;x+=1){
            E[x]=0;}
            for(x=0;x<=8;x+=1){
            F[x]=0;}
            for(x=0;x<=8;x+=1){
            G[x]=0;}
            for(x=0;x<=8;x+=1){
            H[x]=0;}
            for(x=0;x<=8;x+=1){
            I[x]=0;}
            printTable();
            }
    In this code, the last loop with the I[] array does not stop looping, it goes on and on. I have tried changing my variable name, same problem, i have changed the place of the loop and its that one loop that doesnt work.
    I used alot of printfs to discover what the problem was and it seems that in the eight loop the I[8] is set to 8 and x resets to 1, creating a neverending loop.

  2. #2
    Join Date
    Mar 2011
    Posts
    4

    Re: A newbie question

    Its the original, I can place here the full code if you so desire. Here's my variable declaration:
    Code:
      int A[8];
      int B[8];
      int C[8];
      int D[8];
      int E[8];
      int F[8];
      int G[8];
      int H[8];
      int I[8];
      int x;
      int y;
      int z;
    x y and z are placeholder variables, they still have no use

  3. #3
    Join Date
    Mar 2011
    Posts
    4

    Re: A newbie question

    Sorry for double post, here's the full code:
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    void resetTable(void);
    void printTable(void);
                                                       
    
      int A[8];
      int B[8];
      int C[8];
      int D[8];
      int E[8];
      int F[8];
      int G[8];
      int H[8];
      int I[8];
      int x;
      int y;
      int z;
    int main(int argc, char *argv[])
    {
    
      resetTable();
      
      
    
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
       void resetTable(void){
            //Limpa todas as variaveis e imprime uma table limpa
            for(x=0;x<=8;x+=1){
            A[x]=0;}
            for(x=0;x<=8;x+=1){
            B[x]=0;}
            for(x=0;x<=8;x+=1){
            C[x]=0;}
            for(x=0;x<=8;x+=1){
            D[x]=0;}
            for(x=0;x<=8;x+=1){
            E[x]=0;}
            for(x=0;x<=8;x+=1){
            F[x]=0;}
            for(x=0;x<=8;x+=1){
            G[x]=0;}
            for(x=0;x<=8;x+=1){
            H[x]=0;}
            for(x=0;x<=8;x+=1){
            I[x]=0;}
            printTable();
            }
       void printTable(void){
            //Limpar consola e imprimir a linha dos numeros
            system("cls");
            printf(" 1 2 3 4 5 6 7 8 9\n");
            //Imprimir a linha do A
            printf("A");
            for(x=0;x<=8;x+=1){                           
                               if(A[x]==0||A[x]==1)
                               printf("  ");
                               if(A[x]==2)
                               printf("O ");
                               if(A[x]==3)
                               printf("X ");
                               if(x=8)
                               printf("\n");                                                      
                              }
            //Imprimir a linha do B
                    printf("B");
            for(x=0;x<=8;x+=1){                           
                               if(B[x]==0||B[x]==1)
                               printf("  ");
                               if(B[x]==2)
                               printf("O ");
                               if(B[x]==3)
                               printf("X ");
                               if(x=8)
                               printf("\n");                                                      
                              }
            //Imprimir a linha do C
                    printf("C");
            for(x=0;x<=8;x+=1){                           
                               if(C[x]==0||C[x]==1)
                               printf("  ");
                               if(C[x]==2)
                               printf("O ");
                               if(C[x]==3)
                               printf("X ");
                               if(x=8)
                               printf("\n");                                                      
                              }
            //Imprimir a linha do D
                    printf("D");
            for(x=0;x<=8;x+=1){                           
                               if(D[x]==0||D[x]==1)
                               printf("  ");
                               if(D[x]==2)
                               printf("O ");
                               if(D[x]==3)
                               printf("X ");
                               if(x=8)
                               printf("\n");                                                      
                              }
            //Imprimir a linha do E
                    printf("E");
            for(x=0;x<=8;x+=1){                           
                               if(E[x]==0||E[x]==1)
                               printf("  ");
                               if(E[x]==2)
                               printf("O ");
                               if(E[x]==3)
                               printf("X ");
                               if(x=8)
                               printf("\n");                                                      
                              }
            //Imprimir a linha do F
                    printf("F");
            for(x=0;x<=8;x+=1){                           
                               if(F[x]==0||F[x]==1)
                               printf("  ");
                               if(F[x]==2)
                               printf("O ");
                               if(F[x]==3)
                               printf("X ");
                               if(x=8)
                               printf("\n");                                                      
                              }
            //Imprimir a linha do G
                    printf("G");
            for(x=0;x<=8;x+=1){                           
                               if(G[x]==0||G[x]==1)
                               printf("  ");
                               if(G[x]==2)
                               printf("O ");
                               if(G[x]==3)
                               printf("X ");
                               if(x=8)
                               printf("\n");                                                      
                              }
            //Imprimir a linha do H
                    printf("H");
            for(x=0;x<=8;x+=1){                           
                               if(H[x]==0||H[x]==1)
                               printf("  ");
                               if(H[x]==2)
                               printf("O ");
                               if(H[x]==3)
                               printf("X ");
                               if(x=8)
                               printf("\n");                                                      
                               }
            //Imprimir a linha do I
                    printf("I");
            for(x=0;x<=8;x+=1){                           
                               if(I[x]==0||I[x]==1)
                               printf("  ");
                               if(I[x]==2)
                               printf("O ");
                               if(I[x]==3)
                               printf("X ");
                               if(x=8)
                               printf("\n");                                                      
                               }
                               }

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: A newbie question

    Worked fine for me.

    FWIW, that's a horrible indentation style you have. Don't be afraid of the space bar

    Compare
    Code:
            for(x=0;x<=8;x+=1){                           
                               if(A[x]==0||A[x]==1)
                               printf("  ");
                               if(A[x]==2)
                               printf("O ");
                               if(A[x]==3)
                               printf("X ");
                               if(x=8)
                               printf("\n");                                                      
                              }
    to
    Code:
    for(x = 0; x <= 8; x += 1)
    {                           
       if(A[x] == 0 || A[x] == 1)
              printf("  ");
       if(A[x] == 2)
              printf("O ");
       if(A[x] == 3)
              printf("X ");
       if(x = 8)
              printf("\n");                                                      
    }
    Last edited by GCDEF; March 24th, 2011 at 07:42 AM.

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: A newbie question

    You have declared the variables :

    Code:
    int A[8];
    
    // etc.
    valid indices are : 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7

    8 is not a valid index.

    your for loops are all :

    Code:
    for (x=0; x<=8; x+=1) 
    {
    Usually, the loop would be written:

    Code:
    for (x=0; x<8; ++x)
    {

  6. #6
    Join Date
    Mar 2011
    Posts
    4

    Re: A newbie question

    Thank you for all your feedback, i will try what you said when i am home. Thank you~!

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: A newbie question

    Quote Originally Posted by Luthion View Post
    Hello everyone, i am new to c++ programming and I have a problem in a simple function I made.
    Code:
    void resetTable(void){
            //Limpa todas as variaveis e imprime uma table limpa
            for(x=0;x<=8;x+=1){
            A[x]=0;}
            for(x=0;x<=8;x+=1){
            B[x]=0;}
            for(x=0;x<=8;x+=1){
            C[x]=0;}
            for(x=0;x<=8;x+=1){
            D[x]=0;}
            for(x=0;x<=8;x+=1){
            E[x]=0;}
            for(x=0;x<=8;x+=1){
            F[x]=0;}
            for(x=0;x<=8;x+=1){
            G[x]=0;}
            for(x=0;x<=8;x+=1){
            H[x]=0;}
            for(x=0;x<=8;x+=1){
            I[x]=0;}
            printTable();
            }
    Once you fix this code, a sign of a good programmer is one that detects repeated patterns, and then attempts to remove the repeated code.

    For example, if arrays A, B, C, etc. are the same type, then this could all be written with a little more scalability:
    Code:
    void resetTable(void)
    {
        int* ptrArray[] = {A, B, C, D, E, F, G, H, I};  // assume that all arrays are  int arrays
        for (int i = 0; i < sizeof(ptrArray) / sizeof(ptrArray[0]); ++i)
        {
             for ( int j = 0; j < 8; ++j )
                  ptrArray[i][j] = 0;
        }
    }
    If all the arrays are of another type, then replace "int*" with whatever type those arrays are (double*, float*, whatever*, etc.)

    This may be a little more advanced than what you've seen, but this is one way to reduce that code to something more scalable. If another array needs to be added, then you just add it to the ptrArray definition above, and none of the other code needs to change.

    Also, since all the arrays are initialized the same, then all of them will be initialized correctly since they are using the same code (you aren't writing the code over and over again, and possibly making a mistake).

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured