samedi 26 octobre 2019

How can I fix my random generators used to generate factors and messages using function?

I have to write a program to simulate the Education System by dividing the program into functions (requirements are below). I am trying to use rand() to generate random numbers from 0 to 9 and also to generate random messages to display but both are not working. There might be a problem in my switch statement or function definition and prototype or use of rand() and srand(). I can't figure it out please help.

These are the instructions I was given by my instructor:

Step 1: Computers are playing an increasing role in education. Write a program that will help an elementary school student learn multiplication. Use a Random number function to produce two positive one-digit integers. The program should then prompt the user with a question, such as

How much is 6 times 7?

The student then inputs the answer. Next, the program checks the student’s answer. If it is correct, display the message "Very good!" and ask another multiplication question. If the answer is wrong, display the message "No. Please try again." and let the student try the same question repeatedly until the student finally gets it right.

A separate function should be used to generate each new question. This function should be called once when the application begins execution and each time the user answers the question correctly.

Step 2: Modify the program to vary the comments that are displayed for each correct answer and each incorrect answer.

The response to a correct answer should be chosen from: Very good! Excellent! Nice work! Keep up the good work!

The response to an incorrect answer should be chosen from: No. Please try again. Wrong. Try once more. Don't give up! No. Keep trying.

Use random-number generation to choose a number from 1 to 4 that will be used to select an appropriate response to each answer. Use a switch statement to issue the responses.

Step 3: Modify the program to count the number of correct and incorrect responses typed by the student. After the student types 10 answers, your program should calculate the percentage of correct responses.

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

//Function declaration
void getInput(int& userAnswer);
int randomFactors(int a, int b, int randomNumbmer1, int randomNumber2);
int multiply(int correctAnswer, int randomNumber1, int randomNumber2);
int randomRight(int right1, int right2, int randomMessege);
int randomWrong(int wrong1, int wrong2, int randomMessege);

// Main Program
int main()
{
    //Variable declaration
    int correctAnswer = 0,
        userAnswer = 0;
    int randomMessege = 0;
    int right1 = 1,
        right2 = 4;
    int wrong1 = 1,
        wrong2 = 4;
    int cCounter = 0,
        iCounter = 0;

    //While loop counts the number of times user gives an answer
    int count = 0; //control variable for while loop 
    while (count < 10)
    {
        //do while loop for the program to exit when user enters "-1" 
        do
        {
            int a = 0,
                b = 9,
                randomNumber1 = 0,
                randomNumber2 = 0;
            //Function call
            randomFactors(a, b, randomNumber1, randomNumber2);
            multiply(correctAnswer, randomNumber1, randomNumber2);
            getInput(userAnswer);

            /*if else to check if the answer is correct and
            to give another chance if the user is wrong*/
            //icounter and cCounter keep track of the number of correct and incorrect answers

            while(userAnswer != correctAnswer)
            {
                randomWrong(wrong1, wrong2, randomMessege);
                getInput(userAnswer);
                iCounter = iCounter + 1;
            }
            randomRight(right1, right2, randomMessege);
            cCounter = cCounter + 1;
        } while (userAnswer > -1); //end of do while loop

        if (count == 10)
        {
            int percentage = (cCounter / 10) * 100;
            if (percentage >= 75)
            {
                cout << "You have scored " << percentage << "%. Congratulations, you are ready to go to the next level!";
            }
            else
            {
                cout << "You have scored " << percentage << "%. Please ask your instructor for extra help.";
            }
            count, iCounter, cCounter = 0;
        }
        count++;
    }
}

//Function defination
int randomFactors(int a, int b, int randomNumber1, int randomNumber2)
{
    srand(time(0));
    randomNumber1 = rand() % (b + 1) + a;
    randomNumber2 = rand() % (b + 1) + a;
    cout << "How much is " << randomNumber1 << " times " << randomNumber2 << "?\n";
    return randomNumber1, randomNumber2;
}
void getInput(int& userAnswer)
{
    cout << "Enter your answer (-1 to exit):\n";
    cin >> userAnswer;
}
int multiply(int correctAnswer, int randomNumber1, int randomNumber2)
{
    correctAnswer = randomNumber1 * randomNumber2;
    return correctAnswer;
}
int randomRight(int right1, int right2, int randomMessege)
{
    srand(time(0));
    randomMessege = (rand() % (right2 + 1) + right1);
    switch (randomMessege)
    {
    case 1: cout << "Very good!";

    case 2: cout << "Excellent!";

    case 3: cout << "Nice work!";

    case 4: cout << "Keep up the good work!";
    }
    cout << endl << endl;
    return randomMessege;
}
int randomWrong(int wrong1, int wrong2, int randomMessege)
{
    srand(time(0));
    randomMessege = (rand() % (wrong2 + 1) + wrong1);
    switch (randomMessege)
    {
        case 1: cout << "No. Please try again.";

        case 2: cout << "Wrong. Try once more.";

        case 3: cout << "Don't give up!";

        case 4: cout << "No. Keep trying.";
    }
    cout << endl << endl;
    return randomMessege;
}```[![Second picture is of the output I am getting and first is the expected output.[![\]\[1\]][1]][1]][2]


  [1]: https://i.stack.imgur.com/nItE5.png
  [2]: https://i.stack.imgur.com/r6Cv0.png



Aucun commentaire:

Enregistrer un commentaire