I need to write a program that generates a random question ten times. The program then has to be organized using function calls, as well as, keep track of the amount of right and wrong answers.
My issue is that when I run the program the "for()" loop doesn't even run. It simply displays the title and my name and then promptly ends the program. I made the mistake of foolheartedly writing the program in one go and tried to find out what was causing the issue but I was stumped.
I hope the code is easy to read and thank you to anyone who takes the time to look through it. (Though I'm sure there have been people who've posted hundreds of lines of code before...)
The code itself is run on Visual Studio 2019.
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <cassert>
usingnamespace std;
void randwrong() {
int number;
number = rand() % 4;
if (number == 0) {
cout << "No. Please try again.\n";
}
if (number == 1) {
cout << "Wrong. Try once more.\n";
}
if (number == 2) {
cout << "Don't give up!\n";
}
if (number == 3) {
cout << "No. Keep trying.\n";
}
}
void randcorrect() {
int number;
number = rand() % 4;
if (number == 0) {
cout << "Very good!\n";
}
if (number == 1) {
cout << "Excellent!\n";
}
if (number == 2) {
cout << "Nice work!\n";
}
if (number == 3) {
cout << "Keep up the good work!\n";
}
}
int randomQuestion(int correctanswer) {
int number = rand() % 10;
if (number == 0) {
cout << "What is 6 * 5? ";
correctanswer = 30;
}
if (number == 1) {
cout << "What is 7 * 3? ";
correctanswer = 21;
}
if (number == 2) {
cout << "What is 2 * 5? ";
correctanswer = 10;
}
if (number == 3) {
cout << "What is 9 * 4? ";
correctanswer = 36;
}
if (number == 4) {
cout << "What is 5 * 3? ";
correctanswer = 15;
}
if (number == 5) {
cout << "What is 3 * 11? ";
correctanswer = 33;
}
if (number == 6) {
cout << "What is 4 * 6? ";
correctanswer = 24;
}
if (number == 7) {
cout << "What is 6 * 8? ";
correctanswer = 48;
}
if (number == 8) {
cout << "What is 7 * 9? ";
correctanswer = 63;
}
if (number == 9) {
cout << "What is 3 * 4? ";
correctanswer = 12;
}
return correctanswer;
}
int main()
{
cout << " Mulitplication Table Practice\n";
cout << " by MY NAME\n\n";
int rightanswer{}, answer;
double correctcount = 0, percentage;
for (int total = 0; total > 10; total++) {
cout << "ENTER -1 TO QUIT\n";
randomQuestion(rightanswer);
cin >> answer;
if (answer == rightanswer) {
randcorrect();
correctcount++;
}
elseif (answer == -1) {
return 0;
}
else {
randwrong();
}
}
}
I then have a few things to calculate and display at the end like the percentage grade and the closing statements but they weren't apart of the issue so I didn't include them.
My issue is that when I run the program the "for()" loop doesn't even run
for (int total = 0; total > 10; total++)
Your for loop is never entered because the looping condition is never true.
You meant total < 10;
Other issues:
- You never use the value returned from your randomQuestion function.
- in your randomQuestion function, you shouldn't have any input parameters, because you overwrite the value anyway. Just declare correctanswer as local int variable within the function, not as a parameter.
1 2 3 4 5 6
int randomQuestion()
{
int correctanswer = 0;
// ...
return correctanswer;
}
Thank you again for helping me Ganado. (Both times I've posted you've answered my question)
I was wondering if you could clarify what you meant in your second half about the return value.
This is where I use the returned value and when the function call is edited to your specification I get the same error of never having the right answer. Even when I use the original code I had written I find that the program is struggling to have a correct answer to compare to.
1 2 3 4 5
if (answer == correctanswer) {
randcorrect();
correctcount++;
}
when I ask the program to display what it uses as the correct answer I get 0.
correcvtanswer is pased by value - so any changes done to it within the functioin aren't passed back to the calling function. There's no need to have a parameter here.
1 2 3 4
int randomQuestion() {
int correctanswer {};
.....
}
Then in main:
1 2 3
cout << "ENTER -1 TO QUIT\n";
randomQuestion(rightanswer);
cin >> answer;