Thread: Can someone help me find out why my program is unable to compile??

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

    Can someone please help me understand why program is not running correctly?

    tr45
    Last edited by matthayzon89; 03-18-2010 at 03:30 PM.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Your title says it's unable to compile, your post says it compiles. You don't give us any errors that you're receiving either way.

    You're just not picking up on this whole Internet forum thing, are you?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Rags_to_Riches: ^^^ ROFL!!


    I was able to get this to compile, but not your original program:

    Code:
    #include <stdio.h>
    #include <time.h>
    
    
    // Constants used in the program.
    #define NUM_PEGS 4
    #define NUM_COLORS 6
    #define MAX_TURNS 10
    #define MARKED_BOARD -1
    #define MARKED_ANSWER -2
    
    // Functions 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);
    
    int main() {
       int num_perfect, num_imperfect, start, end; 
        
        // Stores the real answer and the user's board, respectively.
        int answer[NUM_PEGS];
        int board[NUM_PEGS];
        
        // Set everything up.
        // Seed the random number generator.
        int num_turns = 0;    
        srand(time(0));
    
        fillBoard(answer);
        greeting();
        start = time(0);
     
        // Loop through each turn, ending at 10 moves or a win.
        do {
            
            // Read in the user's guess.
            getUserGuess(board);
            
            // Figure out the number of matches.
            num_perfect = numPerfectMatches(board, answer);
            num_imperfect = numWrongPlaceMatches(board, answer);
            
            // Update for this turn.
            printf("You have %d perfect matches and %d imperfect matches.\n", num_perfect, num_imperfect);
            num_turns++;
            
        } while (num_turns < MAX_TURNS && numPerfectMatches(board, answer) < NUM_PEGS);
        
        // Winning case.
        if (numPerfectMatches(board, answer) == NUM_PEGS) {
            end = time(0);
            printWinner(num_turns, end-start);
        }
        
        // Losing case.
        else {
            printf("Sorry, you have exceeded the maximum number of turns. You lose.\n");
            printf("Here is the winning board: ");
            printArray(answer, NUM_PEGS);
            printf("\n");
        }
        
        system("PAUSE");
        return 0;
    }
    
    // Pre-conditions: board is uninitialized and of size NUM_PEGS.
    // Post-conditions: board will be filled in randomly with values ranging from
    //                  0 to NUM_COLORS-1.
    void fillBoard(int board[]) {
         int i;
         for(i=0; i < NUM_PEGS; i++) {
            board[i] = rand()% NUM_COLORS;
         }
    }
    
    // Pre-conditions: board and answer are of length NUM_PEGS and store the player's
    //                 guess and the correct board, respectively.
    // Post-conditions: returns the number of pegs that match at the correct spots.
    int numPerfectMatches(int board[], int answer[]) {
      int i, num_perfect;
       {
        for (i=0; i<NUM_PEGS; i++) {
           if (answer[i]==board[i])
             num_perfect+=1;
        }
      }
      return num_perfect;
    }
    
    // Pre-conditions: None.
    // Post-conditions: Prints out a greeting for the beginning of the game.
    void greeting() {
    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");
       
    }
    
    // Pre-conditions: board is of length NUM_PEGS
    // Post-conditions: NUM_PEGS integers are read in from the user and stored in
    //                  board in the natural order.
    void getUserGuess(int board[]) {
         
         int i = 0;
         for (i = 0; i < NUM_PEGS; i++) {
             scanf("%d", board[i]);
         }
     
    }
    
    // Pre-conditions: board and answer are of length NUM_PEGS and store the player's
    //                 guess and the correct board, respectively.
    // Post-conditions: returns the number of pegs that match at INCORRECT spots
    //                  after "erasing" those pegs that match at correct spots.
    int numWrongPlaceMatches(int board[], int answer[]) {
           
        // Create temporary copies of both boards, since we'll need to make changes
        // to each during this process and we don't want to ruin the original
        // state of either board.
         int tempAns[NUM_PEGS];
         int tempBoard[NUM_PEGS];
         int i;
         //create a copy of my answer board
         copyArray(tempAns, answer, NUM_PEGS);
         //create a copy of my board
         copyArray(tempBoard, board, NUM_PEGS);
         //call the function
         markOutCorrect(tempBoard ,tempAns);
        // Mark out the spots on the board and answer that match up perfectly.
        // Go through the board and try to match each peg on the board to
        // a spot on the answer. Hint: A double loop is needed.
        // Note: Remember to "mark" an answer when a match is found.
        
       /* Logic is needed to calculate the number of wrong matches
       that have been found. Then that value should be returned
       by this function.
       */
       return 0; //Just for now. 
    }
    
    // Pre-conditions: dest and source both have the given length.
    // Post-conditions: All values from source are copied into the corresponding
    //                  locations in dest.
    void copyArray(int dest[], int source[], int length) {
         int i = 0;
         for (i = 0; i < length; i++) 
              dest[i] = source[i];
    
    
    }
    
    // Pre-conditions: board and answer are of length NUM_PEGS.
    // Post-conditions: For each corresponding slot that matches in both boards,
    //                  the slots are changed to equal each board's appropriate
    //                  marker.
    void markOutCorrect(int board[], int answer[]) {
          int i;
          for(i=0; i<NUM_PEGS; i++) 
            if (board[i]==answer[i])
            {
               answer[i] =  MARKED_ANSWER;
               board[i] =   MARKED_BOARD;                     
            }
       
        
    }
    
    // Pre-conditions: array has the given length.
    // Post-conditions: each value of array is printed, separated by a space.
    void printArray(int array[], int length) {
         int i;
         for(i=0; i < length; i++)
           printf("%d ", array[i]);
    
    }
    
    // Pre-conditions: num_turns is the number of turns it took the winner to win,
    //                 and num_seconds is the total amount of time they took to
    //                 win in seconds.
    // Post-conditions: A winning message is printed stating the number of moves
    //                  and amount of time taken to win in M:SS format.
    void printWinner(int num_turns, int num_seconds) {
    
                                  
    printf("You have won the game in %d turns and %d seconds", num_turns, num_seconds);
    
    }
    The part in red is lacking code to play the game. I put in "return 0", just to make it compile.

    You should have been getting warnings or errors. Are your warnings or errors not turned up all the way?

    After you get the logic and return added, see how it does. If you want to add variables "any old place" in the functions, instead of only at the top of the function, then I won't be able to advise you further.
    Last edited by Adak; 03-18-2010 at 02:41 PM.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    12
    Yeah i also Looked at it. I cant figure out that function

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Okay so i worked on my code and now it works perfect, the only thing NOW is the time that prints out prints out in SS format instead of MM:SS. Can anyone help me with that?

    Code:
    #include <stdio.h>
    #include <time.h>
    
    
    // Constants used in the program.
    #define NUM_PEGS 4
    #define NUM_COLORS 6
    #define MAX_TURNS 10
    #define MARKED_BOARD -1
    #define MARKED_ANSWER -2
    
    // Functions 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);
    
    int main() {
        
        // Seed the random number generator.
        srand(time(0));
        
        // Stores the real answer and the user's board, respectively.
        int answer[NUM_PEGS];
        int board[NUM_PEGS];
        
        // Set everything up.
        int num_turns = 0;    
        fillBoard(answer);
        greeting();
        int start = time(0);
        printArray(answer, 4);
        
        // Loop through each turn, ending at 10 moves or a win.
        do {
            
            // Read in the user's guess.
            getUserGuess(board);
            
            // Figure out the number of matches.
            int num_perfect = numPerfectMatches(board, answer);
            int num_imperfect = numWrongPlaceMatches(board, answer);
            
            // Update for this turn.
            printf("You have %d perfect matches and %d imperfect matches.\n\n", num_perfect, num_imperfect);
            num_turns++;
            
        } while (num_turns < MAX_TURNS && numPerfectMatches(board, answer) < NUM_PEGS);
        
        // Winning case.
        if (numPerfectMatches(board, answer) == NUM_PEGS) {
            int end = time(0);
            printWinner(num_turns, end-start);
        }
        
        // Losing case.
        else {
            printf("Sorry, you have exceeded the maximum number of turns. You lose.\n");
            printf("Here is the winning board: ");
            printArray(answer, NUM_PEGS);
            printf("\n");
        }
        
        system("PAUSE");
        return 0;
    }
    
    // Pre-conditions: board is uninitialized and of size NUM_PEGS.
    // Post-conditions: board will be filled in randomly with values ranging from
    //                  0 to NUM_COLORS-1.
    void fillBoard(int board[]) {
         int i;
         for(i=0; i < NUM_PEGS; i++) {
            board[i] = rand()% NUM_COLORS;
         }
    }
    
    // Pre-conditions: board and answer are of length NUM_PEGS and store the player's
    //                 guess and the correct board, respectively.
    // Post-conditions: returns the number of pegs that match at the correct spots.
    int numPerfectMatches(int board[], int answer[]) {
      int i, num_perfect= 0;
       {
        for (i=0; i<NUM_PEGS; i++) {
           if (answer[i]==board[i])
             num_perfect++;
        }
      }
      return num_perfect;
    }
    
    // Pre-conditions: None.
    // Post-conditions: Prints out a greeting for the beginning of the game.
    void greeting() {
    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");
       
    }
    
    // Pre-conditions: board is of length NUM_PEGS
    // Post-conditions: NUM_PEGS integers are read in from the user and stored in
    //                  board in the natural order.
    void getUserGuess(int board[]) {
         
         int i = 0;
         for (i = 0; i < NUM_PEGS; i++) {
             scanf("%d", &board[i]);
         }
     
    }
    
    // Pre-conditions: board and answer are of length NUM_PEGS and store the player's
    //                 guess and the correct board, respectively.
    // Post-conditions: returns the number of pegs that match at INCORRECT spots
    //                  after "erasing" those pegs that match at correct spots.
    int numWrongPlaceMatches(int board[], int answer[]) {
           
        // Create temporary copies of both boards, since we'll need to make changes
        // to each during this process and we don't want to ruin the original
        // state of either board.
         int tempAns[NUM_PEGS];
         int tempBoard[NUM_PEGS];
         int i;
         int j;
         int num_imperfect = 0;
          
          //create a copy of my answer board
         copyArray(tempAns, answer, NUM_PEGS);
         //create a copy of my board
         copyArray(tempBoard, board, NUM_PEGS);
         //call the function
         markOutCorrect(tempBoard, tempAns);
        
        for (i=0; i<NUM_PEGS; i++) {
            for (j = 0; j < NUM_PEGS; j++){
                if(tempBoard[i] == tempAns[j]){
                  num_imperfect++;
                  tempBoard[i] = -1;
                  tempAns[j] = -2; 
                }
            }
            
        }
         
    
         
        
        return num_imperfect;
    }
    
    // Pre-conditions: dest and source both have the given length.
    // Post-conditions: All values from source are copied into the corresponding
    //                  locations in dest.
    void copyArray(int dest[], int source[], int length) {
         int i = 0;
         for (i = 0; i < length; i++) 
              dest[i] = source[i];
    
    
    }
    
    // Pre-conditions: board and answer are of length NUM_PEGS.
    // Post-conditions: For each corresponding slot that matches in both boards,
    //                  the slots are changed to equal each board's appropriate
    //                  marker.
    void markOutCorrect(int board[], int answer[]) {
          int i;
          for(i=0; i<NUM_PEGS; i++) 
            if (board[i]==answer[i])
            {
               answer[i] =  MARKED_ANSWER;
               board[i] =   MARKED_BOARD;                     
            }
       
        
    }
    
    // Pre-conditions: array has the given length.
    // Post-conditions: each value of array is printed, separated by a space.
    void printArray(int array[], int length) {
         int i;
         for(i=0; i < length; i++)
           printf("%d ", array[i]);
    
    }
    
    // Pre-conditions: num_turns is the number of turns it took the winner to win,
    //                 and num_seconds is the total amount of time they took to
    //                 win in seconds.
    // Post-conditions: A winning message is printed stating the number of moves
    //                  and amount of time taken to win in M:SS format.
    void printWinner(int num_turns, int num_seconds) {
    
                                  
    printf("You have won the game in %d turns and %d seconds\n", num_turns, num_seconds);
    
    }

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You want something like this?

    Code:
    while(seconds > 59) {
      minutes++;
      seconds -= 60;
    }
    
    printf("\n  Elapsed Time: %02d:%02d", minutes, seconds);
    If you needed something more elaborate, time.h has a struct with all kinds of stuff, but this seems simpler to just roll your own.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Adak sometimes likes to keep his processor busy
    A shorter option:
    Code:
    printf("\n  Elapsed Time: %02d:%02d", seconds / 60, seconds % 60); // mm:ss
    or even
    Code:
    printf("\n  Elapsed Time: %d:%02d:%02d", seconds / 3600, seconds / 60 % 60, seconds % 60); // h:mm:ss
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by iMalc View Post
    Adak sometimes likes to keep his processor busy
    Yep! That's me - no lazy processors!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Struct Program to find Point in Rectangle
    By DMJKobam in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2009, 08:56 PM
  3. help newbie compile program!
    By candysandra88 in forum C Programming
    Replies: 7
    Last Post: 12-13-2008, 11:27 PM
  4. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM
  5. how to compile a c program in Linux
    By xman in forum Linux Programming
    Replies: 4
    Last Post: 03-18-2003, 10:41 AM