Originally posted by cyberCLoWn
Code:
/*
##############################################
## Addition & Subtraction Assistance Program
##############################################
##
## Written by : CC
## Last modified : 15/01/2004
## Comments : A program designed to help children practise simple addition
## and subtraction sums.
##############################################
*/
#include <cstdlib>
Nice
using std::rand;
using std::srand;
#include <iomanip>
Nice
using std::setw;
#include <iostream>
... but then... :(
using namespace std;
// Prototypes
void addition();
void subtraction();
char repeat();
Misleading name. It seems like this function would check y for validity.
Actually it just resets cin to a ready state and writes an error message. It doesn't even use the argument.
void numeric_check( int y );
It would make more sense to make the argument a bool called correct and do the comparison outside.
void response( int total1, int total2 );
inline void screen_clear();
int main()
{
int pickAddSub;
Make the next two one line. Initialization is better than definition followed by assignment.
char again;
again = 'y';
cout << "Addition & Subtraction Assistance Program\n"
<< "-----------------------------------------\n";
srand( time( 0 ) );
while ( again == 'y' )
{
// Giving pickAddSub a random value of either 1 or 2
pickAddSub = ( 1 + rand() % 2 );
if ( pickAddSub == 1 )
addition();
Given that the rand guarantees that pickAddSub is 1 or 2, the else here isn't really necessary.
else if ( pickAddSub == 2 )
subtraction();
// Asks the user if they want to carry on playing
again = repeat();
screen_clear();
}
return 0;
}
void addition()
{
int number1, number2, usertotal, total;
Unused as far as I can see.
char again;
number1 = ( 0 + rand() % 15 ); // Gives number1 and number2 random values each time function runs
number2 = ( 0 + rand() % 15 );
total = number1 + number2;
cout << '\n' << setw( 6 ) << number1 << " + " << number2 << " = ";
while ( !(cin >> usertotal) ) // Incase something non-valid is entered the program won't die
{
numeric_check( usertotal );
}
response( usertotal, total ); // Random responses to correct or incorrect answers
}
void subtraction()
{
int number1, number2, usertotal, total;
Here, too.
char again;
number1 = ( 0 + rand() % 15 ); // Gives number1 and number2 random values each time function runs
number2 = ( 0 + rand() % 15 );
// We cannot have negative numbers subtracted by negative numbers, the poor children
if ( number1 > number2 )
{
total = number1 - number2;
cout << '\n' << setw( 6 ) << number1 << " - " << number2 << " = ";
while ( !(cin >> usertotal) ) // Incase something non-valid is entered the program won't die
{
numeric_check( usertotal );
}
}
else
{
total = number2 - number1;
cout << '\n' << setw( 6 ) << number2 << " - " << number1 << " = ";
while ( !(cin >> usertotal) ) // Incase something non-valid is entered the program won't die
{
numeric_check( usertotal );
}
}
response( usertotal, total ); // Random responses to correct or incorrect answers
}
char repeat()
{
char again;
cout << "Would you like to try again? [y/n] ";
cin >> again;
return again;
}
inline void screen_clear()
{
system ( "cls" );
cout << "Addition & Subtraction Assistance Program\n"
<< "-----------------------------------------\n";
}
void numeric_check( int y )
{
cin.clear();
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
cerr << "\nWoops! Try again: ";
}
void response( int total1, int total2 )
{
int pick;
pick = ( 1 + rand() % 4 );
if ( total1 == total2 )
{
switch( pick ) // if correct
{
case 1:
cout << "\n /\n\\/\n\nVery Good!\n";
break;
case 2:
cout << "\n /\n\\/\n\nExcellent!\n";
break;
case 3:
cout << "\n /\n\\/\n\nNice Work!\n";
break;
case 4:
cout << "\n /\n\\/\n\nKeep up the good work!\n";
break;
}
}
else
{
switch( pick ) // if incorrect
{
case 1:
cout << "\n\\/\n/\\\n\nNo.\n";
break;
case 2:
cout << "\n\\/\n/\\\n\nWrong. Try once more.\n";
break;
case 3:
cout << "\n\\/\n/\\\n\nDon't give up!\n";
break;
case 4:
cout << "\n\\/\n/\\\n\nNo. Keep trying.\n";
break;
}
}
}