Hello, I have a problem which goes like this.
I need to be able to design a simple class, which i have done, and it must represent any numeric value, which includes a decimal and negative value. So i need to overload the operators, because the '%' will make an error if introduced a double.
Type in explanations as simple as possible.
Someone help!!
//****************************************************
// This Program Demonstrates a simple class structure
//****************************************************
#include <iostream>
using namespace std;
class game
{
private:
int number_one;
int number_two;
public:
void set_numbers();
int validation();
void addition();
void subtraction();
void multiplication();
void division();
void modulus();
};
void game::set_numbers() // Give Values Prompts For User Input
{
cout << "Please Enter the 1st number" << endl;
number_one = validation();
cout << "Please Enter the 2nd number" << endl;
number_two = validation();
cout << "\n";
};
int game::validation() // Validates Input for set_numbers
{
char input[20];
int number;
bool valid = false;
int period = 0; // Countes Periods
int negative = 0; // Counts Negative Symbols
do
{
period = 0;
negative = 0;
valid = false;
cin >> input;
for (int i = 0; i<strlen(input); i++)
{
if (!isdigit(input[i]) && (input[i] != '.') && (input[i] != '-'))
{
cout << "Error! Please Re-Enter Value That has no letters" << endl;
valid = true;
fflush(stdin);
break;
}
if (input [i] == '.')
{
period++;
}
if (input[i] == '-')
{
negative++;
}
if (period >= 2 || negative >=2)
{
cout << "Do not enter a number with more than one Period (.) or Negative Symbol (-)" << endl;
valid = true;
fflush(stdin);
break;
}
}
} while (valid == true);
number = atoi(input);
return number;
};
void game::addition()
{
set_numbers();
cout << "Addition: " << number_one << " + " << number_two << " = " << (number_one+number_two) << endl;
};
void game::subtraction()
{
cout << "Subtraction: " << number_one << " - " << number_two << " = " << (number_one-number_two) << endl;
};
void game::multiplication()
{
cout << "Multiplication: " << number_one << " * " << number_two << " = " << (number_one * number_two) << endl;
};
void game::division()
{
cout << "Division " << number_one << " / " << number_two << " = " << (number_one / number_two) << endl;
};
void game::modulus()
{
cout << "Modulus: " << number_one << " % " << number_two << " = " << (number_one % number_two) << endl;
cin.get();
};
int main() // Main
{
game something; // Creates Object 'something' of class "game"
something.addition(); // Access Addition
something.subtraction(); // Access Subtraction
something.multiplication(); // Access Multiplication
something.division(); // Access Division
something.modulus(); // Access Modulus
cin.get();
fflush(stdin);
return 0;
}