/////////////////////////////////////////////////////////////////////////////////////////
//                                                                                     //
//  #   #   #   ####  ####  #   #   #   #   #   ##### #   #  ####                      //
//  #   #  # #  #   # #   # #   #   ## ##  # #    #   #   # #                          //
//  #   # #   # #   # #   # #   #   # # # #   #   #   #   # #             ##### ###    //
//  ##### #   # ####  ####   # #    #   # #   #   #   #####  ###             # #   #   //
//  #   # ##### #     #       #     #   # #####   #   #   #     #   #   #   #  #   #   //
//  #   # #   # #     #       #     #   # #   #   #   #   #     #    # #   #   #   #   //
//  #   # #   # #     #       #     #   # #   #   #   #   # ####      #   #  #  ###    //
//                                                                                     //
//                                                                                     //
//                              #                                                      //
//                              #                                                      //
//                              ####                                                   //
//                              #   # #   #                                            //
//                              #   #  # #                                             //
//                              ####    #                                              //
//                                     #                                               //
//                                    #                                                //
//                                                                                     //
//      _____            _____   _____    _____             ____   _____  _____        //
//     /     \  \     \    \          \        \  \    \   /    \     \     \          //
//     \      \  \     \    \         /       /    \    \  \  \\ \     \     \         //
//      \      \  \     \    \       /       /      \____\  \    <      \     \        //
//       \      \  \     \    \     /       /             \  \ \\  \     \     \       //
//        \___\_/   \____/    __\__ \_____  \_____   _____/   \____/    __\__   \      //
//             \                                                                       //
//                                    INDUSTRIES!!!                                    //
//                                                                                     //
/////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////
//          INCLUDES            //
//////////////////////////////////
#include <stdio.h>              // Random numbers
#include <iostream.h>           // Input and output stream
#include <iomanip.h>            // Changing decimal points 
#include <time.h>               // Timing the questions	
#include <fstream.h>            // Writing options & high scores to files 
#include <windows.h>            // Clearing the screen, changing colour
//////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////
//                              Global Variables                                    //
//////////////////////////////////////////////////////////////////////////////////////
int type;                           //  Type of question                            //
int ciog;                           //  Number of questions correct in one attempt  //
float difficulty;                   //  Difficulty Level                            //
                                    //                                              //
float avg_add, avg_sub;             //  The average time it takes for a person      //
float avg_mult, avg_div;            //  to answer the question (used for scoring)   //
                                    //                                              //
float taken=0;                      //  Time taken to complete a question           //
float final_score=0;                //  Final score at the end of the quiz          //
float total_taken=0;                //  Total time taken to complete the quiz       //
//////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////
//             COLOURS              //
//////////////////////////////////////
WORD intense=FOREGROUND_INTENSITY;  //
WORD red=FOREGROUND_RED;            //
WORD green=FOREGROUND_GREEN;        //
WORD blue=FOREGROUND_BLUE;          //
WORD orange=red|green;              //
WORD purple=red|blue;               //
WORD yellow=red|green|intense;      //
//////////////////////////////////////
WORD border1=blue;                  //
WORD border2=red|intense;           //
WORD box_c=blue|intense;            //
WORD shadow=blue;                   //
WORD text_c=green|intense;          //
WORD numbers=green|red;             //
//////////////////////////////////////

//////////////////////////////////////////////////////////////
//            A structure for use in the high               //
//              score recording system                      //
//////////////////////////////////////////////////////////////
struct _score                       //                      //
{                                   //                      //
	int rank;                       //  Player rank         //
	char name[20];                  //  Player name         //
	float score;                    //  Player high score   //
};                                  //                      //
//////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////
//                                                                                 //
//                      CHANGE COLOUR FUNCTION - colour()                          //
//                                                                                 //
/////////////////////////////////////////////////////////////////////////////////////

// This function changes the colour of the text for a certain amount of characters

void colour(DWORD code, int l, int x, int y)
{
	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD const xy ={x, y};						

	DWORD len;
	FillConsoleOutputAttribute(console, code, l, xy, & len);
	
	return;
}

void colour_whole(int code)
{
	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(console, code); 
	return;
}
/////////////////////////////////////////////////////////////////////////////////////
//                                                                                 //
//                        CLEAR SCREEN FUNCTION - clrscr()                         //
//                                                                                 //
/////////////////////////////////////////////////////////////////////////////////////

// This is a function for clearing the screen, it is an alternative to system("cls") 
// which is slower than this one

void clrscr()
{
	COORD const topleft = {0,0};
	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	DWORD len;FillConsoleOutputCharacter(console, ' ', 4000, topleft, & len);
	DWORD len2;FillConsoleOutputAttribute(console, text_c, 4000, topleft, & len2);
	SetConsoleCursorPosition(console, topleft);
//	SetConsoleTextAttribute(console, text_c); 
	return;
}

/////////////////////////////////////////////////////////////////////////////////////
//                                                                                 //
//                        CURSOR POSITIONING FUNCTION - gotoxy()                   //
//                                                                                 //
/////////////////////////////////////////////////////////////////////////////////////

// This function changes the position of the cursor on the screen

void gotoxy(int x, int y)
{
	x--;
	y--;
	COORD const topleft = {x,y};
	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(console, topleft);
	return;
}

/////////////////////////////////////////////////////////////////////////////////////
//                                                                                 //
//                        SCREEN SETTING UP FUNCTION - screensize()                //
//                                                                                 //
/////////////////////////////////////////////////////////////////////////////////////

// This function changes the size of the screen to 80 * 50 characters and changes the
// title of the console window

void screensize()
{
	COORD const size = {80,50};
	_CONSOLE_CURSOR_INFO ccinfo;
	ccinfo.dwSize = 99;
	ccinfo.bVisible = 1;
	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorInfo(console, &ccinfo);
	SetConsoleScreenBufferSize(console, size);
	SetConsoleTitle("HappyMaths v7.0 - (c) QuizzyBit Industries MMI");
	return;
}

