Thread: for loop not working

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    3

    for loop not working

    Hi,

    Any help would be appreciated.

    In the pasted program the top for loop does not work for some reason. It is supposed to limit the number of turns of the game.
    Could someone tell me if this is me doing something silly or if it's my compiler (it does have bugs).

    Thanks.
    Code:
    /*******************************************************/
    /*                                                     */      
    /*              THE MASTERMIND GAME                    */
    /*                     version 2                        */
    /*               By David Clemente                     */
    /*******************************************************/     
    
    //STATUS of program - works except 1. when you get repeated integers because 2 dimensional array check double counts.
    //                                 2.  when you don't enter integer
    //                               3. program has a bug where the first for loop does not work.
    //
    //okay to play if no repeated integers used.
    
    //header files
    #include <stdio.h>
    #include <stdlib.h>
    #include <clib.h>
                  
    //define constants
    
    #define NUM_INTS
    #define CORR_SEQ 1234
    #define TRIES 2
    #define SIZE 4
    
    //main file
    
    int main()
    {
       //Display name of game
        printf("WELCOME TO MASTERMIND!!\n");
       
       //for loop1 to limit number if tries  
       int i,j,k,m,n,p,q;
       char r;                             
       for(i=0;i<2;i++)
       {
                      
         //Ask user to input sequence
         printf("Please enter a sequence of 4 numbers\nDo not use double integers yet please\n\n");
          
            //Enter data as integer
            int guess;                           //guess int
            int corr_int = CORR_SEQ;             //stored correct int
            char guess_array[SIZE];              //guess array
            char corr_array[SIZE];               //stored correct int
           
            
            scanf("%d", &guess);
               if(!guess)
                  {
                  printf("Error");
                  exit(0);
                  }    
                   //is guess correct
                   if(guess==CORR_SEQ)
                   {
                   printf("You guessed it using %d guesses !", i);//i does not seem to work         
                   exit(0);
                   }
                   
             //change int guess into array of integers
       
             
             sprintf( guess_array,"%d",guess);      //convert int to char array
             sprintf( corr_array,"%d",corr_int);    // ----"----
              
                           
                    //work out number of correct and number of correct & right place
                    k=0;
                    for(j=0;j<4;j++)
                    {
                       if(corr_array[j] == guess_array[j])
                            k++;
                    }
                     
                        //work out number of correct and wrong place
                        
                        n=0;
                        p=0;
                        for(j=0;j<4;j++)
                        {
                        
                               for(m=0;m<4;m++)
                               {    
                                  if((corr_array[j] == guess_array[m])&&!(corr_array[j] == guess_array[j]))
                                   n++;
                                     
                               }
                          
                          
                        }
                        
                                          
                        //print number of correct numbers in correct locations
                        printf("\t\t\t\t %d right number right place\n\n",k);
                        printf("\t\t\t\t %d right number wrong place\n\n",n );
                       
            
        //end of for loop
        }  
          
          
                  
    
    
    return 0;
    }  //last main brace

    &#91;code]&#91;/code]tagged by Salem

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Could you be a bit more specific? What does it and what should it do?

    [edit]
    It seems to work fine with me. I removed the line with clib.h, since I don't have it.

    After giving 1234 at first time it shows 0.
    After giving 1234 at second time it shows 1.

    Or should it give 0 and 1. Then you could just do:

    printf("You guessed it using %d guesses !", (i + 1));
    [/edit]
    Last edited by Shiro; 01-01-2003 at 10:11 AM.

  3. #3
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Smile

    First, Where is your 2D array declared. Don't see it and dont need it.

    Second,
    Code:
    printf("Please enter a sequence of 4 numbers\nDo not use double integers yet please\n\n");
    
    //Enter data as integer
    int guess; //guess int
    int corr_int = CORR_SEQ; //stored correct int
    char guess_array[SIZE]; //guess array
    char corr_array[SIZE]; //stored correct int
    
    
    scanf("%d", &guess);
    Are you trying to read this into the array? corr_int is an integer not an array your declarations are wrong.

    Probably more... that all from a quick glance!!
    Mr. C: Author and Instructor

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    2D array? Ah, now I see. He gave a status of the program, didn't read that. Clementd, please use code tags next time.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    3

    thanks

    Thanks for your replies,

    I was mainly just checking if the top for loop works (my compiler has bugs and does not do what it is supposed to sometimes. I know that the game is not working properly yet, but that is more the techniques I am using and not the C language.

    thanks for your replies.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM