// Chapter 3 of C++ How to Program // Programming Challenge 9 (Solution) #include using std::cout; using std::endl; using std::cin; using std::fixed; #include using std::setw; using std::setprecision; #include double calculateCharges( double ); int main() { double hour; // hours parked for each car double currentCharge; // current parking charge double totalCharges = 0.0; // total charges double totalHours = 0.0; // total hours cout << "Enter the hours parked for three cars: "; for ( int i = 1; i <= 3; i++ ) { cin >> hour; totalHours += hour; if ( i == 1 ) { cout << setw( 5 ) << "Car" << setw( 15 ) << "Hours" << setw( 15 ) << "Charge\n"; } // end if totalCharges += ( currentCharge = calculateCharges( hour ) ); cout << fixed << setw( 3 ) << i << setw( 17 ) << setprecision( 1 ) << hour << setw( 15 ) << setprecision( 2 ) << currentCharge << "\n"; } // end for cout << setw( 7 ) << "TOTAL" << setw( 13 ) << setprecision( 1 ) << totalHours << setw( 15 ) << setprecision( 2 ) << totalCharges << endl; return 0; } // end main // calculate charges for hours parked double calculateCharges( double hours ) { double charge; if ( hours < 3.0 ) charge = 2.0; else if ( hours < 19.0 ) charge = 2.0 + .5 * ceil( hours - 3.0 ); else charge = 10.0; return charge; } // end function calculateCharges /************************************************************************** * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * * Hall. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * *************************************************************************/