// Includes
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
usingnamespace std;
// Prototypes
int addition();
int subtraction();
int multiplication ();
int division();
int exit();
// Addition Function
int add()
{
srand ((unsignedint) time(NULL));
int num1 = 1 + rand() % 1000;
int num2 = 1 + rand() % 1000;
double answer;
double correct;
cout << "This is an addition math tutor.\n\n";
cout << " " << num1 << endl;
cout << " + " << num2 << endl;
cout << "\n\nEnter your answer: ";
cin >> answer;
correct = num1 + num2;
cout << "\n\n";
if ( answer != correct)
{
cout << "You got it wrong." << endl;
cout << "The correct answer is " << correct << endl;
}
elseif ( answer == correct)
{
cout << "Congratulations, your got it right." << endl;
}
_getch();
return main();
}
// Subtraction Function
int subtraction()
{
srand ((unsignedint) time(NULL));
int num1 = 1 + rand() % 1000;
int num2 = 1 + rand() % 1000;
double answer;
double correct;
cout << "This is an addition math tutor.\n\n";
cout << " " << num1 << endl;
cout << " - " << num2 << endl;
cout << "\n\nEnter your answer: ";
cin >> answer;
correct = num1 - num2;
cout << "\n\n";
if ( answer != correct)
{
cout << "You got it wrong." << endl;
cout << "The correct answer is " << correct << endl;
}
elseif ( answer == correct)
{
cout << "Congratulations, your got it right." << endl;
}
_getch();
return main();
}
// Multiplication Function
int multiplication()
{
srand ((unsignedint) time(NULL));
int num1 = 1 + rand() % 1000;
int num2 = 1 + rand() % 1000;
double answer;
double correct;
cout << "This is an addition math tutor.\n\n";
cout << " " << num1 << endl;
cout << " * " << num2 << endl;
cout << "\n\nEnter your answer: ";
cin >> answer;
correct = num1 * num2;
cout << "\n\n";
if ( answer != correct)
{
cout << "You got it wrong." << endl;
cout << "The correct answer is " << correct << endl;
}
elseif ( answer == correct)
{
cout << "Congratulations, your got it right." << endl;
}
_getch();
return main();
}
// Division Function
int division()
{
srand ((unsignedint) time(NULL));
int num1 = 1 + rand() % 1000;
int num2 = 1 + rand() % 1000;
double answer;
double correct;
cout << "This is an addition math tutor.\n\n";
cout << " " << num1 << endl;
cout << " / " << num2 << endl;
cout << "\n\nEnter your answer: ";
cin >> answer;
correct = num1 / num2;
cout << "\n\n";
if ( answer != correct)
{
cout << "You got it wrong." << endl;
cout << "The correct answer is " << correct << endl;
}
elseif ( answer == correct)
{
cout << "Congratulations, your got it right." << endl;
}
_getch();
return main();
}
// Exit Function
int exit()
{
cout << "Progam is exiting.";
_getch();
return (0);
}
// Main Function
int main()
{
int choice;
cout << "This program is a math tutor.\n\n";
do
{
cout << "-----------------------------------" << endl;
cout << "| |" << endl;
cout << "| |" << endl;
cout << "| 1. Addition |" << endl;
cout << "| 2. Subtraction |" << endl;
cout << "| 3. Multiplication |" << endl;
cout << "| 4. Division |" << endl;
cout << "| 5. Exit |" << endl;
cout << "| |" << endl;
cout << "| |" << endl;
cout << "-----------------------------------" << endl;
cout << "\n\nEnter a choice: ";
cin >> choice;
while ( choice < 1 || choice > 5)
{
cout << "Please enter 1, 2, 3, 4, or 5: ";
cin >> choice;
}
switch (choice)
{
case 1: addition(); break;
case 2: subtraction(); break;
case 3: multiplication(); break;
case 4: division(); break;
case 5: exit(); break;
}
} while (choice != 5);
_getch();
return 0;
}
How can i make the addition, subtraction, multiplication and division function make a return to main function so the user when done can repick a choice.
You declared the functions such a way that they shall return a value of type int. So each function shall contain a return statement with an expression of type int.
But if to follow the logic of the functions it is better if they wouldl return nothing that is if they have return type void
actually i do have 1 more question, why is the right side of my box in the main function all messed up, its off alignment. Even when i run the program its off alignment.
As you see in the very beginning of the line there are numbers 9 that correspond to the TAB symbol. 32 is the code of the space. You should use spaces instead of tabs.