/////////////////////////////////////////////////////////////////////////////////////
//                                                                                 //
//                        NUMBER DRAWING FUNCTION - number()                       //
//                                                                                 //
/////////////////////////////////////////////////////////////////////////////////////

// This function draws the big numbers that appear in the quiz

void number(int num,int a, int b)
{
	int x, y, m=0, n=0;
	
// Extremely large 3D Array containing the Numbers font
	int number[14][10][7]= 
// This looks complex but is just an array containg the positions of all the "pixels" in
// the big numbers, it is compressed to take up less space

{{{0,0,15,15,15,0,0},{0,15,0,0,0,15,0},{0,15,0,0,0,15,0},{15,0,0,0,15,15,15},
{15,0,0,15,15,0,15},{15,0,15,15,0,0,15},{15,15,15,0,0,0,15},{0,15,0,0,0,15,0},
{0,15,0,0,0,15,0},{0,0,15,15,15,0,0}},{{0,0,15,15,15,0,0},{0,0,0,15,0,0,0},
{0,0,0,15,0,0,0},{0,0,0,15,0,0,0},{0,0,0,15,0,0,0},{0,0,0,15,0,0,0},{0,0,0,15,0,0,0},
{0,0,0,15,0,0,0},{0,0,0,15,0,0,0},{0,0,15,15,15,0,0}},{{0,0,15,15,15,0,0},
{0,15,0,0,0,15,0},{15,0,0,0,0,0,15},{15,15,0,0,0,0,15},{0,0,0,0,0,15,0},
{0,0,0,0,15,0,0},{0,0,0,15,0,0,0},{0,0,15,0,0,0,0},{0,15,0,0,0,0,15},
{15,15,15,15,15,15,15}},{{15,15,15,15,15,15,15},{15,0,0,0,0,15,0},{0,0,0,0,0,15,0},
{0,0,0,0,15,0,0},{0,0,0,15,15,0,0},{0,15,0,0,0,15,0},{15,0,0,0,0,0,15},
{15,0,0,0,0,0,15},{0,15,0,0,0,15,0},{0,0,15,15,15,0,0}},{{0,0,0,0,15,0,0},
{0,0,0,15,15,0,0},{0,0,15,0,15,0,0},{0,0,15,0,15,0,0},{0,15,0,0,15,0,0},
{15,0,0,0,15,0,15},{15,15,15,15,15,15,15},{0,0,0,0,15,0,15},{0,0,0,0,15,0,0},
{0,0,0,15,15,15,0}},{{0,15,15,15,15,15,15},{0,15,0,0,0,0,15},{15,0,0,0,0,0,0},
{15,0,0,0,0,0,0},{15,15,15,15,15,0,0},{0,0,0,0,0,15,0},{15,15,0,0,0,0,15},
{15,0,0,0,0,0,15},{0,15,0,0,0,0,15},{0,0,15,15,15,15,0}},{{0,0,0,15,15,0,0},
{0,0,0,15,0,0,0},{0,0,15,0,0,0,0},{0,0,15,0,0,0,0},{0,15,15,15,15,0,0},
{0,15,0,0,0,15,0},{15,0,0,0,0,0,15},{15,0,0,0,0,0,15},{0,15,0,0,0,15,0},
{0,0,15,15,15,0,0}},{{15,15,15,15,15,15,15},{15,0,0,0,0,0,15},{0,0,0,0,0,15,0},
{0,0,0,0,0,15,0},{0,0,0,0,15,0,0},{0,0,0,0,15,0,0},{0,0,0,15,0,0,0},{0,0,0,15,0,0,0},
{0,0,15,0,0,0,0},{0,15,15,0,0,0,0}},{{0,0,15,15,15,0,0},{0,15,0,0,0,15,0},
{0,15,0,0,0,15,0},{0,15,0,0,0,15,0},{0,0,15,15,15,0,0},{0,15,0,0,0,15,0},
{15,0,0,0,0,0,15},{15,0,0,0,0,0,15},{0,15,0,0,0,15,0},{0,0,15,15,15,0,0}},
{{0,0,15,15,15,0,0},{0,15,0,0,0,15,0},{15,0,0,0,0,0,15},{15,0,0,0,0,0,15},
{0,15,0,0,0,15,0},{0,0,15,15,15,15,0},{0,0,0,0,15,0,0},{0,0,0,0,15,0,0},
{0,0,0,15,0,0,0},{0,0,15,15,0,0,0}},{{0,0,0,0,0,0,0},{0,0,15,15,15,0,0},
{0,0,0,15,0,0,0},{0,15,0,15,0,15,0},{0,15,15,15,15,15,0},{0,15,0,15,0,15,0},
{0,0,0,15,0,0,0},{0,0,15,15,15,0,0},{0,0,0,0,0,0,0},{0,0,0,0,0,0,0}},{{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},{0,0,0,0,0,0,0},{0,15,0,0,0,15,0},{0,15,15,15,15,15,0},
{0,15,0,0,0,15,0},{0,0,0,0,0,0,0},{0,0,0,0,0,0,0},{0,0,0,0,0,0,0},{0,0,0,0,0,0,0}},
{{0,0,0,0,0,0,0},{0,0,0,0,0,0,0},{0,0,0,0,0,0,0},{0,15,0,0,0,15,0},{0,0,15,0,15,0,0},
{0,0,0,15,0,0,0},{0,0,15,0,15,0,0},{0,15,0,0,0,15,0},{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0}},{{0,0,0,0,0,0,0},{0,0,0,0,0,0,0},{0,0,0,15,0,0,0},{0,0,0,0,0,0,0},
{0,15,15,15,15,15,0},{0,0,0,0,0,0,0},{0,0,0,15,0,0,0},{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},{0,0,0,0,0,0,0}}};


if(num>99)					// checks for a special character (+, -, x, ÷)
{
	int j;
	switch(num)
	{
	case 100:
		j=10;
		break;
	case 101:
		j=11;
		break;
	case 102:
		j=12;
		break;
	case 103:
		j=13;
		break;
	default:
		cout<<"you have a serious error!";		// if the number is not within the parameters
	}											// this should never happen
	
	// This section draws the actual numbers
	gotoxy(a,b);
	for(y=0;y<=9;y++)
	{
		gotoxy(a, b+y);
		for(x=0;x<=6;x++)
		{
			if(number[j][y][x]==15)
			{
				cout<<char(219);
				colour(numbers, 7, a-1, b+y-1);
			}
			else
			{
				cout<<char(255);
			}
		}
		cout<<endl;
	}
}
else
{
	if(num<10)
	{
		m=num;
	}
	else
	{
		if(num>=10&&num<20)
		{
			n=1;
			m=num-10;
		}
		else
		{
			if(num>=20&&num<30)
			{
				n=2;
				m=num-20;
			}
			else
			{
				if(num>=30&&num<40)
				{
					n=3;
					m=num-30;
				}
				else
				{
					if(num>=40&&num<50)
					{
						n=4;
						m=num-40;
					}
					else
					{
						if(num>=50&&num<60)
						{
							n=5;
							m=num-50;
						}
						else
						{
							if(num>=60&&num<70)
							{
								n=6;
								m=num-60;
							}
							else
							{
								if(num>=70&&num<80)
								{
									n=7;
									m=num-70;
								}
								else
								{
									if(num>=80&&num<90)
									{
										n=8;
										m=num-80;
									}
								}
							}
						}
					}
				}
			}
		}
	}
	gotoxy(a,b);
	if(num>9)
	{
		for(y=0;y<=9;y++)
		{
			gotoxy(a, b+y);
			for(x=0;x<=6;x++)
			{
				if(number[n][y][x]==15)
				{
					cout<<char(219);
				colour(numbers, 7, a-1, b+y-1);
				}
				else
				{
					cout<<char(255);
				}
			}
			cout<<endl;
		}
	}
	for(y=0;y<=9;y++)
	{
		gotoxy(a+9, b+y);
		for(x=0;x<=6;x++)
		{

			if(number[m][y][x]==15)
			{
				cout<<char(219);
				colour(numbers, 7, a+8, b+y-1);
			}
			else
			{
				cout<<char(255);
}
		}
		cout<<endl;
	}
}
return;
}


