computer assisted instruction

Nov 16, 2015 at 2:31am
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;

}
Nov 16, 2015 at 8:51pm
still confused
Nov 16, 2015 at 8:58pm
are you compiling on a mac?
Nov 16, 2015 at 9:03pm
yes
Nov 16, 2015 at 9:08pm
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.
Nov 16, 2015 at 9:17pm
so I have to put the correctAnswer call and the incorrectAnswer call in the while loop?
Nov 16, 2015 at 9:19pm
yes
Nov 16, 2015 at 9:24pm
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.

Nov 16, 2015 at 9:32pm
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.
Nov 16, 2015 at 9:57pm
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 Nov 16, 2015 at 9:58pm
Nov 17, 2015 at 4:17am
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
Nov 17, 2015 at 5:25am
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 Nov 17, 2015 at 5:48am
Topic archived. No new replies allowed.