fork download
  1. //Maxwell Brewer CS1A Chapter 3, P. 143, #1
  2. //
  3. /***************************************************************
  4.  *
  5.  * CALCULATE FUEL ECONOMY
  6.  * _____________________________________________________________
  7.  *
  8.  * This program will receive input from the user
  9.  * and output a calculation of fuel economy. The
  10.  * user will input the vehicle fuel tank capacity
  11.  * and total distance traveled.
  12.  * _____________________________________________
  13.  * INPUT
  14.  *
  15.  * Fuel tank capacity
  16.  * Total distance traveled on full tank.
  17.  *
  18.  * OUTPUT
  19.  *
  20.  * Average fuel consumption
  21.  *
  22.  ***************************************************************/
  23.  
  24. #include <iostream>
  25. #include <iomanip>
  26. #include <string>
  27.  
  28. using namespace std;
  29.  
  30. int main() {
  31.  
  32.  
  33. // Initialization
  34. float galFuel; //INPUT - Fuel tank capacity
  35. float distanceMi; //INPUT - Total distance on a full tank
  36. float averageFuel; //OUTPUT - Average distance per gallon
  37.  
  38. const char msgGal[] = "Enter total gallons per tank: ";
  39. const char msgDis[] = "Enter total distance per tank: ";
  40. const char msgVeh[] = "Your vehicle can travel";
  41. const char msgMpg[] = " miles per gallon.";
  42.  
  43. // Display data
  44. cout << msgGal << endl;
  45. cin >> galFuel;
  46. cout << msgDis << endl;
  47. cin >> distanceMi ;
  48.  
  49. // Output
  50. averageFuel = distanceMi / galFuel;
  51.  
  52. // Display Output
  53. cout << setprecision(2) << fixed;
  54. cout << msgVeh << setw(6) << averageFuel << msgMpg;
  55.  
  56. return 0;
  57. }
Success #stdin #stdout 0.01s 5280KB
stdin
14
400
stdout
Enter total gallons per tank: 
Enter total distance per tank: 
Your vehicle can travel 28.57 miles per gallon.