Thread: Need Help Getting Program To work?

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    13

    Need Help Getting Program To work?

    Hello everyone there's this program I need to make for my programming class. I believe I wrote the majority of it right but now I just need help putting the four functions I wrote into main which I'm not sure how to do. I'll include an example of the sample output I want so that you can see what I'm trying to do. Sample Output
    Please make a selection from the following:
    1. Play Arithmetic Game.
    2. Play Guessing Game.
    3. Print Score.
    4. Quit.
    1

    Would you like, 1)Addition or 2)Multiplication?
    1

    What is the maximum number you would like?
    100

    What is 21+86?
    107
    Correct, great job!
    What is 87+96?
    173
    Sorry, that's incorrect, the answer is 183.
    What is 86+70?
    156
    Correct, great job!
    What is 55+4?
    59
    Correct, great job!
    What is 13+17?
    30
    Correct, great job!
    What is 89+73?
    162
    Correct, great job!
    What is 22+11?
    33
    Correct, great job!
    What is 67+9?
    76
    Correct, great job!
    What is 35+94?
    129
    Correct, great job!
    What is 16+85?
    101
    Correct, great job!
    Your score for the round is 9.

    Please make a selection from the following:
    1. Play Arithmetic Game.
    2. Play Guessing Game.
    3. Print Score.
    4. Quit.
    2

    Enter the guess!
    50
    Your guess is too high, try again.

    Enter your guess!
    30
    Your guess is too high, try again.

    Enter your guess!
    10
    Your guess is too high, try again.

    Enter your guess!
    5
    Your guess is too low, try again.

    Enter your guess!
    7
    Your guess is too high, try again.

    Enter your guess!
    6
    Great, you guessed the correct number 6 in 6 guesses.

    Your score for the round is 10.

    Please make a selection from the following:
    1. Play Arithmetic Game.
    2. Play Guessing Game.
    3. Print Score.
    4. Quit.
    3
    Your score is 19.

    Please make a selection from the following:
    1. Play Arithmetic Game.
    2. Play Guessing Game.
    3. Print Score.
    4. Quit.
    4

    Thank you for playing!

    Now here's the code I have so far.

    Code:
     #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    //prototypes
    int arithGame(int max, int op);
    int guessGame();
    int max(int a, int b);
    int min(int a, int b);
    
    
    //mian function
    int main() {
        int choice;
        //seed random number generator.
        srand(time(0));
    
    
    
    
        printf("Please make a selection from the following: \n");
        printf("1. Play Arithmetic Game. \n");
        printf("2. Play Guessing Game. \n");
        printf("3. Print Score. \n");
        printf("4. Quit. \n");
        scanf("%d", &choice);
    
    
        switch (choice) {
    
    
        case 1:
    
    
    
    
        }
    
    
    
    
    
    
        return 0;
    }
    
    
    /*Pre-Conditions: This function takes in two integers, max and op.
              This function gives the user 10 arithmetic questions, where
    each operand ranges from 1 to max, inclusive. The value of
    operator dictates whether the problems are addition or
    multiplication problems.
    Namely, if op is 1, they are addition problems, otherwise,
    they are multiplication problems.
          *Post-Conditions:  The function returns the number problems solved
            correctly, and prints out for the user after their answer
    whether or not they got the question correct.
            */
    
    
    int arithGame(int max, int op) {
        int ans, score, i;
        printf("Would you like, 1)Addition or 2)Multiplication? \n");
        scanf("%d", &op);
        printf("What is the maximum number you would like? \n");
        scanf("%d", &max);
        if (op==1) {
        for(i=1;i<10;i++) {
            printf("What is %d + %d? \n", 1+rand()%max,1+rand()%max);
            scanf("%d", &ans);
            if(ans== (1+rand()%max) + (1+rand()%max)){
                printf("Correct! Great job! \n");
                score++;
            }
            else
                printf("Sorry, that's incorrect, the answer is %d", (1+rand()%max) + (1+rand()%max));
        }
        printf("Your score for the round is %d \n", score);
    }
    else {
        for(i=1;i<10;i++) {
            printf("What is %d * %d? \n", 1+rand()%max,1+rand()%max);
            scanf("%d", &ans);
            if(ans== (1+rand()%max) * (1+rand()%max)){
                printf("Correct! Great job! \n");
                score++;
            }
            else
                printf("Sorry, that's incorrect, the answer is %d", (1+rand()%max) * (1+rand()%max));
        }
        printf("Your score for the round is %d \n", score);
    }
    }
    
    
    
    /*Pre-Conditions: This function does not take in any parameters.
        This function allows the user to play the guessing game
        where the randomly generated number lies in between 1
        and 100, inclusive.
      *Post-Conditions:  The value returned is the score of the user in the game.
    This score is 17 minus the number of guesses unless this
    value is lower than 0 or greater than 10.
    In these cases, 0 and 10 are returned, respectively.
      */
    
    
    int guessGame() {
     int secnum, guess, num_guesses;
     secnum = rand()%100+1;
     while (guess != secnum) {
        printf("Enter the guess! \n");
        scanf("%d", &guess);
        num_guesses++;
        if (max(secnum, guess) == guess)
            printf("Your guess is too high, try again. \n");
        else if (min(secnum, guess) == guess)
            printf("Your guess is too low, try again. \n");
        }
        printf("Great, you guessed the correct number %d in %d guesses. \n", secnum, num_guesses);
        score = 17 - num_guesses;
        if (score < 0)
            score = 0;
        else if (score > 10)
            score = 10;
        else ;
    
    
    }
    
    
     /*Pre-Conditions: This function takes in two integers, a and b.
      *Post-Conditions: This function returns the larger of a and b.
      */
    int max(int a, int b) {
        if (a > b)
            return a;
        else
            return b;
    }
    
    
     /*Pre-Conditions: This function takes in two integers, a and b.
      *Post-Conditions: This function returns the smaller of a and b.
      */
    int min(int a, int b) {
        if (a < b)
            return a;
        else
            return b;
    
    
    }
    If you could help me out with this program I would really appreciate it!

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You just need to call the functions based on which case you want.

    case1 is play arithmetic game, so call arithgame function, so on so forth with case 2.

    one thing I did notice was this piece of code

    Code:
    printf("What is %d + %d? \n", 1+rand()%max,1+rand()%max);//This rand() is not going to be the same number as the rand below it in the if statement
    if(ans== (1+rand()%max) + (1+rand()%max)){
                printf("Correct! Great job! \n");
                score++;
            }
    You should store a 1+rand()%max into a variable or else the program will not know how to determine the sum of the two rands.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    13
    Alright I did what you said but all of the answers became random. Is there a way to fix that? Here's my new code:
    Code:
     
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    //prototypes
    int arithGame(int max, int op);
    int guessGame();
    int max(int a, int b);
    int min(int a, int b);
    
    
    //mian function
    int main() {
        int choice, score, op, max;
        //seed random number generator.
        srand(time(0));
    
    
    
    
        printf("Please make a selection from the following: \n");
        printf("1. Play Arithmetic Game. \n");
        printf("2. Play Guessing Game. \n");
        printf("3. Print Score. \n");
        printf("4. Quit. \n");
        scanf("%d", &choice);
    
    
        switch (choice) {
    
    
        case 1: printf("Would you like, 1)Addition or 2)Multiplication? \n");
        scanf("%d", &op);
        printf("What is the maximum number you would like? \n");
        scanf("%d", &max);
        score = arithGame(max, op);
    
    
        }
    
    
    
    
    
    
        return 0;
    }
    
    
    /*Pre-Conditions: This function takes in two integers, max and op.
     	 	This function gives the user 10 arithmetic questions, where
    each operand ranges from 1 to max, inclusive. The value of
    operator dictates whether the problems are addition or
    multiplication problems.
    Namely, if op is 1, they are addition problems, otherwise,
    they are multiplication problems.
    	  *Post-Conditions:  The function returns the number problems solved
    		correctly, and prints out for the user after their answer
    whether or not they got the question correct.
      	  */
    
    
    int arithGame(int max, int op) {
        int ans, score = 0, i, variable;
        if (op==1) {
        for(i=1;i<10;i++) {
            variable = 1+rand()%max;
            printf("What is %d + %d? \n", variable,1+rand()%max);
            scanf("%d", &ans);
            if(ans== (variable) + (1+rand()%max)){
                printf("Correct! Great job! \n");
                score++;
            }
            else
                printf("Sorry, that's incorrect, the answer is %d \n", (variable) + (1+rand()%max));
        }
        printf("Your score for the round is %d \n", score);
    }
    else {
        for(i=1;i<10;i++) {
            printf("What is %d * %d? \n", variable,1+rand()%max);
            scanf("%d", &ans);
            if(ans== (variable) * (1+rand()%max)){
                printf("Correct! Great job! \n");
                score++;
            }
            else
                printf("Sorry, that's incorrect, the answer is %d \n", (variable) * (1+rand()%max));
        }
        printf("Your score for the round is %d \n", score);
    }
    return score;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    /*Pre-Conditions: This function does not take in any parameters.
    	This function allows the user to play the guessing game
    	where the randomly generated number lies in between 1
    	and 100, inclusive.
      *Post-Conditions:  The value returned is the score of the user in the game.
    This score is 17 minus the number of guesses unless this
    value is lower than 0 or greater than 10.
    In these cases, 0 and 10 are returned, respectively.
      */
    
    
    int guessGame() {
     int secnum, guess, num_guesses, score;
     secnum = rand()%100+1;
     while (guess != secnum) {
        printf("Enter the guess! \n");
        scanf("%d", &guess);
        num_guesses++;
        if (max(secnum, guess) == guess)
            printf("Your guess is too high, try again. \n");
        else if (min(secnum, guess) == guess)
            printf("Your guess is too low, try again. \n");
        }
        printf("Great, you guessed the correct number %d in %d guesses. \n", secnum, num_guesses);
        score = 17 - num_guesses;
        if (score < 0)
            score = 0;
        else if (score > 10)
            score = 10;
        else ;
    
    
    }
    
    
     /*Pre-Conditions: This function takes in two integers, a and b.
      *Post-Conditions: This function returns the larger of a and b.
      */
    int max(int a, int b) {
        if (a > b)
            return a;
        else
            return b;
    }
    
    
     /*Pre-Conditions: This function takes in two integers, a and b.
      *Post-Conditions: This function returns the smaller of a and b.
      */
    int min(int a, int b) {
        if (a < b)
            return a;
        else
            return b;
    
    
    }

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You still need a variable2 for the second rand call you have.

    Then in your if statement you will compare like so

    Code:
    if(ans== (variable + variable2))

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    13
    Ohhh, okay that makes sense! Alright now that I've fixed that up I got arithGame working perfectly but there's a tiny little bug with guessGame that I don't know how to stop. When you guess the right number there isn't supposed to show a "you guessed too high" or "you guessed too low" message, the only message it's supposed to respond with is the congratulatory message but when I run the program it does, along with the congratulatory message. Do you know how I can fix that?

  6. #6
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    EDIT: lol I just realized that When you guess the correct answer guess and secnum are going to be the same thing so no matter which one is returned it will always evaluate to true.

    Take a piece of paper and pencil and walk through it slowly. It's kind of hard to help you without giving you the answer on how to fix this problem. The best advice I can give you is to walk through it step for step. Maybe even print out the secnum before you enter your while loop so you can get a grasp on whether or not you are too high or too low on guesses.
    Last edited by camel-man; 11-09-2013 at 10:14 PM.

  7. #7
    Registered User
    Join Date
    Sep 2013
    Posts
    13
    Alright I'll try working through it like you said. The last thing I need help with is that for some reason case 3 never prints the right score. Here's my newest code in case you need to look through it.
    Code:
     
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    //prototypes
    int arithGame(int max, int op);
    int guessGame();
    int max(int a, int b);
    int min(int a, int b);
    
    
    //mian function
    int main() {
        int choice, score, op, max;
        //seed random number generator.
        srand(time(0));
    
    
    
    
        printf("Please make a selection from the following: \n");
        printf("1. Play Arithmetic Game. \n");
        printf("2. Play Guessing Game. \n");
        printf("3. Print Score. \n");
        printf("4. Quit. \n");
        scanf("%d", &choice);
        while (choice != 4) {
        switch (choice) {
        case 1: printf("Would you like, 1)Addition or 2)Multiplication? \n");
        scanf("%d", &op);
        printf("What is the maximum number you would like? \n");
        scanf("%d", &max);
        score = arithGame(max, op);
        break;
    
    
        case 2: score = guessGame();
        break;
    
    
        case 3: printf("Your score is %d. \n", score);
        break;
    
    
        default: printf("\n\n Thank you for playing!");
        break;
        }
        printf("Please make a selection from the following: \n");
        printf("1. Play Arithmetic Game. \n");
        printf("2. Play Guessing Game. \n");
        printf("3. Print Score. \n");
        printf("4. Quit. \n");
        scanf("%d", &choice);
    
    
        }
        return 0;
    }
    
    
    /*Pre-Conditions: This function takes in two integers, max and op.
              This function gives the user 10 arithmetic questions, where
    each operand ranges from 1 to max, inclusive. The value of
    operator dictates whether the problems are addition or
    multiplication problems.
    Namely, if op is 1, they are addition problems, otherwise,
    they are multiplication problems.
          *Post-Conditions:  The function returns the number problems solved
            correctly, and prints out for the user after their answer
    whether or not they got the question correct.
            */
    
    
    int arithGame(int max, int op) {
        int ans, score = 0, i, variable, variable2;
        if (op==1) {
        for(i=1;i<=10;i++) {
            variable = 1+rand()%max;
            variable2 = 1+rand()%max;
            printf("What is %d + %d? \n", variable,variable2);
            scanf("%d", &ans);
            if(ans== (variable) + (variable2)){
                printf("Correct! Great job! \n");
                score++;
            }
            else
                printf("Sorry, that's incorrect, the answer is %d \n", (variable) + (variable2));
        }
        printf("Your score for the round is %d. \n\n", score);
    }
    else {
        for(i=1;i<10;i++) {
            variable = 1+rand()%max;
            variable2 = 1+rand()%max;
            printf("What is %d * %d? \n", variable,variable2);
            scanf("%d", &ans);
            if(ans== (variable) * (variable2)){
                printf("Correct! Great job! \n");
                score++;
            }
            else
                printf("Sorry, that's incorrect, the answer is %d \n", (variable) * (variable2));
        }
        printf("Your score for the round is %d. \n\n", score);
    }
    return score;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    /*Pre-Conditions: This function does not take in any parameters.
        This function allows the user to play the guessing game
        where the randomly generated number lies in between 1
        and 100, inclusive.
      *Post-Conditions:  The value returned is the score of the user in the game.
    This score is 17 minus the number of guesses unless this
    value is lower than 0 or greater than 10.
    In these cases, 0 and 10 are returned, respectively.
      */
    
    
    int guessGame() {
     int secnum, guess, num_guesses = 0, score;
     secnum = rand()%100+1;
     while (guess != secnum) {
        printf("Enter the guess! \n");
        scanf("%d", &guess);
        num_guesses++;
        if (max(guess, secnum) == guess)
            printf("Your guess is too high, try again. \n");
        else if (min(guess, secnum) == guess)
            printf("Your guess is too low, try again. \n");
        else;
        }
        printf("Great, you guessed the correct number %d in %d guesses. \n", secnum, num_guesses);
        score = 17 - num_guesses;
        if (score < 0)
            score = 0;
        else if (score > 10)
            score = 10;
        else ;
        printf("Your score for the round is %d. \n\n", score);
    
    
    }
    
    
     /*Pre-Conditions: This function takes in two integers, a and b.
      *Post-Conditions: This function returns the larger of a and b.
      */
    int max(int a, int b) {
        if (a > b)
            return a;
        else
            return b;
    }
    
    
     /*Pre-Conditions: This function takes in two integers, a and b.
      *Post-Conditions: This function returns the smaller of a and b.
      */
    int min(int a, int b) {
        if (a < b)
            return a;
        else
            return b;
    
    
    }
    Last edited by zaihed13; 11-09-2013 at 10:37 PM. Reason: Question was answered

  8. #8
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Your guess game function never actually returns score. I don't know if score is supposed to be cumulative or not, but if so, then you want to call it like this in main

    Code:
    score=0;//Dont forget to initialize score to 0
    
    score += arithGame(max, op);
    score += guessGame();
    This is short hand of saying
    Code:
    score = score + guessGame();// just adding to previous score.

  9. #9
    Registered User
    Join Date
    Sep 2013
    Posts
    13
    Oh wow I completely forgot to do that! No wonder score kept on coming out wrong. I was able to fix it and now my program runs perfectly, thanks so much for all your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How does this program work??
    By Prashanth Sn in forum C Programming
    Replies: 10
    Last Post: 08-28-2012, 05:45 AM
  2. My c++ program does not work
    By vlan in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2011, 09:30 AM
  3. Can not get this program to work?
    By BLG in forum C Programming
    Replies: 9
    Last Post: 09-09-2009, 09:28 AM
  4. Why won't my program work?
    By Unregistered in forum C Programming
    Replies: 15
    Last Post: 01-11-2002, 12:57 PM
  5. how does this program work
    By ssjnamek in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2002, 01:48 PM

Tags for this Thread