Game Programming



Visit Base Classics - Gaming Chairs

 
game programming
Game Programming :: Programming :: General Programming :: Code Examples :: [C++] Variable Constants - View Topic
Topic Rating: *****
Printable View
Andy
Administrator
*****

Posts: 40
Status: Offline
Gender: Male
Joined:  
pm

Reputation: 0%

[C++] Variable Constants (11th Jan 08 at 2:03am UTC)
Level: Beginner (no experience)

This program just shows you how you define constant variables that don't change in value in your program. In this program, they are below the using namespace std statement.

What this program does is calculate the tax, tip, and final cost of a meal.As you can see, the sales tax rate and tip rate do not change.

Code:
 
  1. //*************************************************************************************
  2. //
  3. // restaurantTip.cpp
  4. //
  5. // A program to calculate the sales tax, tip, and final cost of a meal in a restaurant
  6. //
  7. //*************************************************************************************
  8.  
  9. #include <iostream>
  10. #include <iomanip>
  11.  
  12. using namespace std;
  13.  
  14. const double SALES_TAX_RATE = 0.0875,
  15. TIP_RATE = 0.15;
  16.  
  17. int main()
  18. {
  19. double mealCost,
  20. tax,
  21. tip,
  22. totalCost;
  23.  
  24. cout << "Enter cost of meal: $";
  25. cin >> mealCost;
  26.  
  27. cout <<"\n\n";
  28.  
  29. tax = mealCost * SALES_TAX_RATE;
  30.  
  31. tip = mealCost * TIP_RATE;
  32.  
  33. totalCost = mealCost + tax + tip;
  34.  
  35. cout << fixed << setprecision(2);
  36. cout << "Meal Cost = $" << mealCost << "\n\n" << "Sales Tax = $" << tax << "\n\n"
  37. << "Tip = $" << tip << "\n\n" << "Total Cost = $" << totalCost << "\n\n";
  38.  
  39. return(0);
  40. }
  41.  
 
 Printable View

All times are GMT+0 :: The current time is 7:58am
Page generated in 0.1737 seconds
Purchase Ad-Free Credits
This Forum is Powered By vForums (v2.4)
Create a Forum for Free | Find Forums