/////////////////////////////////////////////////////////////////////////////////////
//                                                                                 //
//                        BORDER DRAWING FUNCTION - draw_border()                  //
//                                                                                 //
/////////////////////////////////////////////////////////////////////////////////////

// A function for drawing the border around the screen (the smileys ) 

void draw_border()
{
	int x, y;			// x & y coords
	for(x=2;x<80;x=x+2)
	{
		gotoxy(x,1);
		if(((x+1)/2)%2==0)
		{
			cout<<char(2);
			cout<<endl;
			colour(border1, 1, x-1, 0);
		}
		else
		{
			cout<<char(1);
			cout<<endl;
			colour(border2, 1, x-1, 0);
		}
	}
	for(x=2;x<80;x=x+2)
	{
		gotoxy(x,49);
		if(((x+1)/2)%2==0)
		{
			cout<<char(2);
			cout<<endl;
			colour(border1, 1, x-1, 48);
		}
		else
		{
			cout<<char(1);
			cout<<endl;
			colour(border2, 1, x-1, 48);
		}
	}
	for(y=2;y<50;y=y+2)
	{
		gotoxy(1,y);
		if(((y+1)/2)%2==0)
		{
			cout<<char(2);
			cout<<endl;
			colour(border1, 1, 0, y-1);
		}
		else
		{
			cout<<char(1);
			cout<<endl;
			colour(border2, 1, 0, y-1);
		}
		gotoxy(80, y);
		if(((y+1)/2)%2==0)
		{
			cout<<char(2);
			cout<<endl;
			colour(border1, 1, 79, y-1);
		}
		else
		{
			cout<<char(1);
			cout<<endl;
			colour(border2, 1, 79, y-1);
		}
	}
	return;
}

/////////////////////////////////////////////////////////////////////////////////////
//                                                                                 //
//                        BOX DRAWING FUNCTION - box()                             //
//                                                                                 //
/////////////////////////////////////////////////////////////////////////////////////

// A function for drawing the boxes around the menu options

void box(int x, int y, int x2, int y2)
{
	// x & y are the x, y coords for the top-left corner of the box
	// x2 & y2 are the x, y coords for the bottom-right corner of the box
	
	int loop;			//Used for the loop
	gotoxy(x, y);
	cout<<char(201);
	for(loop=1; loop<(x2-x); loop++)
	{
		cout<<char(205);
	}
	cout<<char(187)<<endl;
	colour(box_c, x2-x+1, x-1, y-1);
	for(loop=1;loop<(y2-y);loop++)
	{
		gotoxy(x,loop+y);
		cout<<char(186)<<endl;
		colour(box_c, 1, x-1, loop+y-1);
		gotoxy(x2,loop+y);
		cout<<char(186)<<endl;
		colour(box_c, 1, x2-1, loop+y-1);
	}
	gotoxy(x, loop+y);
	cout<<char(200)<<endl;
	colour(box_c, 1, x-1, loop+y-1);
	for(loop=1; loop<(x2-x); loop++)
	{
		gotoxy(loop+x, y2);
		cout<<char(205)<<endl;
		colour(box_c, 1, loop+x-1, y2-1);
	}
	gotoxy(x2,y2);
	cout<<char(188)<<endl;
	colour(box_c, 1, x2-1, y2-1);
	for(loop=1;loop<(x2-x);loop++)
	{
		gotoxy(loop+x+1, y2+1);
		cout<<char(176)<<endl;
		colour(shadow, 1, loop+x, y2);
	}
	for(loop=1;loop<=(y2-y);loop++)
	{
		gotoxy(x2+1, y+loop+1);
		cout<<char(176)<<endl;
		colour(shadow, 1, x2, y+loop);
	}
	return;
}

/////////////////////////////////////////////////////////////////////////////////////
//                                                                                 //
//                        MAIN MENU FUNCTION - main_menu()                         //
//                                                                                 //
/////////////////////////////////////////////////////////////////////////////////////

