Thread: C guessing game (hmwrk)-It works as required & submitted: What would make it better?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    2

    C guessing game (hmwrk)-It works as required & submitted: What would make it better?

    First. Thank you for being here and allowing me to make this post. As the title states this was a homework assignment. I've turned it in and it compiles and works. But, I'm wondering what methods would have been better for doing this assignment. I've no programming experience and I'm just trying to figure out the best way to do this sort of thing.

    Thanks again!!

    Code:
    //     Purpose: Program plays a guessing game. 
    //        Program randomly generates a number in the domain of 1-100
    //        Program picks a number between 1 and 100.  User guesses the number.
    //        The program provides feedback for each guess. 
    //        When you guess correctly user is given feedback about the number of guesses. 
    //    
    //    Inputs:  
    //        User enters a guess each round.
    //
    //    Outputs--
    //        Ask for initial guess between 1-100
    //        Provide feedback for each guess: ice cold, cold, cool, warm, hot, boiling hot
    //        When correct guess is made: Provide number of turns to reach correct.
    
    //These go before main()
    
    #include <stdio.h>                             //Preprocessor Directive
    #include <stdlib.h>                            //Preprocessor Directive
    #include <time.h>                            //Preprocessor Directive
                    
    
    //main argument
    
    int main() {                                //Function-main argument
    
                                                //Code for random number generator-Instructor's code.//
                                                
        srand(time(NULL));                       // Seed the random number generator
        
    //variables
    
        int goal = rand() % 100 + 1;             // goal will be 1-100, inclusive
    
        int i = 1,                                // Index for guess counter
            turn,                                // Guess counter for final answer.
            guess,                                // User's guess.
            distance;                            // Distance of guess from goal on the numberline.
            
        printf("I have a number, 1-100.\n");
            
        
        do    {
            
            //printf("Super Secret # %d / Guess # %d\n",goal, turn);    //used for testing--if/ else if
            
            printf("What's your guess?  ");
                
                scanf("%d", &guess);                                    //User inputs integer    
        
        distance = abs(goal - guess);                                    //distance gets value.
        turn = i++;                                                        //turn counter
            
            printf("%d",guess);
    
            if (guess < 1 || guess > 100)    {                
                    printf(" is not 1-100.\n", guess);
                  } 
                  
                  else if (distance > 50)        {
                      printf(" is ice cold.\n");
                      }
                  
                  else if (distance <= 50 && distance > 30)    { 
                      printf(" is cold.\n");
                      }
                  
                  else if (distance <= 30 && distance > 20)    {
                      printf(" is cool.\n");    
                      } 
                  
                  else if (distance <= 20 && distance > 10)    {
                    printf(" is warm.\n");
                      } 
                  
                  else if (distance <= 10 && distance > 2) {
                       printf(" is hot.\n");
                      } 
                  
                else if (distance <= 2 && distance >= 1) {
                    printf(" is boiling hot.\n");
                    }
                    
                    continue;                                            //continue sends us back to the begining of do
          }    
          
        while (guess != goal);                                            //while keeps us in loop until input is same as goal
        
        printf(" is right in %d guesses.\n",turn);
                
            return 0;                                        //e.m.d.w.
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The first step is consistent indentation.
    Code:
    //     Purpose: Program plays a guessing game.
    //        Program randomly generates a number in the domain of 1-100
    //        Program picks a number between 1 and 100.  User guesses the number.
    //        The program provides feedback for each guess.
    //        When you guess correctly user is given feedback about the number of guesses.
    //
    //    Inputs:
    //        User enters a guess each round.
    //
    //    Outputs--
    //        Ask for initial guess between 1-100
    //        Provide feedback for each guess: ice cold, cold, cool, warm, hot, boiling hot
    //        When correct guess is made: Provide number of turns to reach correct.
    
    //These go before main()
    
    #include <stdio.h>              //Preprocessor Directive
    #include <stdlib.h>             //Preprocessor Directive
    #include <time.h>               //Preprocessor Directive
    
    
    //main argument
    int main()
    {                               //Function-main argument
      //Code for random number generator-Instructor's code.//
      srand(time(NULL));            // Seed the random number generator
    
      //variables
      int goal = rand() % 100 + 1;  // goal will be 1-100, inclusive
    
      int i = 1,                    // Index for guess counter
          turn,                     // Guess counter for final answer.
          guess,                    // User's guess.
          distance;                 // Distance of guess from goal on the numberline.
    
      printf("I have a number, 1-100.\n");
      do {
        //printf("Super Secret # %d / Guess # %d\n",goal, turn);    //used for testing--if/ else if
        printf("What's your guess?  ");
        scanf("%d", &guess);        //User inputs integer
    
        distance = abs(goal - guess); //distance gets value.
        turn = i++;                 //turn counter
    
        printf("%d", guess);
    
        if (guess < 1 || guess > 100) {
          printf(" is not 1-100.\n", guess);
        }
        else if (distance > 50) {
          printf(" is ice cold.\n");
        }
        else if (distance <= 50 && distance > 30) {
          printf(" is cold.\n");
        }
        else if (distance <= 30 && distance > 20) {
          printf(" is cool.\n");
        }
        else if (distance <= 20 && distance > 10) {
          printf(" is warm.\n");
        }
        else if (distance <= 10 && distance > 2) {
          printf(" is hot.\n");
        }
        else if (distance <= 2 && distance >= 1) {
          printf(" is boiling hot.\n");
        }
        continue;                   //continue sends us back to the begining of do
      }
      while (guess != goal);        //while keeps us in loop until input is same as goal
    
      printf(" is right in %d guesses.\n", turn);
    
      return 0;                     //e.m.d.w.
    }
    > turn = i++; //turn counter
    Or just turn++ and delete an extra variable which has no use.

    > continue; //continue sends us back to the begining of do
    This too is unnecessary.
    All it does is jump to the loop condition, but that is exactly where it was going to end up anyway.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    2
    Thanks for your feedback. What convention do you use for indention? I know there should be some indentation; but I'm not sure how to go abouyt it. Is it based on preference, convention or just something you pick up reading other people's code?

    Thanks for the tips about the variable and continue. They were legacies of my previous versions of the project although until you pointed it out I did think they were necessary.

    Thanks again!!!

    jjm

  4. #4
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by AxeSwinger View Post
    Thanks for your feedback. What convention do you use for indention? I know there should be some indentation; but I'm not sure how to go abouyt it. Is it based on preference, convention or just something you pick up reading other people's code?

    Thanks for the tips about the variable and continue. They were legacies of my previous versions of the project although until you pointed it out I did think they were necessary.

    Thanks again!!!

    jjm
    Check out this wikipedia page about indentation.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The first thing to do is make sure your text editor / IDE is set to "spaces for tabs". It's the only way to ensure a consistent look across the widest possible range of media. Mixed tabs and spaces are a disaster when posted.

    The second point is simply to be consistent with your chosen style.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Guessing game: how to quit the game?
    By hzr in forum C Programming
    Replies: 5
    Last Post: 12-18-2008, 10:53 AM
  2. guessing game
    By Sal79 in forum C Programming
    Replies: 14
    Last Post: 05-09-2007, 02:22 PM
  3. guessing game
    By Inferno in forum C++ Programming
    Replies: 2
    Last Post: 09-22-2004, 05:37 PM
  4. A guessing game
    By Lyanette in forum C++ Programming
    Replies: 5
    Last Post: 04-03-2003, 10:02 AM
  5. guessing game
    By wayko in forum C++ Programming
    Replies: 11
    Last Post: 09-19-2001, 06:10 PM

Tags for this Thread