Library code snippets
[C++] Simple Multiplication Test - Involves with : files & strings & arrays & ...
By Mohammad Rastkar, published on 07 May 2007
Page 2 of 5
- About The Program
- Lettered_Numbers.cpp
- Class_List_Manipulation.cpp
- Download Files
- Note About Retrieving a File
Lettered_Numbers.cpp
// 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
Related articles
Related discussion
-
C++ CATMULL-ROM
by tkruvgt (0 replies)
-
VS2005 app's won't run on another machine
by ted4444 (0 replies)
-
VB.NET: Hide and show table using radio buttons
by converter2009 (1 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
How to create a games like FIFA08
by mawcot (0 replies)
This thread is for discussions of [C++] Lettered_Numbers : Simple Multiplication Test, that involves with : files & strings & arrayes & ....