// The Main Menu Drawing function, gets the main menu choice

int main_menu()
{
	int loop;		// used for the loop
	char choice;	// the variable the choice is put into
	clrscr();
	draw_border();


	gotoxy(13, 6);
	cout<<"Welcome to HappyMaths "<<char(1)<<" v7.0, by QuizzyBit Industries!"<<endl;

	gotoxy(36, 14);
	
	cout<<"Main Menu"<<endl;
	gotoxy(36, 15);
	for (loop=0;loop<9;loop++)
	{
		cout<< char(196);
	}
	cout<<endl;

	gotoxy(28, 20);
	cout<<"1. Start a quiz"<<endl;
	gotoxy(28, 23);
	cout<<"2. Change difficulty level"<<endl;
	gotoxy(28, 26);
	cout<<"3. Change question type"<<endl;
	gotoxy(28, 29);
	cout<<"4. View high scores"<<endl;
	gotoxy(28, 32);
	cout<<"5. Quit HappyMaths "<<char(1)<<endl;

	box(26, 11, 55, 37);
	
	box(26, 41, 55, 45);
	
	do
	{
		gotoxy(28, 43);
		cout<<char(1)<<">";
		cin>>choice;
		gotoxy(28, 43);
		cout<<"          "<<endl;
	}
	while(choice<'1' || choice>'5');
	return choice;
}

/////////////////////////////////////////////////////////////////////////////////////
//                                                                                 //
//                        QUESTION FUNCTION - question()                           //
//                                                                                 //
/////////////////////////////////////////////////////////////////////////////////////

// This is the big function which generates the questoins asks them, checks to see
// if they're correct then loops 10 times

