Math Quiz - Functions - Help please!

The object of the program is to generate two random multiplicands and compute the answer. The user selects the range of numbers to be multiplied as the difficulty and then answers as many questions the user wishes until "quit" is typed into the input parameter.

The use of functions is what is expected for this program, and my problem is getting my PrintScore function to work properly in the code. I need it to be called once the user types quit to exit the loop:

example:

Difficulty: 8
Question Response Cumul. Score
========= ======== ========--======
3*5=15 15 1/1 (100%)
8*2=16 16 2/2 (100%)
6*9=54 45 2/3 (66%)




Thank you in advanced for your help!




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
106
107
#include <iostream>
#include <string>
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */
using namespace std;

// Function Prototypes 
int NewRandomNumber(int n);
void MakeQuestion(int n, int& a, int& b, int& atimesb);
void PrintScore(int numCorrect, int numAsked, string userName, int setDiff,int a, int b, string toto);
bool UserAnswer(int a, int b, int atimesb, int input);

int main()
{
	string userName;
	string toto;
	int setDiff;
	int atimesb;
	int numCorrect=0;
	int numAsked=0;
	int a, b;
	int input = atoi(toto.c_str());
	
		
	/* initialize random seed: */
	srand(time(NULL));


	cout << "Enter Username: ";
	cin >> userName;
	cout << "" << endl;
	cout << "Enter Difficulty (1-10): ";
	cin >> setDiff;
	cout << "" << endl;


	while (toto != "quit" && toto != "Quit")
	{


	NewRandomNumber(setDiff);
	MakeQuestion(setDiff,a,b,atimesb);
	
	
		if (UserAnswer(a,b,atimesb,input))
	
	{
		numCorrect++;
		numAsked++;
	}
	else
	{
		numAsked++;
	}

		PrintScore(numCorrect,numAsked,userName,setDiff,a,b,toto);

	}
	
	//PrintScore(numCorrect,numAsked,userName,setDiff,a,b,toto);

	return 0;
}


int NewRandomNumber(int n)
{
	n = rand() %n + 1;
	return n;
}

void MakeQuestion(int n, int& a, int& b, int& atimesb)
{
	a = NewRandomNumber(n);
	b = NewRandomNumber(n);
	atimesb = a*b;
	return;
}

void PrintScore(int numCorrect, int numAsked, string userName, int setDiff, int a, int b, string toto)
{
	cout << "" << endl;
	cout << "Quiz Results:" << endl;
	cout << "" << endl;
	cout << "Username: " << userName << endl;
	cout << "Difficulty: " << setDiff << endl;
	cout << "" << endl;
	cout << " Question  " << "  Response     " <<  " Cumul." << "Score" << endl;
	cout << "==========  ==========  ========--======" << endl;
	cout << "  "<<a<<"x"<<b<<"        "<<toto<<"            "<<numCorrect<<"/"<<numAsked<<"   " <<(numCorrect/numAsked)*100<<"%";
	cout << "" << endl;
	//return; 
}

bool UserAnswer(int a, int b, int atimesb,int input)
{
	
	cout << "" << endl;
	cout << "Question: " << a << "x" << b << " = ";
	cin >> input;


	if (input == atimesb)
		return true;
	else
		return false;
}
Last edited on
I keep getting an infinite loop if I un-comment the function call after the while loop. I cannot get the program to execute the function and print out the quiz results.
Last edited on
Okay, first of all, you need a set time to ask the question of whether the user wants to quit. Like say after ten questions, the program asks the user: "Do you want to stop?". The user would enter yes or no, if it was no, then keep going, if it was yes, THEN you end your while loop with your uncommented PrintScore function(PS remove the one in the loop if you do this). If you go this route, you will need a counter for to keep track of the number of questions, or you could use the numAsked....

Those are my suggestions, hope it helps!
Topic archived. No new replies allowed.