-2

This program is supposed to take generate 2 random numbers between 1 and 9 and present them to the user asking them what the answer is. The purpose is to help students learn the multiplication table. However when ever the program initializes, it constantly ONLY generates the numbers 6 and 9 to be multiplied. Also the program is supposed to check every 10 entries to see if the user has a wrong rate of 75% and if not it proceeds on to another set of 10 questions or stops if it is 75%. I'm not sure how to get it to go terminate if it meets the termination requirements, and continue if it doesn't. Any advice? (ALso I did look up the answer for a question similar to this here C++ random number 1-9, but I met with the same 6 and 9 scenario)

void Miscellaneous::multiplication(){//This function will generate two random single digit numbers and ask the user to say what their product is.
int num1 = (rand()% 9) + 1;
int num2 = (rand()% 9) + 1;
int ans,response = rand()% 4 + 1;
int totalans = 0, correctans = 0, wrongans = 0;


while (totalans != 10){//This will make the function repeat 10 times, collecting 10 answers.

        cout<<"What is the product of "<< num1 << " and "<<num2<<"?\n";
        cin>>ans;
        if (ans != num1 * num2){
            wrongans += 1;
            switch(response){//One of the following responses will be chosen at random and be displayed to the user when the answer is wrong
            case 1 :
                cout<<"No. Please Try Again.\n";

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

            case 3 :
                cout<<"Don’t give up!\n";

            case 4 :
                cout<<"No.  Keep trying.\n";
            }

        }

        else if (ans == num1 * num2){//One of the following responses will be displayed to the user when they get an answer correct
            correctans += 1;
            switch(response){
            case 1 :
                cout<<"Very good!\n";

            case 2 :
                cout<<"Excellent!\n";

            case 3 :
                cout<<"Nice Work!\n";

            case 4 :
                cout<<"Keep up the good work!\n";
            }
        }
    }
    totalans = correctans + wrongans;//This will keep track of the total of wrong and correct answers the user has already given
    if (totalans == 10){
        if (wrongans == 10 * 75/100){//This will determine if the user got 75% of the questions wrong in order to terminate the program
            cout<<"Ask your Instructor for help\n";
        }
    }
}

Edit: Thanks to all of you who helped me with this. it had me stumped for a bit.I wish I could give you all the "correct answer" thingy.

Community
  • 1
  • 1

3 Answers3

1

You need to seed your random number generator by calling

srand(static_cast<unsigned>(time(NULL)));

PS: do not forget to #include <ctime> to call srand() with the right parameter.

Community
  • 1
  • 1
Varaquilex
  • 3,447
  • 7
  • 40
  • 60
1

You are only assigning values to num1, num2 and for that matter response once. You need to assign new values inside the loop, otherwise the same values will be used for each iteration.

Instead of this:

int num1 = (rand()% 9) + 1;
int num2 = (rand()% 9) + 1;
int ans,response = rand()% 4 + 1;
int totalans = 0, correctans = 0, wrongans = 0;


while (totalans != 10){//This will make the function repeat 10 times, collecting 10 answers.

        cout<<"What is the product of "<< num1 << " and "<<num2<<"?\n";
        cin>>ans;
        if (ans != num1 * num2){
            // More code removed...

Try this:

int ans;
int totalans = 0, correctans = 0, wrongans = 0;


while (totalans != 10){//This will make the function repeat 10 times, collecting 10 answers.

        int num1 = (rand()% 9) + 1;
        int num2 = (rand()% 9) + 1;
        int response = rand()% 4 + 1;

        cout<<"What is the product of "<< num1 << " and "<<num2<<"?\n";
        cin>>ans;
        if (ans != num1 * num2){
            // More code removed...
clstrfsck
  • 14,715
  • 4
  • 44
  • 59
  • ah okay, so I switched it, however now the rand for response gives me all 4 responses when it should only be one. is it because of the random as well? or did i just make a logical error there? – Alexis Nonya Nov 23 '16 at 01:56
  • You need a `break` statement after each `cout << "Something\n"`, otherwise the case statement will fall through to the next statement. – clstrfsck Nov 23 '16 at 01:57
  • oh I see. Thanks you. also um, if you don't mind, how would be a best way to rephrase the condition way at hte bottom to check if the amount of wrong answers is 75% or more than the amount of correct answer, thus stopping the program? – Alexis Nonya Nov 23 '16 at 02:01
  • I think if you move the statement `totalans = correctans + wrongans` inside the `while` loop then your current logic should work. – clstrfsck Nov 23 '16 at 02:06
  • oh I has it now thank you. The program doesn't terminate, but it does give me the " ask your instructor for help". So if I'm not mistaken, placing a break; clause at the end of that totalans = correctans + wrongans loop will give me the desired effect correct? – Alexis Nonya Nov 23 '16 at 02:10
  • Okay, so I jsut added the break statement, and it creates the desired effect of ending the loop. how would I make it continue? – Alexis Nonya Nov 23 '16 at 02:14
0

You have no "break" after each of your switch statements. That is why it's calling all cases.

 if (ans != num1 * num2)
 {
        wrongans += 1;
        switch(response)
        {
            case 1 :
                cout<<"No. Please Try Again.\n";
                break;
            case 2 :
                cout<<"Wrong.  Try once more.\n";
                break;
            case 3 :
                cout<<"Don’t give up!\n";
                break;
            case 4 :
                cout<<"No.  Keep trying.\n";
                break;
        }

    }

A better way to check percentage of answers:

totalAnswers = correctAnswers + wrongAnswers;
float rightAnswerPercentage = (correctAnswers/totalAnswers) * 100;
if (rightAnswerPercentage < 75)
{
    cout << "You suck!";
}
else cout << "You rock!";
Just Jake
  • 66
  • 2
  • how would I make the program continue as if normal when the 10 tries are met and the correct answer is more than 25%? So far I can stop it when it is 75% wrong, but I'm at a loss to make it continue on as if normal. – Alexis Nonya Nov 23 '16 at 02:18