mercredi 23 décembre 2020

Random Math Problem Generator: userAnswer and realAnswer keep returning 0

This is a random math problem generator program.

The problem is my input answer and the real answer keeps returning 0.

What's the problem here? I can't find out why.

Here's the code step by step...

import libraries

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

calling functions

int getRandNum(int i, int lower, int upper, int randNum);
int getRandOp(int i, int opSel, char randOp);
int getRealAnswer(char randOp, int randNum1, int randNum2, int realAnswer);
void showQuestion(int i, int randNum1, int randOp, int randNum2, int realAnswer, int 
userAnswer);
int getUserAnswer(int userAnswer);
void answerCompare(int realAnswer, int userAnswer);

main() function

int main(void) // main fucntion
{
    int i;
    int randNum1, randNum2;
    int userAnswer, realAnswer;
    char randOp;
    srand(time(NULL));

    for (i = 1; i <= 5; i++)
    {
        showQuestion(i, randNum1, randOp, randNum2, realAnswer, userAnswer);
        getUserAnswer(userAnswer);
        getRealAnswer(randNum1, randNum2, randOp, realAnswer);
        answerCompare(realAnswer, userAnswer);
        printf("Real Answer = %d User Answer = %d\n\n", realAnswer, userAnswer);
    }

    return 0;
}

getRandNum() function

int getRandNum(int i, int lower, int upper, int randNum) // get random number within range using rand() function
 {
    lower = (20 * (i - 1)) + 1;
    upper = 20 * I;
    randNum = (rand() % (upper - lower + 1)) + lower;

    return randNum;
}

getRandOp() function

int getRandOp(int i, int opSel, char randOp) // get random operator within list using rand() function
{
    char opList[4] = {'+', '-', '*', '/'};
    opSel = rand() % 4;
    randOp = opList[opSel];

    return randOp;
}

getRealAnser() function

int getRealAnswer(char randOp, int randNum1, int randNum2, int realAnswer) // get real answer of the problem (problematic part: always returns 0)
{
    switch (randOp)
    {
    case '+':
        realAnswer = randNum1 + randNum2;
    case '-':
        realAnswer = randNum1 - randNum2;
    case '*':
        realAnswer = randNum1 * randNum2;
    case '/':
        realAnswer = randNum1 / randNum2;
    default:
        break;
    }

    return realAnswer;
}

showQuestion() function

void showQuestion(int i, int randNum1, int randOp, int randNum2, int realAnswer, int userAnswer) // print out math question

{
    int randNum, opSel, lower, upper;

    printf("##### Question Number %d #####\n", I);
    randNum1 = getRandNum(i, lower, upper, randNum);
    randOp = getRandOp(i, opSel, randOp);
    randNum2 = getRandNum(i, lower, upper, randNum);
    realAnswer = getRealAnswer(randNum1, randNum2, randOp, realAnswer);
    printf("%d %c %d = ", randNum1, randOp, randNum2);
}

getUserAnswer() function

int getUserAnswer(int userAnswer) // user input answer of the problem (problematic part: always returns 0)
{
    userAnswer = -1;
    userAnswer = scanf("%d", &userAnswer);

    return userAnswer;
}

answerCompare() function

void answerCompare(int realAnswer, int userAnswer) // compare user answer and real answer of the problem and print result
{
    if (userAnswer == realAnswer)
    {
        printf("You are correct!\n");
    }
    else if (userAnswer != realAnswer)
    {
        printf("You are wrong!\n");
    }
    else
    {
        printf("Error! Invalid Comparison!\n");
    }
}



Aucun commentaire:

Enregistrer un commentaire