# Thread: How are things looking

1. ## How are things looking

Yes yes, I know you haven't heard from me in a while. Holidays are brilliant hey! Hope everyone enjoyed theirs!

Anyhow, I've just written this little program. I'll be adding more things to it during the course of tomorrow, but for now I was wondering if there are any comments or crits that people could give me on the code in it's current stage.

Code:
```/*
##############################################
## Addition & Subtraction Assistance Program
##############################################
##
## Written by    : CC
## Comments      : A program designed to help children practise simple addition
##                 and subtraction sums.
##############################################
*/

#include <cstdlib>
using std::rand;
using std::srand;

#include <iomanip>
using std::setw;

#include <iostream>
using std::cout;
using std::cin;
using std::setw;

// Prototypes
char subtraction();
char repeat( int total1, int total2 );
inline void screen_clear();

int main()
{
char again;
again = 'y';

cout << "Addition & Subtraction Assistance Program" << '\n';

srand( time( 0 ) );

while ( again == 'y' )
{
// Giving pickAddSub a random value of either 1 or 2
pickAddSub = ( 1 + rand() % 2 );

if ( pickAddSub == 1 )
else if ( pickAddSub == 2 )
again = subtraction();

screen_clear();
}

return 0;
}

{
int number1, number2, usertotal, total;
char again;

srand( time( 0 ) );

number1 = ( 0 + rand() % 15 );
number2 = ( 0 + rand() % 15 );

total = number1 + number2;

cout << "\nHere is the sum...\n"
<< setw( 6 ) << number1 << "  +  " << number2 << "  =  ";
cin >> usertotal;

again = repeat( usertotal, total );
return again;
}

char subtraction()
{
int number1, number2, usertotal, total;
char again;

srand( time( 0 ) );

number1 = ( 0 + rand() % 15 );
number2 = ( 0 + rand() % 15 );

// We cannot have negative numbers
if ( number1 > number2 )
{
total = number1 - number2;

cout << "\nHere is the sum...\n"
<< setw( 6 ) << number1 << "  -  " << number2 << "  =  ";
cin >> usertotal;
}
else
{
total = number2 - number1;

cout << "\nHere is the sum...\n"
<< setw( 6 ) << number2 << "  -  " << number1 << "  =  ";
cin >> usertotal;
}

again = repeat( usertotal, total );
return again;
}

char repeat( int total1, int total2 )
{
char again;

if ( total1 == total2 )
{
cout << "Very Good!\n"
<< "Would you like to try again? [y/n] ";
cin >> again;
return again;
}
else
{
cout << "No.\n"
<< "Would you like to try again? [y/n] ";
cin >> again;
return again;
}
}

inline void screen_clear()
{
system ( "CLS" );
}```
Much appreciated as always

2. Code:
```char addition()
{
int number1, number2, usertotal, total;

number1 = ( 0 + rand() % 15 );
number2 = ( 0 + rand() % 15 );

total = number1 + number2;

cout << "\nHere is the sum...\n"
<< setw( 6 ) << number1 << "  +  " << number2 << "  =  ";
cin >> usertotal;

return repeat( usertotal, total );
}```
For starters, you only ever call srand once per execution of your program. Don't call it more than once. I mean, I suppose you can, but there is no point to doing so, unless you're specificly seeding rand to give you a specific set of values.

Next, you don't really need the 'again' varible here, since all it's doing is holding the return value of another function, and then immediately returning it.

Code:
```char repeat( int total1, int total2 )
{
char again;

if ( total1 == total2 )
{
cout << "Very Good!" << endl << "Would you like to try again? [y/n] ";
}
else
{
cout << "No." << endl << "Would you like to try again? [y/n] ";
}
cin >> again;
return again;
}```
In either case you're going to be reading into 'again' and returning it, so just do it once at the bottom of the function instead of twice in each branch.

You could of course compact your cout lines to a single string with a newline, instead of three chunks, but either way works.

Also, you could just call 'repeat' in the first while loop, rather than calling once in both the add and subtract function. It would take a minor rewrite, but it would look cleaner.

Quzah.

