Thread: Help please: program using functions

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    5

    Help please: program using functions

    Hello all. I'm trying to write a program to calculate the cost of car insurance based on these criteria:

    "Male drivers under the age of 21 pay a surcharge of 17%, while female drivers under the age of 21 pay a surcharge of 4% of the base charge. A driver with more than 3 tickets pays an extra $100.
    Write a program to calculate insurance rates based on this information. The information will be entered by the user and passed as arguments (actual parameters) to the function. The result will be returned to the main program by the function and displayed. Use a loop in the main program so that the user has the option of running the program as many times as desired."

    So far, I haven't figured out how to put the calculation into the function yet, so it looks real messy. The warning listed when I compile is "'Insurance_Cost' : recursive on all control paths, function will cause runtime stack overflow."

    Below are the parts I have written so far.

    Can someone please point out what I need to fix? Any help is appreciated. Thanks in advance!


    Code:
    #include <iostream>
    #include <iomanip>    // for use with setf(), setw() and setprecision()
    using namespace std;  // Used in place of std::cout, std::cin, etc.
    
    float Insurance_Cost(int age, int ticket_history, char gender, float car_value);
    
    //************************************************************************
    		
    float Insurance_Cost(int age, int ticket_history, char gender,
    		float car_value)
    {
    		float insCost;
    		insCost = Insurance_Cost(age, ticket_history, gender, car_value);
    	    return insCost;
    	}
    
    //************************************************************************
    
    	void DisplayResult(int age, int ticket_history, char gender, float car_value)
    	{
    		cout << "The insurance cost for a " << age << "year old driver with "
    		<< ticket_history << "is: $" 
    		<< Insurance_Cost(age, ticket_history, gender, car_value) << endl;
    
    	}
    
    //************************************************************************
    
    int main ()
    {   
        char gender;
        int age, ticket_history;
        float base_rate, car_value, ticket_fee, cost;
     
    	cout.setf(ios::fixed | ios::showpoint);
    	cout << setprecision(2);
    
        do
        {
          cout << "This program calculates the cost of car insurance." << endl
    		   << endl; 
    	  cout << "Please enter the value of the car: $"; 
          cin  >> car_value;
          
    	  cout << "Please enter the age of the driver: ";
    	  cin  >> age;
    	
    	  cout << "Please enter the gender of the driver (M or F): ";
    	  cin  >> gender;
    
    	  cout << "Please enter the number of tickets the driver has: ";
    	  cin  >> ticket_history;
    	    
    	  //<< Insurance_Cost(age, ticket_history, gender, car_value) << endl;
    	  
    	  //DisplayResult(age, ticket_history, gender, car_value);
    
          base_rate = car_value*0.06F;
    	 // insurance_cost = base_rate + ticket_fee;
    
    
    	  if (age < 21)
    	  {
    		  if (gender == 'M')
    		  {
    			  cost = base_rate*1.17F;
    				if (ticket_history > 3)
    				{
    					ticket_fee = 100.00F;
    				}
    				else if (ticket_history <= 3)
    				{
    					cost = base_rate;	
    				}
    		  }
    		  else if (gender == 'F')
    		  {
    			  cost = base_rate*1.04F;
    				if (ticket_history > 3)
    				{
    					ticket_fee = 100.00F;
    				}
    				else if (ticket_history <= 3)
    				{
    					cost = base_rate;	
    				}
    		  }		  
    	  }
    
    	  else if (age >= 21)
    	  {
    		  cost = base_rate;		  
    		  
    			if (ticket_history > 3)
    			{
    				ticket_fee = 100.00F;
    			}
    			else if (ticket_history <= 3)
    			{
    				cost = base_rate;	
    			}
    	  }
    	 
    	} while (car_value > 0);
    
    
        system ("pause");	
    	//system ("cls");
    
        return 0;
    }

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Well, first off your insurance cost function does not do anything.
    It is totally legit to have your function call it’s self again (it is called recursion) but it has to have an escape…that is, it has to evaluate something as ‘true’ to stop continually calling it’s self. But ALL your function does is call it’s self.

    What you want is to call your insurance_cost function, and pass it all the arguments.

    Also this:
    Code:
     ticket_fee = 100.00F;
    I think is wrong. I think you want to add that 100.00 to the cost. So just something like cost = cost + 100 in that if statement.

    Basically, you want put all your if statement stuff in your function, pass the function the data and do the math and such on it. Then you want to return the final cost.

    Also, if your data type is a floating point you do not have to put the F at the end of every number.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    Thank you for the pointers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program with Shapes using Virtual Functions
    By goron350 in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2005, 01:42 PM
  2. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. Help starting a C program with functions
    By jlmac2001 in forum C Programming
    Replies: 6
    Last Post: 10-12-2002, 02:43 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM