So I am making the program little by little to eventually make it more complex. What I am having trouble with is the logic behind my while loop for looping the number of questions the user wants, and using random numbers for each question.
The problem: I have tried to think about other solutions for this, but when you enter the selection screen and make your selection, it passes the while loop because it cannot find the correct selection and it's function call.
I want to perhaps put the while loop in a function on it's own and just call that, and somehow bring all the functions together for the correct sequence.
What I really am looking for is opinions and ways to code this while loop, and see what other ways there are, so if I see a problem I will change if necessary or if there is another route.
> but when you enter the selection screen and make your selection,
> it passes the while loop because it cannot find the correct selection and it's function call.
What does getSelection() return?
A good way to figure out what is going wrong is to just print out the values of interest. For instance:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
displayMenu();
selection = getSelection();
std::cout << "selection: " << selection << '\n' ; // *****
cout << "How many questions would you like? " << endl;
cin >> numberOfQuestions;
std::cout << "numberOfQuestions: " << numberOfQuestions<< '\n' ; // *****
// Initialize random seed
srand( time(NULL) );
std::cout << "count just before the while loop: " << count << '\n' ; // *****
while (count <= numberOfQuestions) {
// etc.
So getSelection() function returns the user's selection and validates it, if it is < than 1 or > than 5, then bring about an error and have the user select again.
1 2 3 4 5 6 7 8 9 10 11 12 13
int getSelection() {
int selection;
cin >> selection;
while ((selection < 1) || (selection > 5)) {
cout << "!!Incorrect selection!!\nPlease select from the Math Quiz menu: " << endl;
cin >> selection;
}
return selection;
}
And you are absolutely right about testing for correct input values and making sure it works before the while loop, that is why I test myself each step of the way and I did that. What I did was take out the if statements of the while loop and put it before the while loop, worked fine, but not inside the while loop. So I must have to do something with that loop or some how change how I will count each problem and generate 2 random numbers each time while maintaining the structured flow in main().
Thanks for opening my mind JLBorges. I thought about a For loop, and never implemented it so of course I didn't think it would work because I never tried. Defeats the purpose of trying different things and being creative with programming.
Now I do have one more question, and it involves the division section. I'm doing random numbers a bit different with that so the numerator is not so huge of a number and won't fit into the denominator. How would I make sure that both random numbers are divisible by each other? I know it involves the modulus, but I just don't want it to be even numbers the whole way through, because 3 can go into 6 and 9, 9 into 18 and 27. I won't be dealing with floating point, just integers at this moment.