fork download
  1. // Torrez, Elaine CS1A Chapter 3 P. 144, #7
  2.  
  3. /******************************************************************************************
  4.  *
  5.  * CALCULATE CALORIES CONSUMED
  6.  *
  7.  * --------------------------------------------------------------------------------
  8.  * This program asks the user to enter how many cookies he or she ate.
  9.  * Each bag holds 40 cookies, with 10 servings per bag, and each serving equals
  10.  * 300 calories. The program calculates and displays the total calories consumed.
  11.  * --------------------------------------------------------------------------------
  12.  *
  13.  * INPUT
  14.  * cookiesEaten : Number of cookies eaten by the user
  15.  *
  16.  * OUTPUT
  17.  * totalCalories : Total calories consumed
  18.  *
  19.  *******************************************************************************************/
  20.  
  21. #include <iostream>
  22. #include <iomanip>
  23. using namespace std;
  24.  
  25. int main ()
  26. {
  27. // Constants
  28. const int COOKIES_PER_BAG = 40; // Number of cookies in one bag
  29. const int SERVINGS_PER_BAG = 10; // Servings per bag
  30. const int CALORIES_PER_SERVING = 300; // Calories per serving
  31.  
  32. // Variables (ABC order)
  33. int cookiesEaten; // INPUT number of cookies eaten
  34. double totalCalories; // OUTPUT total calories consumed
  35.  
  36. // Ask user for input
  37. cout << "Enter the number of cookies you ate: ";
  38. cin >> cookiesEaten;
  39.  
  40. // Calculate calories per cookie
  41. double caloriesPerCookie = static_cast<double>(CALORIES_PER_SERVING) /
  42. (COOKIES_PER_BAG / SERVINGS_PER_BAG);
  43.  
  44. // Calculate total calories consumed
  45. totalCalories = cookiesEaten * caloriesPerCookie;
  46.  
  47. // Display result
  48. cout << fixed << setprecision(0); // Display whole number calories
  49. cout << "\nYou consumed " << totalCalories << " calories." << endl;
  50.  
  51. return 0;
  52. }
  53.  
Success #stdin #stdout 0s 5320KB
stdin
10
stdout
Enter the number of cookies you ate: 
You consumed 750 calories.