how to solve rand issue and if-else code not running?

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// question:
/*
The use of computers in education is referred to as computer‐assisted instruction (CAI).
Write a program that will help an elementary school student learn multiplication.
Use the rand function to produce two positive one‐digit integers.
The program should then prompt the user with a question, such as "How much is 6 times 7?"
The student then inputs the answer. Next, the program checks the student’s answer.

If it’s correct, display something like "Very good!" and ask another multiplication question.
If the answer is wrong, display a message e.g. "No. Please try again." and let the student try
the same question repeatedly until the student finally gets it right.

A separate function should be used to generate each new question. This function should be called
once when the application begins execution and each time the user answers the question correctly.
*/

#include <iostream>
#include <cstdlib>

using namespace std;

int PosInt1 = rand() % 9;
int PosInt2 = rand() % 9;
int result, guessResult;

void Multiplication()
{

	if (guessResult == result)
	{
		cout << "Very good! Let's try another problem. " << endl;
		return 0;
		Multiplication();
	}
	else
	{
		cout << "No, that's incorrect. Please check your answer and try again. " << endl;
		cout << "Answer: ";
		cin >> guessResult;
		cout << endl;
	}

}

int main()
{
	result = PosInt1 * PosInt2;

	while (PosInt1 > 0 && PosInt2 > 0)
	{
		cout << "Learn Multiplication Program!" << endl;
		cout << "How much is" << " " << PosInt1 << " " << "times" << " " << PosInt2 << "?" << endl;
		cout << "Answer: ";
		cin >> guessResult;
		cout << endl;
	}

	Multiplication();

	system("pause");
}





I can't figure what I'm doing wrong; when I try running the code, the first thing I notice is that the while loops works, but the if-else section does not. The last part of the question says to create a new function for every new multiplication problem, which I'm not sure if I've done correctly (couldn't I use void?); I was wondering if that could be part of the problem? Also, I'm not sure how to run the code so that it generates different random numbers every time.

Last edited on

So after searching up online what I might be doing wrong and what I can improve in my code, I made some changes to the code:

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
//edited code:

int PosInt1 = rand() % 9;
int PosInt2 = rand() % 9;
int result, guessResult;

int main()
{
	result = PosInt1 * PosInt2;

	while (PosInt1 > 0 && PosInt2 > 0)
	{
		cout << "Learn Multiplication Program!" << endl;
		cout << "How much is" << " " << PosInt1 << " " << "times" << " " << PosInt2 << "?" << endl;
		cout << "Answer: ";
		cin >> guessResult;
		cout << endl;

		if (guessResult == result)
		{
			cout << "Very good! Let's try another problem. " << endl;
			cout << endl;
			continue;
		}
		else
		{
			cout << "No, that's incorrect. Please check your answer and try again. " << endl;
			cout << "Answer: ";
			cin >> guessResult;
			cout << endl;
			continue;
		}
	}

	
	system("pause");
}


So it's a lot better than what I previously had, but how do I display the statements EACH time the user enters a number, and not just every other time? Is it the continue function? Also, still stuck on the rand issue...(generate new random numbers, not the same ones for each multiplication problem)

Last edited on
By the way you need srand(seed) in the main function in order for rand() to work. So put srand(time(0)); or srand((unsigned)(time(0)));, if you're allergic to mismatch warnings like me, in main function. Look up how rand() works it'll only take 5 minutes, I promise.

And to make a single digit number you use rand() % 10 but do note that it's possible to get 0. So if you don't want 0 you can do rand() % 9 +1 instead.

Your structure isn't quite right. Regardless of whether the person gets the question right or wrong, the program will go into the next iteration of the while loop which is to ask the same question again (not because of continue). continue here is obsolete, continue is only used to skip the remaining statements in the same iteration and force the program to go to the next iteration of the loop.


In your first snippet, return 0; in the function forces the function to return, and because of that Multiplication(); is never called.

Consider something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void multiplication() {
-> Declare input_result, to store input
-> Initialize No#1 and No#2 with the rand function.
// You can declare a variable to store the correct result if you're particular

do{
-->Display Question
-->Take input answer

if(input_result == No#1*No#2)
 // Display message
 // You can use a break here, then you wouldn't have to use a condition for the while loop

else
  // Display message
  // The while loop goes to the next iteration and asks the same question

} while(input_result != No#1*No#2); 
// Or input_result != result if you have declared a variable to hold the proper result
// You can use a while loop instead of a do-while but then initialize input_result

}// end of function 


If you want another question then run the function again.
Last edited on
Your random values are initialised just once, since they're global variables.

Your first code was better, in the sense that it had a function.

Don't try to write everything at once. You can add useful bits of functionality 5 lines at a time.

For example, you might start with this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void quizMultiply( int x, int y ) {
    int correct = x * y;
    int response;
    cout << "What is " << x << " times " << y << "?" << endl;
    cin >> response;
    if ( response != correct )
        cout << "Wrong" << endl;
    else
        cout << "Correct" << endl;
}

int main ( ) {
    quizMultiply(rand()%10,rand()%10);
}


In two further iterations, you put a while loop in main (to run more quizzes), and you put a while loop in the function (to allow more guesses at a given question).

So you might end up with something like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void quizMultiply( int x, int y ) {
    int correct = x * y;
    int response;
    do {
        cout << "What is " << x << " times " << y << "?" << endl;
        cin >> response;
        if ( response != correct )
            cout << "Wrong" << endl;
        else
            cout << "Correct" << endl;
    } while ( response != correct );
}

int main ( ) {
    char yorn;
    do {
        quizMultiply(rand()%10,rand()%10);
        cout << "Do you want another go(Y/N)?" << endl;
        cin >> yorn;
    } while ( yorn == 'Y' || yorn == 'y' );
}


Say you then wanted to add a limit to how many times they could guess.
You figure that changing quizMultiply is the thing to do, but that might make the function messy.

So first extract the actual Q/A from the function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool multTest(int x, int y) {
    int correct = x * y;
    cout << "What is " << x << " times " << y << "?" << endl;
    int response;
    cin >> response;
    if ( response != correct )
        cout << "Wrong" << endl;
    else
        cout << "Correct" << endl;
    return response == correct;
}

void quizMultiply( int x, int y ) {
    bool correct;
    do {
        correct = multTest(x,y);
    } while ( !correct );
}

Notice that there was no actual change to what the code does at this stage.

A lot of coding is just cleaning things up to make your life easier in the long run. Sure you could pile everything into main, but it would be nested 6 levels deep, 100's of lines long and a complete PITA to change.


Then it's easy to add in a counter to the now much simplified quizMultiply function.
1
2
3
4
5
6
7
8
9
10
11
void quizMultiply( int x, int y ) {
    int guesses = 0;
    bool correct;
    do {
        correct = multTest(x,y);
        if ( !correct )
            guesses++;
    } while ( guesses < 3 && !correct );
    if ( !correct )
        cout << "Better luck next time" << endl;
}

A separate function should be used to generate each new question. This function should be called once when the application begins execution and each time the user answers the question correctly.

Hmmm.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdlib>

int question()
{
  int foo = 6;
  int bar = 7;
  std::cout << "How much is " << foo << " times " << bar << "?\n";
  return foo * bar;
}

int main()
{
  std::cout << "Learn Multiplication Program!\n";
  do
  {
    int correct = question(); // generate new question
    int guess = 0;

    sdt::cin >> guess; // how to repeat this until guess==correct?

  } while ( std::cin );
}
Last edited on
Topic archived. No new replies allowed.