Thread: help with type bool functions

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    5

    help with type bool functions

    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);
    
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    bool is a type just like int or char. You only need to use the keyword bool when declaring the variable -- not when using it.

    For instance:

    Code:
    bool somebool;
    somebool = true;
    somebool = false;
    if(somebool)
      doSomething();
    return somebool;

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Follow the logic path of your code and see where it leads you...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    5

    thank you

    Thanks for pointing me in th right direction. If firgured out both my syntax and logic errors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. How do I play an MP3?
    By Hunter2 in forum Windows Programming
    Replies: 28
    Last Post: 05-20-2002, 08:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM