Thread: Simple do { while () loop entire program

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

    Exclamation Simple do { while () loop entire program

    Program code:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <ctype.h>
    
    
    
    
    int main(void)
    {
        int chosen = 0; // the lucky number
        int guess = 0;  // Stores a guess
        int count = 3; // The meximum number of trie
        int limit = 20; // Upper limit for pseudo-random values
        char input;
    
    
    
    
        do
        {
            srand(time(NULL)); // Use clock vlaue as starting seed
            chosen = 1 + rand() % limit;     // Random int 1 to limit
    
    
    
    
            printf("\nThis is a guessing game.");
            printf("\nI have chosen a number between 1 and 20"
                " which you must guess.\n");
    
    
    
    
    
    
    
    
            for( ; count > 0 ; --count)
            {
                printf("\nYou have %d tr%s left.", count,count == 1 ? "y" : "ies");
                printf("\nEnter a guess: ");    // Prompt for a guess
                scanf("%d", &guess);    // Read in a guess
    
    
    
    
                // Check for a correct guess
                if(guess == chosen)
                {
                    printf("\nCongratulations. You guessed it!\n");
                    return 0;        // End the program
                }
                else if(guess < 1 || guess > 20) // Check for an invalid guess
                {
                    printf("I said the number is between 1 and 20.\n ");
                }
                else
                {
                    printf("Sorry, %d is wrong. My number is %s than that.\n", guess, chosen > guess ? "greater" : "less");
                }
            }
    
    
    
    
        printf("\nYou have had three tries and failed. The number was %ld\n", chosen);
    
    
    
    
        printf("Do you want to play another game? (Press Y to replay)\n");
        scanf(" %c", &input);
    
    
    
    
        } while(toupper(input) == 'Y'); // if 'Y' is pressed loop program
    
    
    
    
        return 0;
    }

    Please, only the final two printf() statements (functions) are executing and scanf() following a failed 3 error response, am I missing a bracket somewhere which would make the do while loop loop the entire program successfully on a "y" or "Y" response for the scanf() function:

    Code:
    scanf(" %c", &input);

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,547
    Perhaps you need to "reset" count?

    Also this:
    Code:
     printf("\nYou have had three tries and failed. The number was %ld\n", chosen);
    should be producing a warning. If your compiler is not warning you about an issue with this line you need to either increase your compiler warning levels or get a better compiler (or maybe both).

  3. #3
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    112
    Quote Originally Posted by jimblumberg View Post
    Perhaps you need to "reset" count?

    Also this:
    Code:
     printf("\nYou have had three tries and failed. The number was %ld\n", chosen);
    should be producing a warning. If your compiler is not warning you about an issue with this line you need to either increase your compiler warning levels or get a better compiler (or maybe both).
    thanks Jim. Yes i forgot to run gcc with -Wall now I see "chosen" is an int not a %ld also count is counting down a max of 3 loops
    Last edited by _jamie; 02-23-2020 at 05:16 PM. Reason: fixed grammar error

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,547
    also count is counting down a max of 3 loops
    Yes, and as presently coded it will do this exactly once.

  5. #5
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    112

    Cool

    Quote Originally Posted by jimblumberg View Post
    Yes, and as presently coded it will do this exactly once.

    this fixed it
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <ctype.h>
    
    
    int main(void)
    {
        int chosen = 0; // the lucky number
        int guess = 0;  // Stores a guess
        int count = 3; // The meximum number of trie
        int limit = 20; // Upper limit for pseudo-random values
        char input;
    
    
        do
        {
            srand(time(NULL)); // Use clock vlaue as starting seed
            chosen = 1 + rand() % limit;     // Random int 1 to limit
    
    
            printf("\nThis is a guessing game.");
            printf("\nI have chosen a number between 1 and 20"
                " which you must guess.\n");
    
    
    
    
            for( ; count > 0 ; --count)
            {
                printf("\nYou have %d tr%s left.", count,count == 1 ? "y" : "ies");
                printf("\nEnter a guess: ");    // Prompt for a guess
                scanf("%d", &guess);    // Read in a guess
    
    
                // Check for a correct guess
                if(guess == chosen)
                {
                    printf("\nCongratulations. You guessed it!\n");
                    return 0;        // End the program
                }
                else if(guess < 1 || guess > 20) // Check for an invalid guess
                {
                    printf("I said the number is between 1 and 20.\n ");
                }
                else
                {
                    printf("Sorry, %d is wrong. My number is %s than that.\n", guess, chosen > guess ? "greater" : "less");
                }
            }
    
    
        printf("\nYou have had three tries and failed. The number was %d\n", chosen);
    
    
        printf("Do you want to play another game? (Press Y to replay)\n");
        scanf(" %c", &input);
    
    
        count = 3;
    
    
        } while(toupper(input) == 'Y'); // if 'Y' is pressed loop program
    
    
        return 0;
    }
    this line fixed it, after the scanf() function:

    Code:
    count = 3;
    Thanks Jim...that was a quick & easy fix.
    Last edited by _jamie; 02-23-2020 at 09:01 PM. Reason: typo

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,547
    Actually this would probably be better:

    Code:
    for(count = 3 ; count > 0 ; --count)
    // Or even better, just define the variable in the loop it's self.
    for(for int count = 3 ; count > 0 ; --count)
    In modern C you no longer need to create your variables at the beginning of some scope and many people prefer to create the variables closer to first use. Also you can now define and initialize the for loop variables within the loop construct.

    Also that srand() call should happen before the loop, it really should only be called once, and never inside some loop.

  7. #7
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    112

    Cool

    Quote Originally Posted by jimblumberg View Post
    Actually this would probably be better:

    Code:
    for(count = 3 ; count > 0 ; --count)
    // Or even better, just define the variable in the loop it's self.
    for(for int count = 3 ; count > 0 ; --count)
    In modern C you no longer need to create your variables at the beginning of some scope and many people prefer to create the variables closer to first use. Also you can now define and initialize the for loop variables within the loop construct.

    Also that srand() call should happen before the loop, it really should only be called once, and never inside some loop.

    alright thanks, i worked through that problem, i'm working on a new problem tonight, will paste soon. Also, i missed your typo "for int count"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another simple program using for loop
    By YaZao in forum C Programming
    Replies: 4
    Last Post: 09-15-2015, 06:28 AM
  2. Simple Loop Program
    By sgk1498 in forum C Programming
    Replies: 5
    Last Post: 02-27-2014, 08:09 PM
  3. loop entire C program ll Please Help
    By Johny2000 in forum C Programming
    Replies: 3
    Last Post: 06-08-2011, 04:00 AM
  4. Loop Entire Program?
    By seanminator in forum C Programming
    Replies: 25
    Last Post: 07-22-2009, 01:23 PM
  5. Replies: 5
    Last Post: 02-01-2003, 10:58 AM

Tags for this Thread