// Begin of File

//************************************************
// Programmer : Mohammad Rastkar
// Last Build : 5_5_07
// Compiler   : VC++6 - Win32 Console App
// Purpose    : Asks some questions in multiplication (answer is in range of 1~100)
//************************************************


//////////////<  Includes  >/////////////////

#include <iostream>
using namespace std;

#include <fstream> // file interactions
#include <string> // class string
#include <cstdlib> // system(), exit(), rand(), srand()
#include <time.h> // time()
#include <conio.h>  // getch()


//////////////<  Declarations  >/////////////

string convertName  ( string ); // return ID of a student in class list or return "ERR" -> error Or "NF" -> not found
string numberToWord ( int );    // converting a number from 1 to 100 to its corresponding word

//////////////////<  main  >/////////////////

int main()
{
	string std_name, 
		   std_id,
		   res,
		   temp;

	int    num1,
		   num2,
		   correct   =0,
		   incorrect =0;
	
	char   loop;
	
	system("cls"); // clear screen
	
	cout << "Please enter your name : ";
	cin  >> std_name;

	std_id = convertName( std_name );

	if (std_id == "ERR")
	{
		cout << "\n\n Can't open or create 'Class_List' file" << endl;
		system ( "pause" );
		return 1;
	}

	if ( std_id == 	"NF")
	{
		cout << std_name << ", you are not required to take this test. \n\nEnd of program" << endl;
		system ("pause");
		
		return 0;
	}

	cout << "\nStudent id : " << std_id << ", starting the test.\n-> Please answer with lowercase letters. [100 = hundred]\n";
	srand( (unsigned)time( NULL ) ); // seed the random_number_generator with the current time
	
	do
	{
		num1 = rand()%10 + 1; // a random number : 1~10
		num2 = rand()%10 + 1;

		cout << '\n' << std_name <<  ", what is " << numberToWord(num1) << " times " << numberToWord (num2) << " ? ";
		cin  >> res;

		if (cin.get() != 10) // if there is not one word... (ASCII of [Enter] == 10)
		{
			cin >> temp;
			res += ' ' + temp;
		}
		
		if ( res == numberToWord(num1 * num2) )
		{
			cout << "Well done, " << std_name << ", you are correct.\n";
			correct++;
		}
		else
		{
			cout << "That is not right, " << std_name << ", the correct answer is : " << numberToWord(num1 * num2) << ".\n";
			incorrect++;
		}

		
		cout << "Do you want to continue (y/n) : ";
		loop = getch();

	}while (loop == 'y' || loop == 'Y' );

	cout << "\nCorrect answers : " << correct << "\nIncorrect answers : " << incorrect;
	cout << "\nEnd of program" << endl;
	system ("pause");
	return 0;
}


///////////////<  Definitions  >/////////////

string convertName ( string pName )
{
	fstream *fs = new fstream( "Class_List.txt", ios::in);
	string file_str;
	
	if (!fs)
		return string("ERR"); // ERRor
	
	(*fs) >> file_str; // store whole of the file in 'file_str'

	fs->close();
	delete fs;
	fs=0;

	int pos = file_str.find(pName); // first character of Name

	if (pos == string::npos)
		return string("NF"); // Not Found

	pos -= 2; // last of ID

	int del_num = pos - file_str.rfind('>', pos); // ID length

	pos = file_str.rfind('>', pos) + 1; // first of ID

	return ( file_str.substr (pos, del_num) ); // ID
}

string numberToWord ( int num )
{
	string zero_to_nine       [11] = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    string ten_to_nineteen    [10] = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
	string twenty_to_ninety   [10] = { "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};

	if (num == 100)
		return string("hundred");
	
	if ( int(num/10) == 1) // 10~19
		return ten_to_nineteen[num%10];

	if ( int(num/10) == 0 )
		return zero_to_nine[num%10];
	
	if ( num%10 == 0 )
		return twenty_to_ninety[num/10];

	return ( twenty_to_ninety[num/10] + ' ' + zero_to_nine[num%10] );
}

// End of File
