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.

functions, how? if...else, how?

I am going to include my code.

-Josh
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150`` ``````#include #include #include #include #include #include #include using namespace std; int main(int argc, const char * 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; } } else while (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; } }``````
Hint: What are you really asking?

If easy questions are random numbers from 1-9 and harder questions are 1-99... what is the real difference in your program?

Xen
Hi Xen,

I am asking, if the user choses difficulty 1 how do I only run that code? If the user chooses difficulty 2 how do I only run that code?

Lines 46-48 define variables x and y with random numbers of 1-9. How can I make that a choice (difficulty 1)?

-Josh
 ``1234567891011`` ``````if (choice == 1) // if user wants easy questions { a = rand() % 9 + 1; b = rand() % 9 + 1; } else if (choice == 2) // if user wants harder questions { a = rand() % 99 + 1; b = radn() % 99 + 1; }``````

@TheJJJunk

That worked, thank you.

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.

Thanks for the push.

-Josh
Instead of putting TheJJJunks code so that it must read the if's each time through the counter loop, a better way would be to create 2 new variables.

 ``123456789101112131415161718192021222324`` `````` 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... ``````
Topic archived. No new replies allowed.