Thread: Such a simple problem

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Such a simple problem

    Title says its all!

    Cannot figure out why when I print "totalFee" im getting 0.00 as output although I am passing it by reference to the function. At no point can I see why the variable is not adding the "charge" value. I am not getting errors or warnings. It looks like quite a small mistake im making.

    Can anyone see what I am doing wrong?

    Code:
    #include <iostream>
    #include <iomanip>
    
    // function prototype
    double calculateCharge ( double, double& );
    
    // main function - driver //////////////////////////////////////////////////////
    //
    int main ( void )
    {
    	std::cout << std::setprecision ( 2 ) << std::fixed;
    
    	double time1 = 0,
    	           time2 = 0,
    	           time3 = 0,
    	           totalTime = 0,
    	           totalFee = 0;
    
    	std::cout << "Enter stay times for three cars: ";
    	std::cin >> time1 >> time2 >> time3;
    
    	totalTime = time1 + time2 + time3;
    
    	std::cout << "\nCAR\tSTAY\tFEE\n\n"
    			  << " 1\t" << time1 << "\t" << calculateCharge ( time1, totalFee )
    			  << "\n 2\t" << time2 << "\t" << calculateCharge ( time2, totalFee )
    			  << "\n 3\t" << time3 << "\t" << calculateCharge ( time3, totalFee )
    			  << "\n\nTotal Time: " << totalTime
    			  << "\nTotal Fee: " << totalFee << std::endl;
    
        std::cin.get(); // freeze console output window
    	std::cin.ignore();
    
        return 0; // return value from int main
    }
    
    // function to calculate the charge
    // for each car stayed
    double calculateCharge ( double x, double &rFee )
    {
    	double charge = 0;
    
    	if ( x <= 3.00 )
    	{
    		charge = 2.00;
    		rFee += charge;
    	}
    
    	if ( x > 3.00 )
    	{
    		double timeOverStandard = 0,
    			   additionalCharge = 0;
    
    		timeOverStandard = x - 3.00;
    		additionalCharge = timeOverStandard * 0.5;
    
    		charge = 2.00 + additionalCharge;
    		rFee += charge;
    	}
    
    	if ( x == 24.00 )
    	{
    		charge = 10.00;
    		rFee += charge;
    	}
    
    	return charge;
    }
    Double Helix STL

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Elementary my dear Watson. One possible fix... change:
    Code:
    	std::cout << "\nCAR\tSTAY\tFEE\n\n"
    			  << " 1\t" << time1 << "\t" << calculateCharge ( time1, totalFee )
    			  << "\n 2\t" << time2 << "\t" << calculateCharge ( time2, totalFee )
    			  << "\n 3\t" << time3 << "\t" << calculateCharge ( time3, totalFee )
    			  << "\n\nTotal Time: " << totalTime
    			  << "\nTotal Fee: " << totalFee << std::endl;
    To:
    Code:
    std::cout << "\nCAR\tSTAY\tFEE\n\n"
             << " 1\t" << time1 << "\t" << calculateCharge ( time1, totalFee )
             << "\n 2\t" << time2 << "\t" << calculateCharge ( time2, totalFee )
             << "\n 3\t" << time3 << "\t" << calculateCharge ( time3, totalFee )
             << "\n\nTotal Time: " << totalTime;
    std::cout << "\nTotal Fee: " << totalFee << std::endl;
    I'll let you think about the "why".
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Good one.
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM