I am giving the user a choice. He inputs the difficulty level he wants to attempt and the code acts appropriately. I don't know how to go about this. I can prompt for the choice and store it. I don't know how to implement it.
Currently, I have my code set up to randomly generate 2 numbers in between 1 and 9. That is the easyQuestions. The hardQuestions will be randomly generated numbers from 1-99. The user will have to multiply those.
I only have the first part, where there is no choice and I don't know how to implement one.
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <ctime>
#include <cmath>
#include <fstream>
usingnamespace std;
int main(int argc, constchar * argv[])
{
//upon recieving a desired level program shall take the student to funciton 1 or function 2
double correctAnswer = 0;
double incorrectAnswer = 0;
double percentCorrect;
int level;
cout << "What difficulty level would you like to try?\n" ;
cout << "Enter 1 for easy problems or 2 for harder problems.\n";
cin >> level;
for(int counter = 1; counter<=5; counter++)
{
srand( (unsigned)time(0) );//randomize the randomizer with time as the seed
double x = rand() % 9 +1;//randomly generated number between 1 and 10
double y = rand() % 9 +1;//randomly generated number between 1 and 10
double product = x * y;//the correct answer
double answer;//student's answer input
cout << "What is " << x << " times " << y << "?" << endl;//prompt user for mulitplication answer to randomly generated problem
cin >> answer;//users answer to be checked and compared with computer's product
if (answer == product)//checking to see correctness of user's answer
{
++correctAnswer;
int goodStatement = rand() % 3 +1;
switch (goodStatement)
{
case 1:
puts("Very good!");
break;
case 2:
puts("Excellent");
break;
case 3:
puts("Nice work!");
break;
case 4:
puts("Keep up the good work");
break;
default:
puts("Default good");
break;
}
}
elsewhile (answer!= product)
{//while loop shall run with the same x and y values being presented. The program will not move forward until a correct answer is entered by user.
++incorrectAnswer;
int badStatement = rand() % 3 +1;
switch (badStatement)
{
case 1:
puts("No. Please try again");
break;
case 2:
puts("Wrong. Try once more");
break;
case 3:
puts("Don't give up");
break;
case 4:
puts("No. Keep trying");
break;
default:
puts("This is the default no");
break;
}
cout << "What is " << x << " times " << y << "?" << endl;
cin >> answer;
if (answer == product)//checking to see correctness of user's answer
{
++correctAnswer;
int goodStatement = rand() % 3 +1;
switch (goodStatement)
{
case 1:
puts("Very good!");
break;
case 2:
puts("Excellent");
break;
case 3:
puts("Nice work!");
break;
case 4:
puts("Keep up the good work");
break;
default:
puts("Default good");
break;
}
}
}
}
cout << "You got " << correctAnswer <<" of them correct. Great work!" << endl;
cout << "You missed " << incorrectAnswer << "." << endl;
percentCorrect = (correctAnswer / correctAnswer + incorrectAnswer)*100;
cout << percentCorrect << (setprecision(2)) << "%" << endl;
if (percentCorrect < .75) {
cout << "Please ask you teacher for extra help." << endl;
} else {
cout << "Congratulations, you are ready to go the next level!" << endl;
}
}
Your answer was so much simpler than what I was envisioning. I had an inkling towards an if...else statement, but I didn't even know how to implement that.
int level, rand_x, rand_y; // Added 2 variables to hold the limits of random numbers x and y
cout << "What difficulty level would you like to try?\n" ;
do
{
cout << "Enter 1 for easy problems or 2 for harder problems.\n";
cin >> level;
if (level <1||level>2)
cout << "Sorry. At the moment, there are only 2 levels to choose from.." << endl << endl;
// Above informs user that there are only the 2 levels at present
} while (level <1 || level >2); // Only allows a 1 or 2, to be entered
if (level == 1)
{
rand_x = 9;
rand_y = 9;
}
else
{
rand_x = 99;
rand_y = 99;
}
for(int counter = 1; counter<=5; counter++)
// The rest of your is here...