computer assisted instruction

Hello,
I am having trouble with my code. I am making a computer assistance program that outputs multiplication problems. At this point, I want the user to type 'y' when he correctly answers the question to move on to the next question. My code is working but it is not outputting the right info on the console window. It doesn't put out responses like "good job" after the first question and sometimes the questions are duplicated. How can I fix this so that the user can only press 'y' when the question is correct?
thank you

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include<iostream>
#include<cstdlib>
#include<xloctime>
using namespace std;
void correctAnswer(int num);
void incorrectAnswer(int num);

char newQuestion(char yes);
int question()
{
	srand(time(0));
	int n1 = rand() % 10;
	int n2 = rand() % 10;
	cout << "what is " << n1 << " * " << n2 << " ? " << endl;
	return n1*n2;

}
int main()
{
	char done = 'y';
	int posResponse = rand() % 4 + 1;
	int negRespinse = rand() % 4 + 1;
	int answer = question();
	int userAnswer;
	cin >> userAnswer;
	if (userAnswer == answer)
	{

		correctAnswer(userAnswer);
		
	}
	else if (userAnswer != answer)
	{
		incorrectAnswer(userAnswer);
		
	}
	while (userAnswer == answer)
	{
		cout << "type 'y' for another question" << endl;
		cin >> done;
		newQuestion(done);
	

	}
	
}
void incorrectAnswer(int num)
{


	{
		switch (rand() % 4 + 1)
		{
		case 1:
			cout << "No. Please try again." << endl;
			break;
		case 2:
			cout << "Wrong.Try once more." << endl;
			break;
		case 3:
			cout << " Don't give up!" << endl;
			break;
		case 4:
			cout << "No.Keep trying" << endl;
			break;


		}
	}


}
void correctAnswer(int num)
{
	{
		switch (rand() % 4 + 1)
		{
		case 1:
			cout << "Very good!" << endl;
			break;
		case 2:
			cout << "Excellent!" << endl;
			break;
		case 3:
			cout << "Nice work!" << endl;
			break;
		case 4:
			cout << "Keep up the good work!" << endl;
			break;

		}

	}

}
char newQuestion(char yes)
{
	srand(time(0));
	int n1 = rand() % 10;
	int n2 = rand() % 10;
	cout << "what is " << n1 << " * " << n2 << " ? " << endl;

	return n1*n2;

}
still confused
are you compiling on a mac?
yes
closed account (48T7M4Gy)
The reason why the message only displays once is because the incorrectAnswer call at line 34 etc is outside the while loop.
so I have to put the correctAnswer call and the incorrectAnswer call in the while loop?
yes
Line 11, 98: Your calls to srand() are in the wrong place. srand() should be called ONCE at the beginning of main. Multiple calls to srand() cause the random number generator to be reset to produce the same sequence of numbers if called within the same second.

can you show me an example of what I should do? I tried putting both function calls in the while loop, but I am still not getting the correct output.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{   char done = 'y';
	int answer; 
	int userAnswer;
	
	srand(time(0));  // Call ONCE at the beginning of main
	do
	{   answer = question();
	    cin >> userAnswer;
	    if (userAnswer == answer)
	        correctAnswer();
        else 
            incorrectAnswer();
        cout << "type 'y' for another question" << endl;
		cin >> done;		
	}
	while (done == 'y');
	return 0;
}


What's the point of newQuestion()? It does exactly the same thing as question().

Last edited on
I have to create a function to create a new question for my program for my projects. BTW thanks for all the help guys!. Much appreciated
closed account (48T7M4Gy)
I have to create a function to create a new question for my program for my projects.
The way I would tackle this is to use your current question() function as a model, rename it questionMultiply() and cut/copy/paste/modify a new one called questionAdd() as an example. From there the world is at your feet. :)
Last edited on
Topic archived. No new replies allowed.