3. Code:
```These, to me, just don't seem to make any sense
again = subtraction();
again = repeat( usertotal, total );

And I would, preferably make these functions take some arguments and return the result

int addition(int firstNum, int secondNum)
int subtraction(int firstNum, int secNum)

A sum is the amount obtained as a result of adding numbers, so the following could be confusing.

total = number2 - number1;

cout << "\nHere is the sum...\n"
<< setw( 6 ) << number2 << "  -  " << number1 << "  =  ";
cin >> usertotal;
Of course above can be seen as the sum of a positive and a negative number, but children will miss it.
```

4. I've been working at this app for a while today and this is what I've come up with. Hope it's improved

Code:
```/*
##############################################
## Addition & Subtraction Assistance Program
##############################################
##
## Written by    : CC
## Comments      : A program designed to help children practise simple addition
##                 and subtraction sums.
##############################################
*/

#include <cstdlib>
using std::rand;
using std::srand;

#include <iomanip>
using std::setw;

#include <iostream>
using namespace std;

// Prototypes
void subtraction();
char repeat();
void numeric_check( int y );
void response( int total1, int total2 );
inline void screen_clear();

int main()
{
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 )
else if ( pickAddSub == 2 )
subtraction();

// Asks the user if they want to carry on playing
again = repeat();
screen_clear();
}

return 0;
}

{
int number1, number2, usertotal, total;
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;
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;
}
}
}```
As always, comments are appreciated as I'm still new to this programming world.

5. Originally posted by cyberCLoWn
Code:
```/*
##############################################
## Addition & Subtraction Assistance Program
##############################################
##
## Written by    : CC
## 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 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()
{
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 )
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;
}

{
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;
}
}
}```

6. I know it's not a big deal but there is no need to store the random value for when you are deciding to do addition or subtraction. You could do it like:

Code:
```if( rand() % 2 ) // If 0 then evaluates to false, if 1 true
{
}
else
{
// subtraction..
}```
Also, what's with the 0 + ?

Code:
`number1 = ( 0 + rand() % 15 );`

7. You know, you buy all these fancy books to teach you stuff. Yes they do accomplish it, but this is how you really learn. Wow! Thanks to this community again. A really valuable resource to which one day I hope I can be an asset to. I'll get there!

I have changed the code as what you guys have said makes sense and looks better than the way I had implemented it.

Here's the code as it stands now:

Code:
```/*
##############################################
## Addition & Subtraction Assistance Program
##############################################
##
## Written by    : CC
## Comments      : A program designed to help children practise simple addition
##                 and subtraction sums.
##############################################
*/

#include <cstdlib>
using std::rand;
using std::srand;

#include <iomanip>
using std::setw;

#include <iostream>
using std::cout;
using std::cin;
using std::cerr;
using std::streamsize;
using std::max;
using std::numeric_limits;

// Prototypes
void subtraction();
char repeat();
void correct();
void response( int total1, int total2 );
inline void screen_clear();

int main()
{
char again = 'y';

cout << "Addition & Subtraction Assistance Program\n"
<< "-----------------------------------------\n";

srand( time( 0 ) );

while ( again == 'y' )
{
if ( rand() % 2 )
else
subtraction();

// Asks the user if they want to carry on playing
again = repeat();
screen_clear();
}

return 0;
}

{
int number1, number2, usertotal, total;

number1 = ( rand() % 15 ); // Gives number1 and number2 random values each time function runs
number2 = ( 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
{
correct();
}

response( usertotal, total ); // Random responses to correct or incorrect answers
}

void subtraction()
{
int number1, number2, usertotal, total;

number1 = ( rand() % 15 ); // Gives number1 and number2 random values each time function runs
number2 = ( 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
{
correct();
}
}
else
{
total = number2 - number1;

cout << '\n' << setw( 6 ) << number2 << "  -  " << number1 << "  =  ";

while ( !(cin >> usertotal) ) // Incase something non-valid is entered the program won't die
{
correct();
}
}

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 correct()
{
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;
}
}
}```
It would make more sense to make the argument a bool called correct and do the comparison outside.
Not too sure what you mean about that

Code:
```response( usertotal, total );

void response( int total1, int total2 )
{
int pick;

pick = ( 1 + rand() % 4 );

if ( total1 == total2 )```
Do
Code:
```response( usertotal == total );

void response( bool correct )
{
int pick;

pick = ( 1 + rand() % 4 );

if ( correct )```

9. response would also work better with a string lookup table.

Popular pages Recent additions