Thread: Simple reset counter on for loop

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123

    Cool Simple reset counter on for loop

    Please, if you have the time and run and compile this program, I am looking for further instruction from someone on game_loop variable to countdown from 3 to 0 (reset it). I've tried with a while loop before and after the for () loop with no success. After three successful tries followed by a failure, and an "y" to continue with another game, just says "you have 1 try left", i'm looking for a way to reset the game_loop variable to reset it to start at 3 again, but it's broke. I've tried with this:


    Code:
    if (game_loop <= 1)
    {
        game_loop += 3;
    }

    I don't even know if the above is valid C but i also tried messing around with a while loop similar to what I did in the main body of the program to no available. Please direct me further.

    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <ctype.h>
    
    
    void delay (int seconds)     // delay function i got this source code from google
    {
        unsigned long int count=333333333, i, j;
    
    
        for(i = 0;i<seconds;i++)
            for(j = 0; j < count; j++);
    }
    
    
    int main (void)
    {
        char c = ' ';  // fill console with blank space to clear screen
        int successful_tries = 0;
        //char newgame = 0;
        int num = 0;
        int input = 0;
        int limit = 9;
        char get_input;
        int limit_2 = 1;
        int game_loop = 3;
        
        printf("This is a game to test your memory, with a random number of 1 digit that will be displayed a max of 3 successive times.\n");
    
    
        do
        {
            clock_t t;
            t = clock();
    
    
            for (; game_loop > 0; --game_loop) // count down how many tries left until program ends
            {
    
    
                // code to compute random digits
                 srand(time(NULL));
                num = limit_2 + rand() % limit;
    
    
                printf("\n%d\n", num);
                delay(1);     // create 1 second program delay before clear screen with 'X's
    
    
                // clear screen with x's
                for (int ctr_1 = 1; ctr_1 < 7500; ctr_1++)
                {
                    printf("%c", c);
                }
        
                printf("\n\n");
    
    
                printf("\nYou have %d tr%s left.\n", game_loop, game_loop == 1 ? "y" : "ies");
                printf("What sequence was displayed?: ");
                scanf("%d", &input);
    
    
                if (input == num) // if correct match
                {
                    printf("Correct Answer!\n");
    
    
                    int i = 0;
    
    
                    while (i < 1)  // per one match (one correct match)
                    {
                        successful_tries += 1; // Successful tries for end of program
                        ++i;
                    }
    
    
                    if (successful_tries == 3) // sequence was inserted 3 times in a row
                    {
                        limit += 191; // make sequence 3 digits
                        limit_2 += 49;  // limit_2 = limit_2 + 49;
                    }
    
    
                    if (successful_tries >= 3)
                    {
                        game_loop += 1;
                    }
                }
        
                if (input != num)
                {
                    printf("You entered an incorrect match!\n");
                    break;
                }
            }
    
    
        // The program will then calculate a score based on the number of successful tries and the time taken and invite the player to play again.
        t = clock() - t;
    
    
        double program_execution = ((double)t)/CLOCKS_PER_SEC; // in seconds
    
    
        printf("You had %d successful tr%s. Program execution took %f seconds.\n", successful_tries, successful_tries == 1 ? "y" : "ies", program_execution);
    
    
        printf("Do you want to play another game? y for yes n for no\n");
    
    
        scanf(" %c", &get_input);
    
    
        if (tolower(get_input) == 'n') // if 'n' is inserted
        {
            printf("You entered %c\nProgram will end...\n", get_input);
            return 0;
        }
        
        } while (tolower(get_input) == 'y');
        
        return 0;
    }

  2. #2
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Code:
    // etc... code ;
    
    do {
      clock_t t; 
      t = clock();
    
    
      game_loop = 3; 
      successful_tries = 0;
    
    
      for (; game_loop > 0; --game_loop) {
        // etc... code ;
      }
    
    
      // etc... code ;
    
    
    }  while (tolower(get_input) == 'y');
    
    // etc... code ;
    "without goto we would be wtf'd"

  3. #3
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123
    Thanks Structure, took a few days for someone to post. Looks like I was just missing that one line:

    Code:
    successful_tries = 0;

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Please, put srand(time(NULL)) outside the loop. Inside you risk not getting "random" values with rand(). The seed must be provided only once in your program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why does the value in for loop reset?
    By mangekyou in forum C Programming
    Replies: 5
    Last Post: 04-02-2019, 04:03 PM
  2. Why counter won't reset in my C program?
    By sunil9211 in forum C Programming
    Replies: 1
    Last Post: 05-16-2018, 09:24 AM
  3. need help about counter in for loop
    By hasib ahmed in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2011, 01:40 PM
  4. Trying to loop my program or reset it Dev c++
    By GameGenie in forum C++ Programming
    Replies: 8
    Last Post: 07-21-2005, 12:39 PM
  5. How to reset a pointer in a loop?
    By stillwell in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 02:06 PM

Tags for this Thread