472,789 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,789 software developers and data experts.

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 5446
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,208 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

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

Similar topics

5
by: tarmat | last post by:
I'm trying to create a function that will turn a float into a std::string that always shows the number to two decimal places. I have the following but it doesn't give the desired output: ...
2
by: Woodster | last post by:
I am using the std::setprecision function to format variables of type double in a string however I am unsure how to stop this appearing in scientific notation. For example std::stringstream...
1
by: Gary Wessle | last post by:
hi the code below is giving me what I want but it is very ugly. and will not work for a long list of different length numbers. could you please look at it and comment. thank you the...
2
by: Wing | last post by:
Hello everyone, I have the following code: /////////////////////////////////////// double num; num=1234567890; num+=(1.0/3.0); cout.setf(ios::fixed);
3
by: Anjo Gasa | last post by:
I'm having some cases where setprecision in combination with iostreams gives some unepected behavior. Consider the following program: #include <iostream> #include <iomanip> int _tmain(int...
7
by: jacek.dziedzic | last post by:
Hello! Can someone tell me what the expected output of the following program is? #include <fstream> #include <iomanip> using namespace std;
2
by: mahesh.kanakaraj | last post by:
Hi All, I am new to C++ programming. I encounter an unexpected behavior of 'setprecision'. The code snippet is : #include <iostream.h> #include <stdio.h> #include <iomanip.h>
3
by: PengYu.UT | last post by:
Hi, I setprecision to be 100 for both cases. I'm wondering why the number of digits are different. Also, for a double number, I think any digits that are longer than 15 (or 16) are not...
1
by: jthep | last post by:
Hi, I'm converting codes that I worked with in C to C++ and having a problem with setprecision. At first the code worked, then I made some changes. When the changes didn't work I wrote it back to...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.