468,513 Members | 1,506 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

SetPrecision and ShowPoint

I'm not 100% sure whats even going on, but I'm working on a C++ assignment for school, and its a road trip program

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. char const METRIC = 'M';              // Input read as metric
  6. char const ENGLISH = 'E';             // Input read as english
  7. char const QUIT = 'Q';                // Input read to quit program
  8. char const QUIT2 = 'q';               // Input read to quit program
  9. float const MINDISTANCE = 16.00;      // Minimum value for tripdistance
  10. float const MAXDISTANCE = 2800.00;    // Maximum value for tripdistance
  11. float const MINPRICE = 0.75;          // Minimum value for volume
  12. float const MAXPRICE = 5.00;          // Maximum value for volume
  13. float const GASMINIMUM = 0.00;        // Minimum value for gas
  14. float const LITPERGAL = 3.79;         // Conversion factor for English <-> Metric (lit to gal)
  15. float const KILPERMIL = 1.61;         // Conversion factor for English <-> Metric (kil to mil)
  16.  
  17. int main()
  18. {
  19.    char start;                   // Input to start program; metric, english, or quit
  20.    float volume;                 // Input for gas volume
  21.    float price;                  // Input for gas price
  22.    float distance;               // Input for total trip distance
  23.    float volumeM;                // Input stored conversion: volume from English to Metric
  24.    float volumeE;                // Input stored conversion: volume from Metric to English
  25.    float priceM;                 // Input stored conversion: price from English to Metric
  26.    float priceE;                 // Input stored conversion: price from Metric to English
  27.    float tripM;                  // Input stored conversion: trip distance from English to Metric
  28.    float tripE;                  // Input stored conversion: trip distance from Metric to English
  29.    float milpergal;              // Input stored computation for ML/Gal
  30.    float kmperliter;             // Input stored computation for KM/Liter
  31.    float tripcost;               // Input stored for total cost of trip
  32.  
  33.    cout << "The program calculates mileage and other statistics" << endl
  34.         << "for a road trip in both Metric and English units." 
  35.         << endl << endl;
  36.  
  37.    cout << "Enter the type of measurement units" << endl << "'M' for Metric"
  38.         << " or 'E' for English or" << endl << "'Q' or 'q' for Quit: ";
  39.    cin  >> start;
  40.  
  41.    while ( start != QUIT && start != QUIT2 )
  42.    { 
  43.       if ( start == METRIC || start == ENGLISH )
  44.      {
  45.          cout << endl << "Please enter the volume of gas consumed: ";
  46.          cin  >> volume;
  47.          while ( volume <= GASMINIMUM )
  48.          {
  49.             cout << "Please reenter the volume gas consumed" 
  50.                  << endl << "(it must be greater than 0.00): ";
  51.             cin  >> volume;
  52.          }
  53.          cout << endl << "Please enter the price of gas per unit volume: ";
  54.          cin  >> price;
  55.          while ( price <= MINPRICE || price >= MAXPRICE )
  56.          {
  57.             cout << "Please reenter Fuel Price (it must be higher" 
  58.                  << endl << "than 0.75 and lower than 5.00) :";
  59.             cin  >> price;
  60.          }
  61.          cout << endl << "Please enter the trip distance: ";
  62.          cin  >> distance;
  63.          while ( distance < MINDISTANCE || distance >= MAXDISTANCE )
  64.          {
  65.             cout << "Please reenter a trip distance greater than"
  66.                  << endl << "or equal to 16.00 and less than 2800.00: ";
  67.             cin  >> distance;
  68.          }
  69.          cout << endl << setw(10) << "MILES" << setw(10) << "KMETERS" 
  70.               << setw(10) << "$/Gal" << setw(10) << "$/Lit" 
  71.               << setw(10) << "ML/Gal" << setw(10) << "KM/Liter" 
  72.               << setw(12) << "Trip Cost" << endl;
  73.          tripcost = price * volume;
  74.          if ( start == METRIC )
  75.          {
  76.            tripE = distance / KILPERMIL;
  77.            priceE = price * LITPERGAL;
  78.            volumeE = volume / LITPERGAL;
  79.            kmperliter = distance / volume;
  80.            milpergal = tripE / volumeE;
  81.            cout << showpoint
  82.                 << setw(10) << setprecision(3) << tripE 
  83.                 << setw(10) << setprecision(3) << distance
  84.                 << setw(10) << setprecision(2) << priceE
  85.                 << setw(10) << setprecision(2) << price 
  86.                 << setw(10) << setprecision(3) << milpergal 
  87.                 << setw(10) << setprecision(3) << kmperliter 
  88.                 << setw(12) << setprecision(2) << tripcost << endl 
  89.                 << endl; 
  90.          }
  91.          else
  92.          {
  93.             tripM = distance * KILPERMIL;
  94.             priceM = price / LITPERGAL;
  95.             volumeM = volume * LITPERGAL;
  96.             kmperliter = tripM / volumeM;
  97.             milpergal = distance / volume;
  98.             cout << showpoint
  99.                  << setw(10) << setprecision(3) << distance 
  100.                  << setw(10) << setprecision(3) << tripM 
  101.                  << setw(10) << setprecision(2) << price 
  102.                  << setw(10) << setprecision(2) << priceM 
  103.                  << setw(10) << setprecision(3) << milpergal 
  104.                  << setw(10) << setprecision(3) << kmperliter
  105.                  << setw(12) << setprecision(2) << tripcost << endl 
  106.                  << endl;
  107.          }
  108.          cout << "Enter the type of measurement units" << endl 
  109.               << "'M' for Metric" << " or 'E' for English or" 
  110.               << endl << "'Q' or 'q' for Quit: ";
  111.          cin  >> start;
  112.         }
  113.         else if ( start == QUIT || start == QUIT2 )
  114.            cout << endl << endl << "Thanks for using the program!";
  115.            else while ( start != METRIC && start != ENGLISH )
  116.               {
  117.                  cout << "Please reenter the type of measurement units"
  118.                       << endl << "'M' for Metric or 'E' for English: ";
  119.                  cin  >> start; 
  120.               }
  121.        }  
  122. cout << endl << endl << "Thanks for using the program!";
  123. return 0;
  124. }
My problem is that at one point, my tripcost is coming out 0.076 when it should be 0.08. I thought with the SetPrecision() function that would be taken care of, but apparently not. Any help on why that happens? It happens when the values for volume, price, and distance are 0.1, 0.76, and 16.00.
Feb 29 '08 #1
2 5025
First of all your course/book/... is outdated.
Iomanip.h, ... should be iomanip.

Second setprecision sets the ammount of digits after the decimal point, it isn't a rounding function.

What you need is a round function, just google a bit and you'll have it fast enough.
Feb 29 '08 #2
weaknessforcats
9,207 Expert Mod 8TB
Second setprecision sets the ammount of digits after the decimal point,
Maybe.

setprecision set the number of digits after the decimal point only if the notation is fixed or scientific. If the notation is floating-point, then setprecision specifies the number of digits to display including those before and after the decimal point.
Mar 1 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by tarmat | last post: by
2 posts views Thread by Woodster | last post: by
1 post views Thread by Gary Wessle | last post: by
2 posts views Thread by Wing | last post: by
3 posts views Thread by Anjo Gasa | last post: by
7 posts views Thread by jacek.dziedzic | last post: by
2 posts views Thread by mahesh.kanakaraj | last post: by
3 posts views Thread by PengYu.UT | last post: by
1 post views Thread by jthep | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.