Hi there I have 2 programs I need help with for my class. One is a random number guessing game with a range between 1 and 100 with the range changing each time and never widening. The problem I'm having is that each time I restart the game it generates the same random number.
The other one is a math tutor program and I think I got it except I need help with the end where its telling the user how many questions they answered correct out of whatever many they answered and the percent correct. I think I need to use an accumulator like correct++ or something but I get lost how to do it after that..
#include <iostream>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int main() {
int random;
int num;
int max;
int min;
int outofrange;
int rightguess;
srand(time(NULL));
random = rand() %100;
rightguess = 1;
outofrange = 1;
max = 100;
min = 1;
bool repeat = true;
while (repeat) {
cout << "Welcome to the game Hi-Lo..\n";
cout << "Enter an integer that is between 1 and 100: " << endl;
cin >> num;
while (num != random) {
if (num > random) {
cout << num << " is too high" << endl;
max = num - 1;
} else {
cout << num << " is too low" << endl;
min = num + 1;
}
cout << "Enter an integer that is between " << min+num << " and " << max << endl;
cin >> num;
if (num > max || num < min) {
cout
<< "the integer you enter is not within range, enter a number between "
<< min << " and " << max << endl;
cin >> num;
outofrange = outofrange + 1;
} else
rightguess = rightguess + 1;
}
cout << " It took you " << rightguess
<< " valid guesses to find the number " << endl;
cout << " You had " << outofrange << " out of range guesses " << endl;
cout << "Do you want to play again? [y/n]" << endl;
char answer;
cin >> answer;
repeat = answer == 'y';
}
cout << "Thank you for playing Hi-Lo. Bye!" << endl;
return 0;
}
I tried putting srand in the while loop like you said and its still giving me the same error. I would run it once, get a random number. Run twice get the same random number from the 1st time..
case 5: // The user chose to quit the program.
cout << "Thank you for using Math Tutor.\n\n";
break;
}
// If student selected a problem, get and evaluate the answer.
if (choice >= 1 && choice <= 4) {
cin >> useranswer;
if (useranswer == result)
cout << "\n\nCongratulations! That's right.\n\n";
else
cout << "\n\nSorry, the correct answer is " << result
<< ".\n\n";
}
} while (choice != 5); // Loop again if student did not choose to quit.
return 0;
}
Nothing displays after the user enters 5 except the thank you message. I need an output that will tell the user how many questions they answered, how many they got right and the percentage correct..except when I try to use a counter it doesnt do anything..
I tried putting srand in the while loop like you said and its still giving me the same error. I would run it once, get a random number. Run twice get the same random number from the 1st time..
The srand() function should only be called once, usually early in main() outside of any loop.
Nothing displays after the user enters 5 except the thank you message. I need an output that will tell the user how many questions they answered, how many they got right and the percentage correct..except when I try to use a counter it doesnt do anything..
while (repeat) {
random = rand() % 100;
cout << "Welcome to the game Hi-Lo..\n";
cout << "Enter an integer that is between 1 and 100: " << endl;
cin >> num;
while (num != random) {
if (num > random) {
cout << num << " is too high" << endl;
max = num - 1;
} else {
cout << num << " is too low" << endl;
min = num + 1;
}
cout << "Enter an integer that is between " << min + num << " and "
<< max << endl;
cin >> num;
if (num > max || num < min) {
cout
<< "the integer you enter is not within range, enter a number between "
<< min << " and " << max << endl;
cin >> num;
outofrange = outofrange + 1;
} else
rightguess = rightguess + 1;
}
cout << " It took you " << rightguess
<< " valid guesses to find the number " << endl;
cout << " You had " << outofrange << " out of range guesses " << endl;
cout << "Do you want to play again? [y/n]" << endl;
char answer;
cin >> answer;
repeat = answer == 'y';
}
cout << "Thank you for playing Hi-Lo. Bye!" << endl;
return 0;
}
Where are you trying to display this information?
At the end of the program, when the user enters 5 as an input. Something like this should be displayed:
Terminating Math Tutor...
__________________________________
You worked on 6 problems.
1 addition problems
2 subtraction problems
1 multiplication problems
2 division problems
You got 4 problems correct!
Your percent correct is: 66.67%
Thank you for using Math Tutor.
But it only displays the Thank you message...I don't know how to use counters or where to put them in my code..
Update your addition counter inside the case statement.
On line 138 before you return you need to display the results.
Ok so I have something like this now in each case..
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Produce a problem.
switch (choice) {
case 1: // Addition problem
// Generate two random numbers in
// the range 1 - 500.
num1 = 1 + rand() % 500;
num2 = 1 + rand() % 500;
// Calculate the correct answer.
result = num1 + num2;
if(result==useranswer)
{
correct+=1;
}
and defined correct as an int and set it to 1. How do I add up the total correct at the end? It just displays 1 problem correct
Wouldn't it make more sense to increment the "counter" when you tell the user the answer is correct?
So are you saying I need an if statement in line 33 for the counter?
Also when I am running my number guess game for the 2nd time the range is off..
Do you want to play again? [y/n]
y
Welcome to the game Hi-Lo..
Enter an integer that is between 1 and 100:
56
56 is too low
Enter an integer that is between 57 and 63
bool repeat = true;
while (repeat) {
cout << "Welcome to the game Hi-Lo..\n";
cout << "Enter an integer that is between 1 and 100: " << endl;
cin >> num;
while (num != random) {
if (num > random) {
cout << num << " is too high" << endl;
max = num - 1;
} else {
cout << num << " is too low" << endl;
min = num + 1;
}
cout << "Enter an integer that is between " << min+num << " and " << max << endl;
cin >> num;
if (num > max || num < min) {
cout
<< "the integer you enter is not within range, enter a number between "
<< min << " and " << max << endl;
cin >> num;
outofrange = outofrange + 1;
} else
rightguess = rightguess + 1;
}
cout << " It took you " << rightguess
<< " valid guesses to find the number " << endl;
cout << " You had " << outofrange << " out of range guesses " << endl;
cout << "Do you want to play again? [y/n]" << endl;
char answer;
cin >> answer;
repeat = answer == 'y';
}
cout << "Thank you for playing Hi-Lo. Bye!" << endl;
return 0;
}
You already have an if() statement, why not just use that statement? By the way the "counter" I'm talking about is your variable "correct".
I'm confused...
1 2 3 4 5 6 7 8 9
// If student selected a problem, get and evaluate the answer.
if (choice >= 1 && choice <= 4) {
cin >> useranswer;
if (useranswer == result)
cout << "\n\nCongratulations! That's right.\n\n";
else
cout << "\n\nSorry, the correct answer is " << result
<< ".\n\n";
Are you saying put correct++ After the congrats message? If I do that I get a syntax error.. Or do I put it with the if statement that has useranswer and result? Do I need to initialize correct to 1 or 0?
1 2 3 4 5 6 7 8
if (useranswer == result) //right here?
cout << "\n\nCongratulations! That's right.\n\n";
//or right here? I get a syntax error here?
else
cout << "\n\nSorry, the correct answer is " << result
<< ".\n\n";
Do realize that without the "optional" braces a control statement will only have a one line body, any other lines are outside the if() statement, and in this case that means that the "else" has no if() statement?
This is why it is usually recommend to always use braces with all control statements, even when not technically required, at least until you are much more familiar with the language.
Do realize that without the "optional" braces a control statement will only have a one line body, any other lines are outside the if() statement, and in this case that means that the "else" has no if() statement?
This is why it is usually recommend to always use braces with all control statements, even when not technically required, at least until you are much more familiar with the language.
1 2 3 4 5 6 7 8
if (useranswer == result) //right here I put a {
cout << "\n\nCongratulations! That's right.\n\n";
//and another one here }
else //but what about this? Do I need a { here?
cout << "\n\nSorry, the correct answer is " << result
<< ".\n\n";
edit: tried this, didnt do anything..
1 2 3 4 5 6 7 8 9 10 11 12 13
// If student selected a problem, get and evaluate the answer.
if (choice >= 1 && choice <= 4) {
cin >> useranswer;
if (useranswer == result) {
if (useranswer == result && result == correct)
correct++;
cout << "\n\nCongratulations! That's right.\n\n";
}
else
cout << "\n\nSorry, the correct answer is " << result
<< ".\n\n";