Thread: Where to start in writing a 'calling function'??

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    Where to start in writing a 'calling function'??

    Hello everyone,
    I was wondering what is the best way to approach a function calling program.

    Suppose someone has written a function that I need to use in my program. Basically the program is suppose to output a greeting message, then the user enters a guess (its a guessing game), and the computer outputs whether the guess is correct based on the conditions of the function that was written....


    I am completely new to this and I am probably over-complicating things, but what would be the best way to approach such a problem?


    thank you,
    Matt H.

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    The idea is that you create a loop that waits for user input, validates and acts accordingly. I've written a tutorial on how to create a tic tac toe game in c++ which is actually pretty easy and is close to what you want to do. Maybe you would want to take a look :

    programming tic tac toe game in C++

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    sure! thank you

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    So i started my program. I basically need to create a Mastermind game (it is basically a guessing game).

    I was trying to set up the beginning of my program output by compiling and running my program frequently and making sure everything is running correctly... However as soon as I got to my if statement my program is not working like I want it to.

    All i want my if statement to do is check whether or not the information provided by the user is an integer between 0 and 5 inclusive and if it is I want it to print "GOOD". That's all.

    Can anyone help me achieve this?

    Code:
    
    #include <stdio.h>
    #include <ctype.h>
    
    int main () {
        
    int guess1, guess2, guess3, guess4;
    
       
    printf("Welcome to Mastermind!\n\nAt each turn, you will enter your guess for the playing board.\n");
    printf("A valid guess has 4 values in between 0 and 5.\n");
    printf("Each guess will have each number within the guess separated by a space.\n");
    printf("When you are ready, enter your first guess.\n");
    printf("From that point on, you will be told how many perfect and imperfect matches you have.\n");
    printf("After this message, you should guess again. You have 10 chances, good luck!\n\n\n");
    
    printf("Please enter your first guess now:\n");
    scanf("%d%d%d%d", guess1, guess2, guess3, guess4);
    
    
    if ((guess1 = 0 > 5) && (guess2 = 0 > 5) && (guess3 = 0 > 5) && (guess4 = 0 > 5));
    printf("GOOD");
    
    
    system("PAUSE");
    return 0;
    
    }
    Thank you

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Can anyone help me achieve this?

    I think so!

    Code:
    
    #include <stdio.h>
    #include <ctype.h>
    
    int main () {
        
      int guess1, guess2, guess3, guess4;
      int guess[4], badGuess;  //new variables
    
    
      do {     //new loop
        printf("Welcome to Mastermind!\n\nAt each turn, you will enter your guess for the playing board.\n");
        printf("A valid guess has 4 values in between 0 and 5.\n");
        printf("Each guess will have each number within the guess separated by a space.\n");
        printf("When you are ready, enter your first guess.\n");
        printf("From that point on, you will be told how many perfect and imperfect matches you have.\n");
        printf("After this message, you should guess again. You have 10 chances, good luck!\n\n\n");
    
        printf("Please enter your first guess now:\n");
        scanf("%d%d%d%d", &guess[0], &guess[1], &guess[2], &guess[3]);
    
    
        //if ((guess1 = 0 > 5) && (guess2 = 0 > 5) && (guess3 = 0 > 5) && (guess4 = 0 > 5));
        //printf("GOOD");
    
        //888888888888888888 New code 8888888888888888
    
        for(i = 0, badguess = 0; i < 4; i++)
          if((guess[i]) >= 0) && (guess[i] <= 5))
          ;
          else {
            badGuess = 1;
            break;
          }
        }
    
        if(!badguess)
          printf("\nGood!");
        getchar();  
      }while(badguess);
    
      system("PAUSE");
      return 0;
    
    }
    Scanf() needs the '&' char, in front of non-string variables, so it can change them.

    Hope that helps.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Adak your code looks awesome!
    I was wondering.... is my code just as efficient and longer way to start my program? would it work as well?

    I kind of find it hard to understand whats going on with your code because my lack of experience:/ what does 'break', 'getchar(), and the last 'while' in the program do?


    Thank you for the advice, my program now runs correctly

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    Actually i have an old book on C++ that was all about creating a mastermind type game. Can't find it now since i'm not home, but it was a good example of such a game.

    At the code you are looking for, break is a way to instruct a program to get out of the current loop (whether that is a for or a while loop). getchar() is used to read a character from the user input (keyboard) and the last while is actually used in the do - while expression. the diffrence between do - while and while is that the first one gets executed at least once and keeps going while the condition is true. On the other hand, while alone cannot be executed at all if the condition is not met. Do - while is definately executed at least once.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    what does
    Code:
    system("PAUSE");
    mean?

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    break:
    causes program to break out of the do while, while, or for loop, with no further looping. Program continues on the first line after the loop.

    getchar():
    checks the keyboard buffer. If a keystroke is there, it removes it. If you have a variable set to "catch" the keystroke, it will assign it: YourChar = getchar(); . If no keystroke is found on the keyboard buffer, then it will stop the program, and wait for you to enter a key and hit <enter>. It will remove the keystroke, but leave the '\n' (newline char), in that case, still on the keyboard buffer.

    Here, it removes a newline, which the scanf() call, (above it in the loop), left behind.

    Code:
    do {
    //some code in here
    }while(someVariable < someValue);
    Just like a while loop, but the test (to see if it should loop again), comes only at the bottom of the loop, instead of the top (like a normal while loop would). Do while loops will always be executed at least once. (while loops may not execute once - depending on the results of the test they perform, first).

    system("pause");
    system sends the string in quotes, to the operating system. Pause (if and ONLY if your directory is "in the path" [because this is DOS stuff, here], will cause the program to pause and the message "Press Any Key to Continue" to be printed on the bottom row of your monitor.

    If you are not in a directory "in the path", then the system("pause"), command, will do nothing, and be ignored.

    getchar() does the same thing, without calling the operating system, but it doesn't print any message (you have to print your own), and you have to hit <enter>, instead of just any key, to continue.

    I would never worry much about the efficiency of a program that is heavily interactive (gets key strokes), from the user. Waiting for a user to enter their keystrokes will takes hundreds of times more seconds, than any efficiency you could gain or lose, in a small program like this.

    Where efficiency is paramount, is in things like video editing, sorting, searching, compression, encrypting, molecular modeling simulations, etc. Things like this are HUGELY computationally intensive, and if the job is big, then the efficiency becomes very critical, indeed.

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by Adak View Post
    If you are not in a directory "in the path", then the system("pause"), command, will do nothing, and be ignored.
    In windows, pause is a built-in shell command so it will work even if you set the path to nothing. (Unless you've written your own pause command, then it'd have to be in the path or the default one would be used)
    In *nix, the pause command has to exist in the search path or it won't work. (there might be some shell that has 'pause' as a built in command, but I don't know of any at least)
    In dos (pure dos, not a windows command shell), I don't know exactly how it works.

    For all of the above, it doesn't matter what your current working directory is set to. (This is how I interpreted "If you are not in a directory "in the path"". Maybe you meant something else?)

  11. #11
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Quote Originally Posted by Adak View Post
    system("pause");
    system sends the string in quotes, to the operating system. Pause (if and ONLY if your directory is "in the path" [because this is DOS stuff, here], will cause the program to pause and the message "Press Any Key to Continue" to be printed on the bottom row of your monitor.

    If you are not in a directory "in the path", then the system("pause"), command, will do nothing, and be ignored.

    getchar() does the same thing, without calling the operating system, but it doesn't print any message (you have to print your own), and you have to hit <enter>, instead of just any key, to continue.
    Yes as Adak says you can achieve the same thing system("PAUSE"); does:
    Code:
    printf("Press ENTER to continue\n");
    getchar();
    Except now the program is portable.

  12. #12
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Sooo... I really don't understand where the functions comes into play in my program.... I have a separate code that has a bunch of pre-written functions that I am suppose to use, do I just copy and paste them? or am I suppose to somehow call it with a file pointer?


    The chapter that I have read about calling functions is not very clear.

    .... I just re-read the chapter about calling functions and it just seems like I can copy and paste the code (framework) for my program from the other .c file and I just need to combine it with my code and make it work smoothly, does this sound correct?
    Last edited by matthayzon89; 03-09-2010 at 10:34 AM.

  13. #13
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    I am really having a hard time can someone please help me?
    I have been trying for hours...

    I understand that I need to include <filename.h> in my program in order to call the functions in that .c file however the name of the library is not given to me.... I am really lost , I have been trying to get started on this program for hourss...

  14. #14
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    For example this is the contents of my Pre-written functions .c document that I need to call into my program:


    Code:
    #include <stdio.h>
    #include <time.h>
    
    #define NUM_PEG 6
    #define NUM_COLOR 4
    #define MAX_TURN 15
    #define MARKED_BOARD -1
    #define MARKED_ANSWER -2
    
    // Functiones used.
    void fillBoard(int board[]);
    int numPerfectMatches(int board[], int answer[]);
    void getUserGuess(int board[]);
    void copyArray(int dest[], int source[], int length);
    void markOutCorrect(int board[], int answer[]);
    int numWrongPlaceMatches(int board[], int answer[]);
    void greeting();
    void printArray(int array[], int length);
    void printWinner(int num_turns, int num_seconds);  ......etc...

    This is my program so far:

    Code:
    #include <stdio.h>
    #include <time.h>
    
    
    int main () {
    
    
    int guess1, guess2, guess3, guess4;
    
    printf("Welcome to Mastermind!\n\nAt each turn, you will enter your guess for the playing board.\n");
    printf("A valid guess has 4 values in between 0 and 5.\n");
    printf("Each guess will have each number within the guess separated by a space.\n");
    printf("When you are ready, enter your first guess.\n");
    printf("From that point on, you will be told how many perfect and imperfect matches you have.\n");
    printf("After this message, you should guess again. You have 10 chances, good luck!\n\n\n");
    
    printf("Please enter your first guess now:\n");
    scanf("%d%d%d%d", &guess1, &guess2, &guess3, &guess4);
    
    
    if ((guess1 = 0 > 5) && (guess2 = 0 > 5) && (guess3 = 0 > 5) && (guess4 = 0 > 5));
    printf("GOOD\n\n");
    
    
    system("PAUSE");
    return 0;
    
    }

  15. #15
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    This is the header file that I have made from the codes above but I am unsure if it works:/

    Code:
    #define NUM_PEGS 4
    #define NUM_COLORS 6
    #define MAX_TURNS 10
    #define MARKED_BOARD -1
    #define MARKED_ANSWER -2
    
    
    void masters()
    {
    void fillBoard(int board[]);
    int numPerfectMatches(int board[], int answer[]);
    void getUserGuess(int board[]);
    void copyArray(int dest[], int source[], int length);
    void markOutCorrect(int board[], int answer[]);
    int numWrongPlaceMatches(int board[], int answer[]);
    void greeting();
    void printArray(int array[], int length);
    void printWinner(int num_turns, int num_seconds);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  5. Function calling woes
    By RedZippo in forum C Programming
    Replies: 6
    Last Post: 01-09-2004, 12:39 AM