Game Programming



Visit Base Classics - Gaming Chairs

 
game programming
Game Programming :: Programming :: General Programming :: Code Examples :: [C++]Implementing a for loop - View Topic
Topic Rating: *****
Printable View
Andy
Administrator
*****

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

Reputation: 0%

[C++]Implementing a for loop (11th Jan 08 at 1:55am UTC)
Level: Beginner

This code asks you for five item prices using a for loop, and at the same time, it is adding your values together to come up with a subtotal. The program then exits the for loop, displays the subtotal, and then calculates and displays the tax, and total cost.

Note: The for loop is used when there is a fixed number of loops to be performed.


Code:
 
  1. //Implementing a for loop
  2.  
  3. #include<iostream>
  4.  
  5.  
  6. inat main()
  7. {
  8. //The total cost of a 5 item purchase
  9.  
  10. short item_number; //used to end loop after 5th item price entry
  11. float item_price = 0.0;
  12. const float tax_rate = 0.08;
  13. float subtotal = 0.0; //sum of all five items
  14. float tax, grand_total = 0.0; //sales tax, and total bill
  15.  
  16.  
  17. cout << "Enter the prices of your five items." << endl <<
  18. "(Make sure to press Enter after each value input.)" << endl;
  19.  
  20. cout << endl;
  21.  
  22. for ( item_number = 1; item_number <= 5; item_number++ )
  23. {
  24. cout << "Item #" << item_number << " = $";
  25.  
  26. cin >> item_price;
  27.  
  28. subtotal += item_price;
  29.  
  30. cout << endl;
  31. }
  32.  
  33. cout << "Your subtotal = $" << subtotal << endl;
  34.  
  35. tax = subtotal * tax_rate;
  36.  
  37. cout << "Your total tax = $" << tax << endl;
  38.  
  39. grand_total = subtotal + tax;
  40.  
  41. cout << "Your grand total = $" << grand_total << endl;
  42.  
  43. }// end of main
  44.  
 
 Printable View

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