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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <vector>
using namespace std;
//the Menu
string Menu(){
string chose;
cout << "Hello this is a c++ quizer pleas enter :\nHelp-for a description\nDiff- for chosing a difficulty level " << endl;
cin >> chose;
if(chose!="Help" && chose!="Diff")
while(chose!="Help" && chose!="Diff"){
cout << "\n\nYou did not enter a valid comand. Enter Help or Diff" << endl;
cin.clear();
cin.ignore(1000,'\n');
cin >> chose;
}
if(chose=="Help"){
cout << "\n\nYou will be presented with a quiz of arithmetic problems. Each ''play'' of the quiz will be 10 questions.\n"
<< "You can chose betwean 3 difficulty levels \n"
<< "Easy-single digit numbers\nModearte-double digit numbers\nAdvanced- 4-digit numbers" << endl;
cout << "\n\nHelp-for a description\nDiff- for chosing a difficulty level " << endl;
getline(cin, chose);
}
if(chose=="Diff"){
cout << "Enter a difficult level : Easy, Moderate or Advanced " << endl;
cin >> chose;
if(chose!="Easy" && chose!="Moderate" && chose!="Advanced")
while(chose!="Easy" && chose!="Moderate" && chose!="Advanced"){
cout << "\nYou did not enter a valid comand. Enter : Easy, Moderate or Advanced" << endl;
cin.clear();
cin.ignore(1000,'\n');
cin >> chose;
}
}
return chose;
}
//a function that returns true or false depending on have some par of numbers appeared alredy
bool beenhere(int num1,int num2,vector<int> v1, vector<int>v2){
for(int i=0; i<v1.size(); i++){
if(v1[i]==num1 && v2[i]==num2){
return true;
}
}
return false;
}
//function wich generates numbers betwen min and max
int Getnumb(int min,int max){
int numb(-1);
while(numb<min || numb>max)
numb=rand();
return numb;
}
//this function gives a random operator "-" or "+"
char Getoperator(){
char operato=1;
while(operato!=43 && operato!=45)
operato=rand();
return operato;
}
//finds the correct answer
int c_answer(int num1, int num2, char operato){
int c_answer(0);;
if(operato==43)
c_answer=num1+num2;
else if(operato==45)
c_answer=num1-(num2);
//cout << "\n\nTacan odgovor je : \n\n" << c_answer;
//return c_answer;
}
//is returning true or false depending on the given answer is right or wrong
bool CorrectAnswer(int c_answerr, int answer){
if(c_answerr==answer)
return true;
return false;
}
//a function wich displays the "question"
int Display(int num1,int num2, char operato, int c_answerr ){
int answer;
for(int i=1; i<=2; i++){
if(i==2)
cout << "Incorrect answer try again" << endl;
cout << num1 << operato << num2 << "= ";
cin >> answer;
if(CorrectAnswer(c_answerr,answer)==true)
break;
}
return answer;
}
//gives the info about the answer being right or wrong
void Display1(bool trueOrfalse, int &correct,int &incorrect){
if(trueOrfalse==true){
cout << "Your answer is correct" << endl;
correct++;
}
else if(trueOrfalse==false){
cout << "That answer is incorrect" << endl;
incorrect++;
}
}
//display gives the player info aboput how many answers he had right
void DisplayEnd(int correct,int incorrect){
cout << "\nYou had " << correct << " correct answers and " << incorrect << " incorrect answers\n"
<< "\nYou had " << correct*100/(correct+incorrect) << " % of correct answers\n" << endl;
}
int main(){
string diff("a");
do{
if(diff=="Exit")
break;
diff=Menu();
int num1,num2;
int min(0), max(9999);
if (diff=="Moderate"){
min=0; max=99;
}
else if(diff=="Easy"){
min=0;
max=9;
}
vector<int> v1, v2;
int correct(0), incorrect(0);
for(int i=1; i<=10; i++){
num1=Getnumb(min,max); num2=Getnumb(min,max);
if(beenhere(num1,num2,v1,v2)==true && i>1)
while(beenhere(num1,num2,v1,v2)==true){
num1=Getnumb(min,max); num2=Getnumb(min,max);
};
v1.push_back(num1);
v2.push_back(num2);
char operato(Getoperator());
int c_answerr(c_answer(num1,num2,operato));
int answer(Display(num1,num2,operato,c_answerr) );
bool trueOrfalse(false);
if(c_answerr==answer)
trueOrfalse=true;
Display1(trueOrfalse,correct,incorrect);
}
DisplayEnd(correct,incorrect);
cout << "\nThis is the end, if You want to play again type Again\n for exit type Exit\n ";
cin >> diff;
}while(diff!="Exit");
return 0;
}
|