Thread: Arithemtic and guessing game program not working right

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    29

    Arithemtic and guessing game program not working right

    I am relatively new to C programming, and I am encountering numerous issues with this program that I cant seem to figure out how to fix.

    First, when the user selects the arithmetic game, I keep on getting different incorrect answers from the arithgame function. For example, when I am presented with the question 3+2=_, sometimes the function claims the answer is the first number, 3, and other times the function gives me a multiplication answer, 6. This happens in both the addition and multiplication parts (ie. the multiplication answer will either be the first number or the addition answer). I cant figure out what I am doing wrong in this function to cause such a problem.
    Additionally, I cant figure out why my guessing game loops forever, rather than letting me guess until I get a correct answer.


    I could use all the help I can get. Thank you
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It would be easier in the future to post your code here using code tags.

    In all of your functions, you seem to be using "while" in many cases where it appears you mean "if". This would explain why some of your menu choices lead to infinite loops.

    Furthermore, your "arithgame" function seems confused (apart from the "while" loops). You appear to want it to receive one int to represent a number, and another int to represent the operation. But oftentimes, you're calling it with two numbers. And in the function itself, you're using the value of operation in your calculations.

    I suspect you want this function to receive three integers: first number, second number, and operation.

    That should be enough to get you started.

    (P.S. It's a long shot, but if your username was inspired by The Court Jester, I take my hat off to you.)

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    In the future, please post your code in [code][/code] tags, so we can read it without needing to download it. Like so:
    Code:
    /* Header Comment
     * These should include your name, the date,
     * and the title and purpose of your program
     */
    
    
    //included libraries
    #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);
    //main function
    
    
    int main(){
    
    
        //seed random number generator
        srand(time(0));
    
    
    //list all variables used in the program
    int choice1, i, op, randnum1, randnum2, answer1, a=0, b=0, max, min, randnum3, guess, counter, score=0, score2=0;
    
    
        //give user menu to choose from
        printf("\nWhat would you like to do?\n");
        printf("\t1 - Play Arithmetic Game\n");
        printf("\t2 - Play Guessing Game\n");
        printf("\t3 - Print Score\n");
        printf("\t4 - Quit\n");
        scanf("%d", &choice1);
    
    
        while (choice1 != 4){
    
    
        switch (choice1){
        case 1:{
    
    
            //let user choose which arithmetic game
            printf("\t1 - Addition\n");
            printf("\t2 - Multiplication\n");
            scanf("%d", &op);
    
    
        while (op == 1){
            printf("\nWhat is the maximum value you would like?\n");
            scanf("%d", &a);
    
    
        //create a for loop give the user 10 different problems to solve
        for (i=1; i<=10; i++){
    
    
        //save random number and set its parameters between 1 and the number the user gave
        randnum1 = rand() % a + 1;
        randnum2 = rand() % a + 1;
    
    
            //give user addition problem with 2 random numbers to solve
            printf("\n%d + %d = ", randnum1, randnum2);
                scanf("%d", &answer1);
    
    
            //if user answers correctly, add a point to their overall score
            if (answer1 == arithgame(randnum1,randnum2)){
                printf("\n Congradulations, you are correct!\n");
                score += 1;}
    
    
            else
                printf("Sorry, the correct answer was %d", arithgame(randnum1,randnum2));
        }
            //once loop is done, bring user back to menu
            break;
        }
    
    
        while (op == 2){
    
    
        //create random intergers between 1 and 100
        randnum1 = rand() % 100 + 1;
        randnum2 = rand() % 100 + 1;
    
    
            //give user mulitplication problem to solve
            printf("\n %d * %d = ", randnum1, randnum2);
            scanf("%d", &answer1);
    
    
            //if user answers correctly, add 1 point to their score
            if (answer1 != arithgame(randnum1,randnum2))
                printf("\nSorry, the correct answer was %d\n", arithgame(randnum1,randnum2));
    
    
            else{
                printf("\n Congradulations, you are correct!\n");
                score += 1;
                }}
            break;
            }
    
    
        case 2:{
    
    
            //have the user guess a random number between 1 & 100
            //loop keeps repeating until answer is found
            randnum3 = rand() % 100 + 1;
            printf("\nEnter your guess?\n");
            scanf("%d",&guess);
    
    
            while (guess > randnum3){
                printf("\nYour guess is too high, try again.\n");
                counter += 1;
    
    
            }
            while (guess < randnum3){
                printf("\nYour guess is too low, try again.\n");
                counter += 1;
    
    
            }
            while (guess == randnum3) {
                printf("\nGreat, you guessed the correct number %d in %d guesses", randnum3, counter);
                //to calculate score, subtract the number of guesses from 17
                //if the number is greater than or equal to 10, give the user 10 points
                //if the number is less than or equal to 10, give the user 0 points
                //if the number is between 1 & 10, give the user this amount of points
                if (a<7)
                    score2 += 10;
                if (a > 17)
                    score2 += 0;
                else
                    score2 = a;
                printf("\nYour score for the round is %d", score2);
                break;
            }
            }
    
    
        case 3:
            //give user their overall score from both arithmetic games and guessing game
            score += score2;
            printf("\nYour score is %d\n", score);
            break;
    
    
        default:
            printf("\nYou did not make a valid selection\n");
            break;
        }
    
    
        //give user menu to choose from
        printf("\nWhat would you like to do?\n");
        printf("\t1 - Play Arithmetic Game\n");
        printf("\t2 - Play Guessing Game\n");
        printf("\t3 - Print Score\n");
        printf("\t4 - Quit\n");
        scanf("%d", &choice1);
    
    
        }
    return 0;
    }
    
    
    //arithgame function
    int arithgame(int max, int op)
    {
    
    
        while(op == 1)
        return max + op;
        while(op == 2)
        return max*op;
    }
    
    
    //guessing game function
    int guessgame()
    {
    
    
    }
    
    
    //max value function
    int max(int a, int b)
    {
        while (a > b)
        return a;
        while (b > a)
        return b;
    
    
    }
    
    
    //min value function
    int min(int a, int b)
    {
        while (a < b)
        return a;
        while (b < a)
        return b;
    }
    Make sure you compile at the maximum warning level, and fix them all:
    Code:
    $ make game
    gcc -Wall -ggdb3 -pedantic -std=c99 -O0 -o game game.c -lm -lpthread -lrt
    game.c: In function ‘main’:
    game.c:24:65: warning: unused variable ‘min’ [-Wunused-variable]
    game.c:24:60: warning: unused variable ‘max’ [-Wunused-variable]
    game.c:24:55: warning: unused variable ‘b’ [-Wunused-variable]
    game.c: In function ‘min’:
    game.c:183:1: warning: control reaches end of non-void function [-Wreturn-type]
    game.c: In function ‘max’:
    game.c:174:1: warning: control reaches end of non-void function [-Wreturn-type]
    game.c: In function ‘guessgame’:
    game.c:164:1: warning: control reaches end of non-void function [-Wreturn-type]
    game.c: In function ‘arithgame’:
    game.c:158:1: warning: control reaches end of non-void function [-Wreturn-type]
    Unused variables are not that big a deal, but the others are. Note, a while is a loop for repeating. A while loop that runs once, and has a break statement at the end that always executes is just pointless. Consider if/else if you don't actually want to repeat.

    Also, note that listing explicitly a < b and a > b ignores the case of a == b, which is why you get the error. If a == b, your code never returns a value, which means it exhibits undefined behavior, and anything can happen (in reality, it probably appears as though your functions return garbage values).
    Code:
    if (a < b)
        return a;
    else
        return b;  // if they are equal, they are both the min, so returning a or b is ok
    Now, on to your real question:
    > First, when the user selects the arithmetic game, I keep on getting different incorrect answers from the arithgame function.
    Fixing the above will almost certainly help with this, but you have a bigger problem:
    Code:
    // call the arithgame function passing in the two numbers to add/mult
    arithgame(randnum1,randnum2)
    // define the arith game function to take two params, one operand and one operator
    int arithgame(int max, int op)
    Notice that how you define and how you call your function are different. You must define the function to take the right number and type of parameters (and return the proper type), and you must make sure to call it with the right number and type of parameters in the right order, and handle the return value correctly. You need to pass in both operands (random numbers) and the operator. Not some bizarre "max" parameter.

    EDIT: There is almost certianly plenty more wrong, but you have some pretty fundamental issues to clean up here, so fix all the issues I mentioned first, then post back if you are still stuck, and we'll help you fix your program some more.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do i program a guessing game?
    By Silsilay in forum C++ Programming
    Replies: 10
    Last Post: 04-02-2012, 11:15 AM
  2. how do i program a guessing game?
    By Silsilay in forum General AI Programming
    Replies: 3
    Last Post: 03-28-2012, 10:46 AM
  3. how do i program a guessing game?
    By Silsilay in forum Windows Programming
    Replies: 5
    Last Post: 03-28-2012, 08:15 AM
  4. help with guessing game program
    By dc0n in forum C++ Programming
    Replies: 13
    Last Post: 02-07-2006, 08:15 PM
  5. need help with a guessing game program
    By davidnj732 in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2003, 11:59 AM