So here is the story in short I have a young family member who struggles with maths so I wanted to try and make this as fun as possible. Below are the C++ project files. At the moment it’s simple maths however I will be making settings that will allow you the parent to set the difficulty. I will also be implementing an automatic difficulty setting which is linked to the score. The plan is once the score reaches 20 it will increase a level.

Now I am aware that no child wants to answer questions in a terminal so I will be creating a UI for it over the coming weeks. Anyway as promised here is the code:

## allHeaders.h

```
/*
* File: allGameModes.h
* Author: Stephen Wilde
* Website: https://stephenwilde.net
*
* Created on 23 May 2023, 14:53
*/
#ifndef ALLGAMEMODES_H
#define ALLGAMEMODES_H
#include <iostream>
#include <cstdlib>
#include <ctime>
int main();
int generateRandomNumber();
void multiplication();
void selection();
void playAgain();
void clearConsole();
void subtraction();
void addition();
void division();
float generateRandomNumberDivision();
#endif /* ALLGAMEMODES_H */
```

## main.cpp

```
/*
* Author: Stephen Wilde
* Website: https://stephenwilde.net
*/
#include "allHeaders.h"
int main() {
selection();
return 0;
}
```

## addition.cpp

```
/*
* Author: Stephen Wilde
* Website: https://stephenwilde.net
*/
#include "allHeaders.h"
void addition()
{
srand(static_cast<unsigned int>(time(0)));
int score = 0;
int chances = 2;
while (true) {
int number1 = generateRandomNumber();
int number2 = generateRandomNumber();
int answer = number1 + number2;
std::cout << "What is " << number1 << " + " << number2 << " = " << std::endl;
int guess;
std::cin >> guess;
if (guess == answer && chances >= 0) {
std::cout << "3[92mWell done! " << number1 << " + " << number2 << " does indeed equal " << answer << "3[0m" << std::endl;
score++;
std::cout << "Current Score: " << score << std::endl;
std::cout << std::endl;
} else if (chances > 0) {
std::cout << "3[91mThat's not right. " << number1 << " + " << number2 << " = " << answer << ", not " << guess << "3[0m" << std::endl;
chances--;
std::cout << "Chances remaining: " << chances + 1 << std::endl;
} else {
std::cout << "3[92mFinal score: " << score << "3[0m" << std::endl;
playAgain();
}
}
}
```

## division.cpp

```
/*
* Author: Stephen Wilde
* Website: https://stephenwilde.net
*/
#include "allHeaders.h"
void division()
{
srand(static_cast<unsigned int>(time(0)));
int score = 0;
int chances = 2;
while (true) {
float number1 = generateRandomNumberDivision();
float number2 = generateRandomNumberDivision();
float answer = number1 / number2;
if (answer == 0.0f) {
number1 = generateRandomNumberDivision();
number2 = generateRandomNumberDivision();
answer = number1 / number2;
}
std::cout << "What is " << number1 << " / " << number2 << " = ";
float guess;
std::cin >> guess;
if (std::abs(guess - answer) < 0.0001f && chances >= 0) {
std::cout << "3[92mWell done! " << number1 << " / " << number2 << " does indeed equal " << answer << "3[0m" << std::endl;
score++;
std::cout << "Current Score: " << score << std::endl;
std::cout << std::endl;
} else if (chances > 0) {
std::cout << "3[91mThat's not right. " << number1 << " / " << number2 << " = " << answer << ", not " << guess << "3[0m" << std::endl;
chances--;
std::cout << "Chances remaining: " << chances + 1 << std::endl;
} else {
std::cout << "3[92mFinal score: " << score << "3[0m" << std::endl;
playAgain();
}
}
}
```

## multiplication.cpp

