Program help

This is a discussion on Program help within the C Programming forums, part of the General Programming Boards category; I am new to C and having some issues with a program. The program should note guesses outside the legal ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    3

    Program help

    I am new to C and having some issues with a program. The program should note guesses outside the legal range shall (1-100) be noted to the user and counted as guesses.
    I am also having issues with the continuation of the program until a key word is entered.

    I am only looking for suggestions

    Thank you for any help.



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int main()
    
    
     {
        int num;
        int guess =0;
    	int counter =0;
    	int total;
    	int i;
    	total = counter;
    
             srand(time(NULL)); //generate the random seed generator
             num =  rand() % 100 +1 ;  //pick a number from 1 to 100
    
    
    
            printf("I generate a random number between 1 and 100\n");
            printf("Can you guess the number?\n");
            printf("Please enter your first guess.\n");
    
            scanf("%d", &guess);
    
            while (num != guess)
            {
    
               if (guess > 100 && guess < 0)
    	  {
    	     printf("Number out of range\n");
    	     printf("Try again\n");
    	     ++counter;
    	  }
    
    
               else if (guess > num)
    	  {
                  printf("Too High! try again!\n");
                  scanf("%d", &guess);
    	     ++counter;
    	  }
    
               else if (guess < num)
    	  {
                 printf("Too Low! try again!\n");
                 scanf("%d", &guess);
    	    ++counter;
    	  }
    
           } //end while
    
    
           printf("You guessed the number in %d tries!\n",counter);
           i = counter/ guess;
    
           if(counter >= 16)
    	   {
    	  printf("You need more practice \n",counter);
    	   }
    	  else if (counter >= 11 && counter <= 15)
    	   {
    	  printf("Your getting better \n",counter);
    	   }
    	  else if (counter >= 6 && counter <= 10)
    	   {
    	 printf("Practice Pays off\n");
    	   }
    	 else if (counter >= 2 && counter <= 5)
    	   {
    	 printf("Nice job \n",counter);
    	   } 
    	 else if (counter == 1)
    	   {
    	 printf("You got lucky !!\n",counter);
    	   }
    
    printf("%d is the average number of tries\n",i);
    
    
    return 0;
    
    }//end of main
    [COLOR=darkblue]

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Here is a first suggestion:

    Your line
    Code:
    if (guess > 100 && guess < 0)
    Just say it to yourself, if guess is bigger than 100 AND guess is smaller than 0. That is never going to be true therefore it is unreachable code. What you want is OR || like this:

    Code:
    if (guess > 100 || guess < 0)
    That should be a little help.

    Edit: Actually looking back, it should probably be <= 0 because the valid range is 1 to 100. So either <= 0 or < 1.
    Last edited by MrWizard; 03-08-2004 at 01:03 AM.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Also notice that there is no scanf() for the out of range case.
    This means that if the user guesses 0 (for example) the code
    will happily go round and round the while loop for ever.

    Personally I would structure the while loop slightly differently:
    Code:
    while (num != guess)
    {
      scanf("%d", guess);
      ++counter;
    
      if (guess > 100 || guess <= 0)
      {
        printf("Number out of range\n");
        printf("Try again\n");
      }
      else if (guess > num)
      {
        printf("Too High! try again!\n");
      }
      else if (guess < num)
      {
        printf("Too Low! try again!\n");
      }
    } //end while
    I've done this because you want to get a new guess from the
    user and increment the guess count every time round the loop.
    By putting it in the code in only one place you avoid:
    1) typing (!)
    2) forgetting it in one branch of the code (like you did)
    3) in a large program its a pain to find all the scattered places
    to make a simple change. (e.g. imagine scanf("%d", guess)
    had to become scanf("%g", guess) )
    DavT
    -----------------------------------------------

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Originally posted by DavT

    3) in a large program its a pain to find all the scattered places
    to make a simple change. (e.g. imagine scanf("%d", guess)
    had to become scanf("%g", guess) )
    like when you forget something like this

    Code:
     scanf("%d", &guess);
    Also, Intabate, for your printf statements:
    Code:
     if(counter >= 16)
    	   {
    	  printf("You need more practice \n",counter); /* this line */
    	   }
    	  else if (counter >= 11 && counter <= 15)
    	   {
    	  printf("Your getting better \n",counter); /* this line */ 
    	   }
    	  else if (counter >= 6 && counter <= 10)
    	   {
    	 printf("Practice Pays off\n");
    	   }
    	 else if (counter >= 2 && counter <= 5)
    	   {
    	 printf("Nice job \n",counter); /* this line */
    	   } 
    	 else if (counter == 1)
    	   {
    	 printf("You got lucky !!\n",counter); /* this line */
    	   }
    
    printf("%d is the average number of tries\n",i);
    Take a look at what you have done here - on some of these you are either passing too many arguments or else you have forgotten to add some format specifiers, depending on what you intended to do.

    ~/

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    like when you forget something like this
    Exactly! (oops!)
    DavT
    -----------------------------------------------

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21