I'm writting a program for my class. It involves validating the users input using a function that will return a value of type bool. My problem is that it will only return the false value. I hope someone can point me in the right direction and explain where I made my mistake. Thanks.
Code:
#include <iostream>
using namespace std;
//function protypes
double conversion(int whole, int num, int dem);
bool validate (float shares, int whole, int num, int dem);
int main()
{
//These are the variables that will be used
float num_shares;
int whole_dol, num_dol, dem_dol;
double total_price, value_of_hold;
char repeat;
bool val;
do
{
do
{
//This inputs the number of stocks, the whole price of one stock
//and the numerator and the denominator of the fraction of the
//price of a share
cout << "\nPlease enter the number of shares you own.\n";
cin >> num_shares;
cout << "\nPlease enter the whole dollar component of the price "
<< "of one stock\n";
cin >> whole_dol;
cout << "\nPlease enter the fraction component of the"
<< " price, seperating\nthe numerator and denominator by"
<< " a space\n (e.g. 7 8 would be equal to 7/8)";
cin >> num_dol;
cin >> dem_dol;
//This validates the user's inputs
val = validate (num_shares, whole_dol, num_dol, dem_dol);
}while (bool(val == false));
//This converts the price into one number of type double
total_price = conversion(whole_dol, num_dol, dem_dol);
//This calculates the total worth of all the stocks the user has
value_of_hold = total_price * num_shares;
// This outputs the total value of the user's stock holdings
cout << "\nThe total value of your "<< num_shares <<" stock(s) ";
cout.setf(ios::fixed); // This rounds the number
cout.setf(ios::showpoint);// of decimal places to 2 places
cout.precision(2);
cout << "is "<< value_of_hold << " dollars\n";
//This asks the user if they wish to repeat the calculations
cout << "\nWould you like to find the total value of another stock?\n";
cout << "\n(enter y for yes or n for no)\n";
cin >> repeat;
}while(repeat == 'y');
return 0;
}
//////////////conversion function
double conversion(int whole, int num, int dem)
{
double total_price, frac_dol;
//This converts the numerator and denominator to
//a fraction of a dollar
frac_dol = static_cast<double>(num)/dem;
// This calculates the total price of one stock
total_price = whole + frac_dol;
return total_price;
}
///////////////validation function
bool validate (float shares, int whole, int num, int dem)
{
bool error, no_error;
bool (error = false);
bool (no_error = true);
//This checks to see if the inputs are valid
if (shares > 0 || whole >= 0 || num >= 0 || dem == 2
|| dem == 4 || dem == 8 || num < dem)
{
//These are the outputs for the appropriate invalid input
if (shares <= 0 )
{
cout << "\nYou have put in an invalid input.\n"
<< "\nThe number of shares has to be greater than zero\n";
}
if (whole < 0)
{
cout << "\n You have put in an invalid input\n";
cout << "\nThe whole number of the price must be a positive\n";
cout << "non-negative number\n";
}
if (num < 0)
{
cout << "\nYou have put in an invaid input\n";
cout << "\nThe numerator of the price must be a positive\n";
cout << "non-negative number";
}
if ((dem % 2 !=0) || (dem > 8) || (dem < 0))
{
cout << "\nYou have put in an invalid input\n";
cout << "\nThe denominator must be 2, 4 or 8\n";
}
if (num > dem)
{
cout << "\nYou have put in an invalid input\n";
cout << "\nThe fraction cannot be improper\n";
}
return bool (error);
}
else
return bool (no_error);
}