This is the class declaration for FineCalculator ( int courtFee), I am attempting to calculate a speeding fine using the member function getFine( zone, speedLimit, actualSpeed). I would like all the input/output activities as shown done in main and the continued program execution "while( zone != 0 ). The delima is that speedFine does not calculate in the cout << statement in main. I thought that creating the myfc.getFine(zone, ......) object and assigning it to speedFine would make the function call. Any guidance and direction would be greatly appreciated.
/* Member functions of class FineCalculator that determines the
dollar amount that is accessed for exceeding the speed limit based
on different zones, a court fee is also included in the fine **/
#include <iostream>
using namespace std ;
#include "FineCalculator.h" // include definition of class FineCalculator
FineCalculator :: FineCalculator ( int courtFee )
{
setFeeAmount (courtFee );
fine = 0 ;
}
int FineCalculator :: getFeeAmount()
{
return feeAmount ;
}
// function that claculates the fine based on speed limit and speed in excess of speed limit
int FineCalculator :: getFine ( int a, int b, int c )
{
int speedLimit = 0 ; // contains the speed limit
int actualSpeed = 0 ; // contains the speed of the vehicle
int zone = 0 ; // contains the zone to calculate infraction amount
int speedFine = 0 ; // speed in excess of variable speed limit
int sfine = 0 ;
do // continue loop until while condition is false
{
int exceedSpeed = speedLimit - actualSpeed ;
if ( zone == 0 )
cout <<"Goodbye" << endl; // exit program
system("Pause") ;
} // end class FineCalculator
/* Creates and manipulates a FineCalculator object: Calculates
the fine amount for speeding**/
#include <string>
#include <iostream>
#include "FineCalculator.h"
; using namespace std ;
// function main begins execution of program
int main()
{
int courtFee = 0 ;
FineCalculator myfc(courtFee) ;
int speedLimit = 0 ; // contains the speed limit
int actualSpeed = 0 ; // contains the speed of the vehicle
int speedFine = 0 ; // speed in excess of variable speed limit
int zone = 0 ; // speed in excess of variable speed limit
int fine = 0 ; // speeding fine
cout <<"Enter the court fee of 62: " ;
cin >> courtFee ;
myfc.setFeeAmount( courtFee) ;
cout <<"Enter the number that corresponds with the violation type: "<< endl ;
cout << "(1) for Regular Violation "<< endl ;
cout << "(2) for Highway Violation" << endl ;
cout << "(3) for Residential Violation: " ;
cin >> zone ;
cout <<"Enter the speed limit: " ;
cin >> speedLimit ;
cout << "Enter the vehicle speed: " ;
cin >> actualSpeed ;
speedFine = myfc.getFine( zone, speedLimit, actualSpeed ) ;
cout <<"The total fine for this violation is: "<< "$" << speedFine <<endl ;
cout <<"\nEnter a 1 to enter another ticket or 0 to quit the fine claculator: " ;
cin >> zone ;
cout << endl ;
a) There is no reason to use "getFeeAmount()" inside a member function of your class. A class can always find its own members, thus you can directly access the amount.
b) getFine() will run forever...but only once. There's an unconditional return statement inside the while loop, thus it will end the function at the first iteration. And that's good, because if not, it would run forever, as zone will never be zero unless it was zero to begin with. I think you meant to use an IF statement rather than a do while loop.
c) If, in getFine(), zone is indeed 0, then that function does not return a value. Which is bad, because you declared that it will.
d) Same goes for your main(). It should return an int, but it doesn't.
e) There might be other mistakes, but fix those first, then check it. If it still doesn't work, post the new code INSIDE CODE TAGS.
a) There is no reason to use "getFeeAmount()" inside a member function of your class
That's not really true. All the same reasons for using a getter outside the class apply to using one inside the class. Just because you can access the variable directly doesn't mean it's benefitial to.
like Gaminic(176) said use an if loop to check if zone is not equal to zero.then the next problem with the code is in getFine function.
in the 'int FineCalculator :: getFine ( int a, int b, int c )' function you are taking three parameters a,b,c of int type.
the parameters zone,speedLimit and actualspeed are passed from main to the getfine function ''speedFine = myfc.getFine( zone, speedLimit, actualSpeed ) ''
the parameters a,b,c collect them.
now when these parameters are passed to the getfine function the variables declared in the getfine function should be assigned these values rather than initialising them to 0, the calculated value that is speedfine should be returned back rather than returning sfine which is already 0.