Thread: Calling functions

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    9

    Calling functions

    Hi everyone,

    I'm looking for some advice on how to go about my program (not looking for you to do my homework!).

    I have to write a program that gives a user two choices for a game or to exit.

    The two games, are a dice game(guessing the role of two dice), and the second is guessing a number between 1 and 100.

    I have written both games separately and they both work. However, I am having trouble putting them together in a single program, because the games' functions need to be called, and not just in the regular main function.

    Here is my code for the guessing game:
    Code:
    :
    
    #include<stdio.h>#include<stdlib.h>
    
    
    void main()
    {
        int random_number, user_input;
        random_number=rand() %100+1;
        int counter=0;
    printf("Welcome to the number guessing game!\n");
        do
        {
        printf("Guess the random number between 1 and 100:\n");
        scanf("%d",&user_input);
    
    
        if (user_input == random_number){
           printf("That is the random number!\n");
        }
        else if (user_input > random_number){
            printf("Your guess is too high.\n");
        }
        else if(user_input < random_number){
            printf("Your guess is too low.\n");
        }
        counter++;
    
    
        }while (random_number!=user_input);
    
    
        printf("\nCongratulations! It took you %d tries", counter);
    
    
    
    
        return 0;
    
    
        }
    So is there a way to take all the above code, and call it say "function_guess" and just put it at the top of my program, then when I need to call it I put "function_guess ()", and then it executes the code?

    Thanks for your time, I hope I am not being too confusing.

    -cda67

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by cda67 View Post
    So is there a way to take all the above code, and call it say "function_guess" and just put it at the top of my program, then when I need to call it I put "function_guess ()", and then it executes the code?
    Sure. Rename 'main' to 'function_guess', then include the correct main method at the bottom:
    Code:
    int main( void )
    {
        function_guess();
        return 0;
    }
    Main should return an int, because that's what your operating system expects from it.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cda67 View Post
    Hi everyone,
    Here is my code for the guessing game:
    Code:
    :
    
    #include<stdio.h>#include<stdlib.h>
    
    
    void main()
    {
        int random_number, user_input;
        random_number=rand() %100+1;
        int counter=0;
    printf("Welcome to the number guessing game!\n");
        do
        {
        printf("Guess the random number between 1 and 100:\n");
        scanf("%d",&user_input);
    
    
        if (user_input == random_number){
           printf("That is the random number!\n");
        }
        else if (user_input > random_number){
            printf("Your guess is too high.\n");
        }
        else if(user_input < random_number){
            printf("Your guess is too low.\n");
        }
        counter++;
    
    
        }while (random_number!=user_input);
    
    
        printf("\nCongratulations! It took you %d tries", counter);
    
    
    
    
        return 0;
    
    
        }
    First you need to fix a few problems...
    line 1 ... You have two #include statments on the same line. The second one will not be seen by the compiler
    line 6 ... The correct form of main is... int main (void) ... not... void main()
    line 38 .... This is correct only after fixing line 6

    Your indentation needs work and a your source needs cleaning up.

    You should probably #include <time.h> and use... srand(time(null)); before picking your number.
    As it is now you'll get the same number every time.

    To "functionize" this you can simply do as Quzah suggests and rename main() to HiLoGame() or such...

    To make the game more challenging you should know that a person working logically can guess 1 of 1000 down to a 50/50 chance in 10 turns... 1 in 100 can be reduced to a 50/50 chance in 6 tries. So you might want to limit the person's number of turns.

    FWIW... what you have here is the core logic of a Binary Search algorythm which can be used to very rapidly locate records in large sorted arrays or random acces disk files... The logic is to start at 1/2 of the range... too low? too high? ... now go to 1/2 the remaining range... and so on... It's an extremely fast way of finding things... one record in a million can be found in only 21 tries...
    Last edited by CommonTater; 10-15-2011 at 12:00 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. not calling functions
    By BungleSpice in forum C Programming
    Replies: 9
    Last Post: 03-06-2004, 01:36 AM
  2. Calling functions help
    By ForlornOdium in forum C++ Programming
    Replies: 14
    Last Post: 09-29-2003, 08:40 PM
  3. Calling App Functions from DLL
    By johnnie2 in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2003, 01:08 AM
  4. calling functions::
    By Yoshi in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2002, 02:34 PM
  5. I need some help on calling other functions in C
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-23-2002, 12:07 PM