int question()
{
	int num1, num2;  // number 1 and 2 of the question
	int loop1, loop2;  // for the 10 question loop and the 3 tries loop
	int correct=0; // whether or not the answer is correct
	int answer; // the answer given by the user
	int vari=0; // indicates whether the 'variety' question type is on or not
	int choice; // input from the user converted to an int
	int digit1, digit2; // 1st and 2nd digits of the users answer
	char choice1[15]; // the real input from the user
	float avg_bit, score=0, dif; // average time, used for the score, used for difficulty
	long time1=0, time2=0; // question start time, question end time

	if(type==5)
	{
		vari=1;
	}
	else
	{
		vari=0;
	}
	gotoxy(1,1);
	
///////////////////////////////////////////////////////////////////////////
// START MAIN LOOP                                                       //
///////////////////////////////////////////////////////////////////////////

// The loop which loops 10 times to ask ten questions
	
	for(loop1=1;loop1<=10;loop1++)
	{
		time1=0;time2=0;			// Reset timer

		if(vari==1)
		{
			type=(rand()%4)+1;
		}
		else
		{
			type=type;
		}
		
//////////////////////////////////////////////////////////
//  Question generation
//////////////////////////////////////////////////////////
		switch(type)
		{
		case 1:
			dif=10*difficulty;
			if(dif==7)
			{
				do
				{
					num1=rand()%10;
					num1=num1+2;
					num2=rand()%10;
					num2=num2+2;
				}
				while(num1+num2>15);
			}
			else
			{
				if (dif==10)
				{
					do
					{
						num1=rand()%19;
						num1=num1+4;
						num2=rand()%19;
						num2=num2+4;
					}
					while(num1+num2>28);
				}
				else
				{
					if (dif==13)
					{
						do
						{
							num1=rand()%37;
							num1=num1+6;
							num2=rand()%37;
							num2=num2+6;
						}
						while(num1+num2>53);
					}
				}
			}
			answer=num1+num2;
			avg_bit=avg_add;
			break;
		case 2:
			dif=10*difficulty;
			if (dif==7)
			{
				do
				{
					num1=rand()%10;
					num1=num1+2;
					num2=rand()%10;
					num2=num2+2;
				}
				while(num1-num2<2);
			}
			else
			{
				if (dif==10)
				{
					do
					{
						num1=rand()%19;
						num1=num1+4;
						num2=rand()%19;
						num2=num2+4;
					}
					while(num1-num2<5);
				}
				else
				{
					if (dif==13)
					{
						do
						{
							num1=rand()%37;
							num1=num1+6;
							num2=rand()%37;
							num2=num2+6;
						}
						while(num1-num2<8);
					}
				}
			}
			answer=num1-num2;
			avg_bit=avg_sub;
			break;
		case 3:
			dif=10*difficulty;
			if (dif==7)
			{
				do
				{
					num1=rand()%6;
					num1=num1+2;
					num2=rand()%6;
					num2=num2+2;
				}
				while(num1*num2>32);
			}
			else
			{
				if (dif==10)
				{
					do
					{
						num1=rand()%9;
						num1=num1+3;
						num2=rand()%9;
						num2=num2+3;
					}
					while(num1*num2>41);
				}
				else
				{
					if (dif==13)
					{
						do
						{
							num1=rand()%12;
							num1=num1+4;
							num2=rand()%12;
							num2=num2+4;
						}
						while(num1*num2>59);
					}
				}
			}
			answer=num1*num2;
			avg_bit=avg_mult;
			break;
		case 4:
			dif=10*difficulty;
			if (dif==7)
			{
				do
				{
					num1=rand()%15;
					num1=num1+2;
					num2=rand()%9;
					num2=num2+2;
				}
				while(num1%num2!=0||num1==num2);
			}
			else
			{
				if (dif==10)
				{
					do
					{
						num1=rand()%39;
						num1=num1+14;
						num2=rand()%15;
						num2=num2+2;
					}
					while(num1%num2!=0||num1==num2);
				}
				else
				{
					if (dif==13)
					{
						do
						{
							num1=rand()%59;
							num1=num1+31;
							num2=rand()%17;
							num2=num2+3;
						}
						while(num1%num2!=0||num1==num2);
					}
				}
			}
			answer=num1/num2;
			avg_bit=avg_div;
			break;
		case 5:
			break;
		}

//////////////////////////////////////////////////////////
//  End of question generation
//////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////
// draws the border & the boxes on the screen
/////////////////////////////////////////////////////////////////
			clrscr();
			draw_border();
			box(3,3,77,10);
			box(3,42,77,46);
			gotoxy(5, 5);
			cout<<"Question "<<loop1<<endl;
			gotoxy(59,8);
			cout<<"Total score: "<<setprecision(1)<<setiosflags(ios::fixed)<<final_score<<endl;
			if(correct==1)
			{
				gotoxy(5,8);
				cout<<"Correct!"<<endl;
				correct=0;
			}
		
		box(12,16,66, 33);
	

/////////////  End of drawing the boxes on the screen

///////////////////////////////////////////////////////////////
// start small loop
///////////////////////////////////////////////////////////////

// Loop which asks the question again if you get it wrong untill 3 times
// Then it displays the correct answer

		for(loop2=1;loop2<=3;loop2++)
		{
			gotoxy(63, 5);
			cout<<"Tries left: "<<4-loop2<<endl;

///////////////////////////////////////////
// draw big numbers onto the screen
///////////////////////////////////////////
	number(num1, 15, 20);
		
	switch(type)
	{
	case 1:
		number(100, 36, 20);
		break;
	case 2:
		number(101, 36, 20);
		break;
	case 3:
		number(102, 36, 20);
		break;
	case 4:
		number(103, 36, 20);
		break;
	case 5:
		break;
	}
	number(num2, 48, 20);
/////////////////////////////////////////////
// END draw big numbers
/////////////////////////////////////////////
		
			time1=clock();				// get start time
			
			do
			{
				gotoxy(5,44);		
				cout<<char(1)<<">";
				cin>>choice1;				// get answer from user
				gotoxy(5,44);		
				cout<<"          "<<endl;

			}while(!(choice1[0]<58 && choice1[0]>47));  // Checks to see if they have used a number for the answer

////////Bit for making sure that you crash the program by entering a letter 

			if(choice1[0]<58 && choice1[0]>47)
			{
				digit2=choice1[0]-48;
				digit1=choice1[1]-48;
			}
			if(choice1[1]<58 && choice1[1]>47)
			{
				choice=((digit2*10)+digit1);
			}
			else
			{
				choice=digit2;
			}
////////////////////////////////////End of that checking bit

///////////////////////////////////////////////////////////////////////
// finish drawing
////////////////////////////////////////////

///////////////////////////////////		
			if(choice==answer)
			{
				correct=1;
				if(loop2==1)
				{
					ciog++;
				}
			
				time2=clock();
				
				taken=float((time2-time1)/CLOCKS_PER_SEC);	// Get the time taken to answer the question

				if(taken==0){taken++;}						// Ensure no division by zero
				total_taken=total_taken+taken;
				if(loop2==1)
				{
					score=10*difficulty*(((avg_bit)*difficulty)/taken); // Calculate the score
					/*score = 10*difficulty;*/
				}
				else
				{
					if(loop2==2)
					{
						score=5*difficulty*(((avg_bit)*difficulty)/taken); // Calculate the score
						/*score = 5*difficulty;*/
					}
					else
					{
						score=2*difficulty*(((avg_bit)*difficulty)/taken); // Calculate the score
						/*score = 2*difficulty;*/
					}
				}
				break;
			}

			gotoxy(5,8);
			cout<<"Incorrect"<<endl;
			correct=0;
		}
///////////////////////////////////////////////////////////////
// END small loop
///////////////////////////////////////////////////////////////
	
	
		if(choice==answer)
		{
		}
		else
		{
			score=0;
	
/////////////////////////////////////////////
// write when got it wrong after 3 tries
/////////////////////////////////////////////
			gotoxy(63, 5);
			cout<<"Tries left: "<<4-loop2<<endl;
			gotoxy(5, 38);
			cout<<"Incorrect after 3 tries, the correct answer to ";
			switch(type)
			{
			case 1:
				cout<<num1<<" + "<<num2<<" is "<<num1+num2<<endl;
				break;
			case 2:
				cout<<num1<<" - "<<num2<<" is "<<num1-num2<<endl;
				break;
			case 3:
				cout<<num1<<" x "<<num2<<" is "<<num1*num2<<endl;
				break;
			case 4:
				cout<<num1<<" "<<char(246)<<" "<<num2<<" is "<<num1/num2<<endl;
				break;
			case 5:
				break;
			}
			gotoxy(5, 40);
			cout<<"Press enter to continue...";
		
			int null=getchar();
			cout<<endl;
/////////////////////////////////////////////
// END write when got it wrong after 3 tries
/////////////////////////////////////////////
	
		}
	
	
		final_score = final_score + score;
		
		if(loop1==10)
		{
			avg_bit = ((total_taken/loop1)+(avg_bit)/2);  // Adjusting the average time at the end of the quiz
		}
		
		switch(type)
		{
		case 1:
			avg_add=avg_bit;
			break;
		case 2:
			avg_sub=avg_bit;
			break;
		case 3:
			avg_mult=avg_bit;
			break;
		case 4:
			avg_div=avg_bit;
			break;
		case 5:
			break;
		}
	}

///////////////////////////////////////////////////////////////////////////
// END MAIN LOOP                                                         //
///////////////////////////////////////////////////////////////////////////
	
///////////////MORE DRAWING//////////////////	
	gotoxy(2,2);
	cout<<setprecision(1)<<setiosflags(ios::fixed);
	gotoxy(59,8);
	cout<<"Total score: "<<final_score<<endl;
	gotoxy(5, 38);
	
	cout<<"Quiz completed!  press the enter key for statistics...                   "<<endl;
///////////////END MORE DRAWING//////////////	
	
	if (vari==1)
	{
		type=5;
	}
	else
	{
		type=type;
	}
	int null=getchar();
	
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////
//                                                                                 //
//                        HIGH SCORE FUNCTION - high_score()                       //
//                                                                                 //
/////////////////////////////////////////////////////////////////////////////////////

// This function receives a score, looks up the file, decides where to put
// in the high scores and then saves the high scores

void high_score(float sc, char na[15])
{
	_score s[10]; // Structure _score (defined at top) for high score data
	

	ifstream scorein("high.dat", ios::in);
	if(!scorein)
	{
		cerr<<"File could not be opened!"<<endl;
		exit(1);
	}
	
	for(int  l=0;l<10;l++)
	{
		scorein>>s[l].rank>>s[l].name>>s[l].score;
	}
	scorein.close();

	for(int z=9;z>0;z--)
	{
		if(sc>=s[0].score)
		{
			for(l=9;l>0;l--)
			{
				for(int i=0;i<15;i++){s[l].name[i] = s[l-1].name[i];}
				s[l].score = s[l-1].score;
			}
			
			for(int i=0;i<15;i++){s[0].name[i] = na[i];}
			s[0].score=sc;
			break;
		}
		else
		{

		if(sc>=s[z].score && sc<s[z-1].score)
		{
			for(l=9;l>z;l--)
			{
				for(int i=0;i<15;i++){s[l].name[i] = s[l-1].name[i];}
				s[l].score = s[l-1].score;
			}
			
			for(int i=0;i<15;i++){s[z].name[i] = na[i];}
			s[z].score=sc;
			break;
		}
		}
	}

	ofstream scoreout("high.dat", ios::out);
	if(!scoreout)
	{
		cerr<<"File could not be opened!"<<endl;
		exit(1);
	}

	for(l=0;l<10;l++)
	{
		scoreout<<s[l].rank<<' '<<s[l].name<<' '<<s[l].score<<endl;
	}
	scoreout.close();
	return;
}
/////////////////////////////////////////////////////////////////////////////////////
//                                                                                 //
//                        DRAW AFTER QUIZ FUNCTION - draw_after_quiz()             //
//                                                                                 //
/////////////////////////////////////////////////////////////////////////////////////

// This function draws the statistics after the quiz and
// gets the users name if they have a high score

void draw_after_quiz()
{
	_score s[10]; // Structure _score (defined at top) for high score data
	int length;  //length of the underline for the title
	int a;  // used for setting out text
	int loop; // for looping 
	int dif; // integer of difficulty
	char nam[15]; // Users name, if they have got a high score

	clrscr();
	draw_border();
	box(20, 8, 60, 38);
	switch(type)
	{
	case 1:
		length=20;
		gotoxy(30,10);
		a=30;
		cout<<"Addition";
		break;
	case 2:
		length=23;
		gotoxy(28,10);
		a=28;
		cout<<"Subtraction";
		break;
	case 3:
		length=26;
		gotoxy(27,10);
		a=27;
		cout<<"Multiplication";
		break;
	case 4:
		length=20;
		gotoxy(30,10);
		a=30;
		cout<<"Division";
		break;
	case 5:
		length=19;
		gotoxy(31,10);
		a=31;
		cout<<"Variety";
		break;
	}
	cout<<" Quiz stats!"<<endl;
	gotoxy(a, 11);
	for(loop=0;loop<length;loop++)
	{
		cout<< char(196);
	}
	cout<<endl;
	gotoxy(22,17);
	dif=int(10*difficulty);
	if(dif==7)
	{
		cout<<"Difficulty: Easy"<<endl;
	}
	else
	{
		if (dif==10)
		{
			cout<<"Difficulty: Medium"<<endl;
		}
		else
		{
			if (dif==13)
			{
				cout<<"Difficulty: Hard"<<endl;
			}
		}
	}
	gotoxy(22, 21);
	cout<<"Total points:                  "<<final_score<<endl;
	gotoxy(22, 24);
	cout<<"Average points per question:   "<<final_score/10<<endl;
	gotoxy(22, 27);
	cout<<"Total time:                    "<<total_taken<<endl;
	gotoxy(22, 30);
	cout<<"Average time per question:     "<<total_taken/10<<endl;
	gotoxy(22, 33);
	cout<<"Questions correct in one go:   "<<ciog<<"/10"<<endl;
	gotoxy(22, 36);
	cout<<"Press enter to return to menu..."<<endl;

	ifstream scorein("high.dat", ios::in);
	if(!scorein)
	{
		cerr<<"File could not be opened!"<<endl;
		exit(1);
	}

	for(int  l=0;l<10;l++)
	{
		scorein>>s[l].rank>>s[l].name>>s[l].score;
	}
	scorein.close();
	int rank=0;
	
	if(final_score>s[0].score)
	{
		rank=1;
	}
	else
	{
		if(final_score>s[1].score)
		{
			rank=2;
		}
		else
		{
			if(final_score>s[2].score)
			{
				rank=3;
			}
			else
			{
				if(final_score>s[3].score)
				{
					rank=4;
				}
				else
				{
					if(final_score>s[4].score)
					{
						rank=5;
					}
					else
					{
						if(final_score>s[5].score)
						{
							rank=6;
						}
						else	
						{
							if(final_score>s[6].score)
							{
								rank=7;
							}
							else
							{
								if(final_score>s[7].score)
								{
									rank=8;
								}
								else
								{
									if(final_score>s[8].score)
									{
										rank=9;
									}
									else
									{
										if(final_score>s[9].score)
										{
											rank=10;
										}
										
									}
								}
							}
						}
					}
				}
			}
		}
	}

	if(rank>0)
	{
		gotoxy(3, 42);
		cout<<"Congratulations, you are rank "<<rank<<" in the high scores,"<<endl;
		gotoxy(3,44);
		cout<<"Please enter your name. "<<char(1)<<">";
		cin.getline(nam, 15);
		cin.getline(nam, 15);
		cout<<endl;
		for(int loop=0;loop<15;loop++)
		{
			if(nam[loop]==' ')
			{
				nam[loop]='_';
			}
			else
			{
				nam[loop]=nam[loop];
			}
		}
		high_score(final_score,nam);
	}
	else
	{
	int null = getchar();
	return;
	}
	return;
}

/////////////////////////////////////////////////////////////////////////////////////
//                                                                                 //
//                        DRAW BEFORE QUIZ FUNCTION - draw_before_quiz()           //
//                                                                                 //
/////////////////////////////////////////////////////////////////////////////////////

// This function writes the instructions to the screen before the quiz

int draw_before_quiz()
{
	clrscr();
	draw_border();

	int length, loop, a; // all used for same as previious
	char choice; // user input

	box(20, 8, 60, 38);
	switch(type)
	{
	case 1:
		length=14;
		gotoxy(33,10);
		a=33;
		cout<<"Addition";
		break;
	case 2:
		length=17;
		gotoxy(31,10);
		a=31;
		cout<<"Subtraction";
		break;
	case 3:
		length=20;
		gotoxy(30,10);
		a=30;
		cout<<"Multiplication";
		break;
	case 4:
		length=14;
		gotoxy(33,10);
		a=33;
		cout<<"Division";
		break;
	case 5:
		length=13;
		gotoxy(34,10);
		a=34;
		cout<<"Variety";
		break;
	}
	cout<<" Quiz!"<<endl;
	gotoxy(a, 11);
	for (loop=0;loop<length;loop++)
	{
		cout<<char(196);
	}
	cout<<endl;
	gotoxy(22,17);
	float dif=10*difficulty;
	if (dif==7)
	{
		cout<<"Current difficulty: Easy"<<endl;
	}
	else
	{
		if (dif==10)
		{
			cout<<"Current difficulty: Medium"<<endl;
		}
		else
		{
			if(dif==13)
			{
				cout<<"Current difficulty: Hard"<<endl;
			}
		}
	}
	gotoxy(22, 21);
	cout<<"1. Start quiz"<<endl;
	gotoxy(22, 24);
	cout<<"2. Return to menu"<<endl;
	gotoxy(22, 27);
	cout<<"Each quiz comprises of ten questions"<<endl;
	gotoxy(22, 28);
	cout<<"of a particular type.  More points"<<endl;
	gotoxy(22, 29);
	cout<<"will be awarded for harder questions"<<endl;
	gotoxy(22, 30);
	cout<<"and less for easier questions.  The"<<endl;
	gotoxy(22, 31);
	cout<<"faster you do a question the more"<<endl;
	gotoxy(22, 32);
	cout<<"points you will get.  You will also"<<endl;
	gotoxy(22, 33);
	cout<<"get more points for getting a question"<<endl;
	gotoxy(22, 34);
	cout<<"on the first go.  After 3 incorrect"<<endl;
	gotoxy(22, 35);
	cout<<"attempts, the correct answer will be"<<endl;
	gotoxy(22, 36);
	cout<<"displayed and you will get no points"<<endl;
	box(20, 42, 60, 46);
	do
	{
		gotoxy(22, 44);
		cout<<char(1)<<">";
		cin>>choice;
		gotoxy(22, 44);
		cout<<"          "<<endl;
	}
	while(choice<'1' || choice >'2');
return choice;
}

/////////////////////////////////////////////////////////////////////////////////////
//                                                                                 //
//                        QUIZ FUNCTION - quiz()                                   //
//                                                                                 //
/////////////////////////////////////////////////////////////////////////////////////

// Calls all the appropriate functions to operate the quiz section

void quiz()
{
		
	switch(draw_before_quiz())
	{
	case '1':
		question();
		break;
	case '2':
		return;
		break;
	default:
		cout<<"words";
		break;
	}
	draw_after_quiz();
	return;
}
	
/////////////////////////////////////////////////////////////////////////////////////
//                                                                                 //
//                        DRAW CHANGE DIFFICULTY FUNCTION - draw_chg_difficulty()  //
//                                                                                 //
/////////////////////////////////////////////////////////////////////////////////////

// Draws the change difficulty menu and gets the user choice

char draw_chg_difficulty()
{
	int loop;
	char choice;
	clrscr();
	draw_border();
	gotoxy(13, 6);
	cout<<"Welcome to HappyMaths "<<char(1)<<" v7.0, by QuizzyBit Industries!"<<endl;
	box(26, 11, 54, 37);
	gotoxy(32, 14);
	cout<<"Change Difficulty"<<endl;
	gotoxy(32, 15);
	for (loop=0;loop<17;loop++)
	{
		cout<< char(196);
	}
	cout<<endl;
	gotoxy(28, 19);
	float dif=10*difficulty;
	if (dif==7)
	{
		cout<<"Current: Easy"<<endl;
	}
	else
	{
		if (dif==10)
		{
			cout<<"Current: Medium"<<endl;
		}
		else
		{
			if (dif==13)
			{
				cout<<"Current: Hard"<<endl;
			}
		}
	}
	gotoxy(28, 24);
	cout<<"1. Easy"<<endl;
	gotoxy(28, 27);
	cout<<"2. Medium"<<endl;
	gotoxy(28, 30);
	cout<<"3. Hard"<<endl;
	box(26, 41, 54, 45);
	do
	{
		gotoxy(28, 43);
		cout<<char(1)<<">";
		cin>>choice;
		gotoxy(28, 43);
		cout<<"           ";
	}
	while(choice<'1' || choice>'3');

return choice;
}

/////////////////////////////////////////////////////////////////////////////////////
//                                                                                 //
//                        CHANGE DIFFICULTY FUNCTION - chg_difficulty()            //
//                                                                                 //
/////////////////////////////////////////////////////////////////////////////////////

// Opens the change difficulty menu, recieves the users choice and changes
// the difficulty

void chg_difficulty()
{
	switch(draw_chg_difficulty())
	{
	case '1':
		difficulty=float(0.7);
		break;
	case '2':
		difficulty=1;
		break;
	case '3':
		difficulty=float(1.3);
		break;
	default:
		cout<<"What are you doing here???";
	}
	return;
}

/////////////////////////////////////////////////////////////////////////////////////
//                                                                                 //
//                        DRAW CHANGE TYPE FUNCTION - draw_chg_type()              //
//                                                                                 //
/////////////////////////////////////////////////////////////////////////////////////

// Draws the change type menu and gets the users choice

char draw_chg_type()
{
	int loop;
	char choice;
	clrscr();
	draw_border();
	gotoxy(13, 6);
	cout<<"Welcome to HappyMaths "<<char(1)<<" v7.0, by QuizzyBit Industries!"<<endl;
	box(26, 11, 54, 37);
	gotoxy(30, 14);
	cout<<"Change Question Type"<<endl;
	gotoxy(30, 15);
	for (loop=0;loop<20;loop++)
	{
		cout<< char(196);
	}
	cout<<endl;
	gotoxy(28, 19);
	switch(type)
	{
	case 1:
		cout<<"Current: Addition"<<endl;
		break;
	case 2:
		cout<<"Current: Subtraction"<<endl;
		break;
	case 3:
		cout<<"Current: Multiplication"<<endl;
		break;
	case 4:
		cout<<"Current: Division"<<endl;
		break;
	case 5:
		cout<<"Current: Variety"<<endl;
		break;
	}
	gotoxy(28, 22);
	cout<<"1. Addition"<<endl;
	gotoxy(28, 25);
	cout<<"2. Subtraction"<<endl;
	gotoxy(28, 28);
	cout<<"3. Multiplication"<<endl;
	gotoxy(28, 31);
	cout<<"4. Division"<<endl;
	gotoxy(28, 34);
	cout<<"5. Variety"<<endl;
	box(26, 41, 54, 45);
	do
	{
		gotoxy(28, 43);
		cout<<char(1)<<">";
		cin>>choice;
		gotoxy(28, 43);
		cout<<"          ";
	}
	while(choice<'1' || choice>'5');
return choice;
}

/////////////////////////////////////////////////////////////////////////////////////
//                                                                                 //
//                        CHANGE TYPE FUNCTION - chg_type()                        //
//                                                                                 //
/////////////////////////////////////////////////////////////////////////////////////

// Receives the users input from the draw change type function and changes the type

void chg_type()
{
	switch(draw_chg_type())
	{
	case '1':
		type=1;
		break;
	case '2':
		type=2;
		break;
	case '3':
		type=3;
		break;
	case '4':
		type=4;
		break;
	case '5':
		type=5;
		break;
	default:
		type=1;
		break;
	}
	return;
}

/////////////////////////////////////////////////////////////////////////////////////
//                                                                                 //
//                        QUIT FUNCTION - quit()                                   //
//                                                                                 //
/////////////////////////////////////////////////////////////////////////////////////

// Saves the options to the disk and then exits the program

void quit()
{
	ofstream outdata("data.dat", ios::out);
	if(!outdata)
	{
		cerr<<"File could not be opened!"<<endl;
		exit(1);
	}
	outdata<<type<<' '<<difficulty<<' '<<avg_add<<' '<<avg_sub<<' '<<avg_mult<<' '<<avg_div<<'\n';
	outdata.close();
	exit(0);
}

/////////////////////////////////////////////////////////////////////////////////////
//                                                                                 //
//                        STARTUP FUNCTION - startup()                             //
//                                                                                 //
/////////////////////////////////////////////////////////////////////////////////////

// Loads the settings from the disk, calls the screensize function and then
// seeds the rand() function with time() so that it is different all the time

void startup()
{
	screensize();
	srand(time(0));
	ifstream datain("data.dat", ios::in);
	if(!datain)
	{
		cerr<<"File could not be opened!"<<endl;
		exit(1);
	}
	datain>>type>>difficulty>>avg_add>>avg_sub>>avg_mult>>avg_div;
	datain.close();
	return;
}

/////////////////////////////////////////////////////////////////////////////////////
//                                                                                 //
//                        VIEW HIGH SCORES FUNCTION - view_high_scores()           //
//                                                                                 //
/////////////////////////////////////////////////////////////////////////////////////

// Loads and the displays the high scores on the screen

void view_high_scores()
{
	_score s[10];
	int loop;
	ifstream scorein("high.dat", ios::in);
	if(!scorein)
	{
		cerr<<"File could not be opened!"<<endl;
		exit(1);
	}
	
	for(int l=0;l<10;l++)
	{
		scorein>>s[l].rank>>s[l].name>>s[l].score;
	}
	scorein.close();
	
	clrscr();
	draw_border();
	box(20, 8, 60, 38);

	gotoxy(35,10);
	cout<<"High Scores"<<endl;
	gotoxy(35, 11);
	for (loop=0;loop<11;loop++)
	{
		cout<< char(196);
	}
	cout<<endl;
	
	for(loop=0;loop<10;loop++)
	{
		gotoxy(22, 15+(2*loop));
		cout<<s[loop].rank<<endl;
		
		for(int l=0;l<10;l++)
		{
			if(s[loop].name[l]=='_')
			{
				s[loop].name[l]=' ';
			}
		}

		gotoxy(35, 15+(2*loop));
		cout<<s[loop].name<<endl;

		gotoxy(54, 15+(2*loop));
		cout<<setprecision(1)<<setiosflags(ios::fixed)<<s[loop].score<<endl;
	}
	gotoxy(22, 36);
	cout<<"Press enter to continue..."<<endl;
	gotoxy(48,36);
	int null=getchar();	

	return;
}
/////////////////////////////////////////////////////////////////////////////////////
//                                                                                 //
//                        MAIN FUNCTION - main()                                   //
//                                                                                 //
/////////////////////////////////////////////////////////////////////////////////////

// Calls the main menu drawing functon and contains the main switch statement
// Controls all aspects of the program

int main()
{
	startup();
	for(;;)
	{
		switch(main_menu())
		{
		case '1':
			quiz();
			break;
		case '2':
			chg_difficulty();
			break;
		case '3':
			chg_type();
			break;
		case '4':
			view_high_scores();
			break;
		case '5':
			quit();
			break;
		default:
			cout<<"Interesting";
		}
	}
return 0;
}
