Alexis Nonya Alexis Nonya - 13 days ago 4
C++ Question

Program that's supposed to generate 2 random numbers between 1 and 9 in C++ keeps generating just 6 and 9 and isn't looping correctly

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";
}
}
}

Answer

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...