help with functions??

This is a discussion on help with functions?? within the C Programming forums, part of the General Programming Boards category; We just started learning functions in my c programming class. We havent learned hardly anything about them yet, but we ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    53

    Smile help with functions??

    We just started learning functions in my c programming class. We havent learned hardly anything about them yet, but we already have an assignment. It is a random number generator with a few things added in that our professor wanted. I have a working version, BUT, he wants us to do this:
    Your program must decompose the problem into functions. Specifically, you must
    declare and use a function that accepts two integer arguments as input and returns a value
    as output. All of your printing must be done in the main function. The input arguments
    represent a user’s guess and the correct answer. The returned value must reflect one of
    the three possible outcomes of comparing the guess to the correct answer. The outcomes
    are “Too Low”, “Too High”, or “Correct”. I recommend representing those outcomes
    using integer values of -1, 1, and 0, respectively.

    heres what i have...how do i do what he wants??

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    { /* begin function main */
    
      int num; /* numbers generated */
      int guess = 0;
      int game = 0;
      int counter = 0;	
    
      srand(time(NULL)); /* generate the random seed generator */
      num =  rand() % 1000 +1 ;  /* pick a number from 1 to 1000 */
    
      printf("I am thinking of a random number between 1 and 1000\n"); /* prompt user for input */
      printf("Try and guess the number.\n"); /* prompt user for input */
      printf("Please enter your first guess.\n"); /* prompt user for input */
    
      do
      {
        while (num != guess)
        {
          scanf("%d", &guess); /* input data from user */
          ++counter;
    
          if (guess > 1000 || 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 loop */
    
        printf("You guessed the number in %d tries!\n",counter);
        printf("Do you want to play again?\n");
        printf("Enter  n or N to quit\n");
    	while ( getchar() != '\n' )
          ;
        game = getchar();
        getchar();
      } /*end of do while */
      while (game != 'n' && game != 'N'); 
    
      return 0;
    
    }/* end function main */
    thx

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    What is it about functions you don't understand? Do you know how to define one?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    i figured it out, i didnt know how to define one first of all,but i understand now. our instructor made it sound like we had to create a function somewhere else and then call it up on our program. turns out i just had to define and create it at the top of the program. Bad instructions lol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

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