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;
}
|