```
/*
* Author: Stephen Wilde
* Website: https://stephenwilde.net
*/
#include "allHeaders.h"
void multiplication()
{
srand(static_cast<unsigned int>(time(0)));
int score = 0;
int chances = 2;
while (true) {
int number1 = generateRandomNumber();
int number2 = generateRandomNumber();
int answer = number1 * number2;
std::cout << "What is " << number1 << " * " << number2 << " = " << std::endl;
int guess;
std::cin >> guess;
if (guess == answer && chances >= 0) {
std::cout << "3[92mWell done! " << number1 << " * " << number2 << " does indeed equal " << answer << "3[0m" << std::endl;
score++;
std::cout << "Current Score: " << score << std::endl;
std::cout << std::endl;
} else if (chances > 0) {
std::cout << "3[91mThat's not right. " << number1 << " * " << number2 << " = " << answer << ", not " << guess << "3[0m" << std::endl;
chances--;
std::cout << "Chances remaining: " << chances + 1 << std::endl;
} else {
std::cout << "3[92mFinal score: " << score << "3[0m" << std::endl;
playAgain();
}
}
}
```

## subtraction.cpp

```
/*
* Author: Stephen Wilde
* Website: https://stephenwilde.net
*/
#include "allHeaders.h"
void subtraction()
{
srand(static_cast<unsigned int>(time(0)));
int score = 0;
int chances = 2;
while (true) {
int number1 = generateRandomNumber();
int number2 = generateRandomNumber();
int answer = number1 - number2;
std::cout << "What is " << number1 << " - " << number2 << " = " << std::endl;
int guess;
std::cin >> guess;
if (guess == answer && chances >= 0) {
std::cout << "3[92mWell done! " << number1 << " - " << number2 << " does indeed equal " << answer << "3[0m" << std::endl;
score++;
std::cout << "Current Score: " << score << std::endl;
std::cout << std::endl;
} else if (chances > 0) {
std::cout << "3[91mThat's not right. " << number1 << " - " << number2 << " = " << answer << ", not " << guess << "3[0m" << std::endl;
chances--;
std::cout << "Chances remaining: " << chances + 1 << std::endl;
} else {
std::cout << "3[92mFinal score: " << score << "3[0m" << std::endl;
playAgain();
}
}
}
```

## playAgain.cpp

```
/*
* Author: Stephen Wilde
* Website: https://stephenwilde.net
*/
#include "allHeaders.h"
void playAgain(){
std::cout << "Type Y to play again or N to exit: ";
std::string YN;
std::cin >> YN;
if (YN == "y" || YN == "Y")
{
clearConsole();
selection();
}
else
{
clearConsole();
std::cout << "GoodBye for now!";
exit(0);
}
}
```

## selection.cpp

```
/*
* Author: Stephen Wilde
* Website: https://stephenwilde.net
*/
#include "allHeaders.h"
void selection()
{
int choice;
puts("Please select an option \nOption 0: Exit \nOption 1: Addition \nOption 2: Subtraction \nOption 3: Division \nOption 4: Multiplication \n");
std::cin >> choice;
switch (choice) {
case 0:
clearConsole();
std::cout << "GoodBye for now!";
exit(0);
case 1:
clearConsole();
std::cout << "You selected Option 1.\n";
addition();
break;
case 2:
clearConsole();
std::cout << "You selected Option 2.\n";
subtraction();
break;
case 3:
clearConsole();
std::cout << "You selected Option 3.\n";
division();
break;
case 4:
clearConsole();
std::cout << "You selected Option 4.\n";
multiplication();
break;
default:
std::cout << "Oops that's not a valid selection.\n";
selection();
}
}
```

## settings.cpp

```
/*
* Author: Stephen Wilde
* Website: https://stephenwilde.net
*/
#include "allHeaders.h"
void clearConsole() {
std::cout << "\x1B[2J\x1B[H";
}
int generateRandomNumber() {
return rand() % 13;
}
float generateRandomNumberDivision()
{
//I made the division different to fix a bug that exits the program after typing either - or .
return static_cast<float>(rand() % 10 + 1); // Generate a random number between 1 and 10
}
```

If you want to download this project it’s now on GitHub feel free to use and modify it however you, please!