Math Quiz program

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
do {
		if (count > 1)
			clearScreen();

		displayMenu();
		selection = getSelection();

		cout << "How many questions would you like? " << endl;
		cin >> numberOfQuestions;

		// Initialize random seed
		srand( time(NULL) );
		

		while (count <= numberOfQuestions) {
			num1 = rand() % 80 + 1;
			num2 = rand() % 80 + 1;

			// call swap function if num2 is bigger
			if (num1 < num2)
				swap(num1, num2);

		        // Depending on selection, do that Math problem
		        if (selection == 1)
				addition(num1, num2); 
			else if (selection == 2) 
				subtraction(num1, num2);
			else if (selection == 3) 
				multiplication(num1, num2);
			else if (selection == 4) 
				division(num1, num2);

		// Increase count
		count++;

		} // While loop for number of questions
	} while (selection != 5);


Not the entire program, just the piece that is causing me to think a little. If you need more let me know I will provide it. Thank you.
Last edited on
> 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().
This is one way of writing the do-while:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int main()
{
    std::srand( time(0) ) ; // seed the rng just once
    
    int selection = 0 ;

    do
    {
        displayMenu();
	selection = getSelection();
        if( selection == 5 ) break ; // **** 

		int numberOfQuestions;
		std::cin >> numberOfQuestions;
   
		for( int count = 0 ; count < numberOfQuestions ; ++count )
		{
                        int num1 = rand() % 80 + 1;
			int num2 = rand() % 80 + 1;
			if (num1 < num2) std::swap( num1, num2 ) ;

		        if (selection == 1) /* ... */ ;
		        // etc..
	        }
    } while( selection != 5 ) ;
}

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.

Thanks again!
Something like this would be the simplest:

1
2
3
int denominator = a_random_number_in_the_range_2_to_50 ;
int multiplier = a_random_number_in_the_range_2_to_40 ;
int numerator = denominator * multiplier ;
Topic archived. No new replies allowed.