473,734 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SetPrecision and ShowPoint

1 New Member
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 5519
Simonius
47 New Member
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 Recognized Expert Moderator Expert
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
22579
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: inline std::string ftos(float f) { std::ostringstream buffer; buffer << std::setprecision(2) << f;
2
8721
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 buffer; buffer << setprecision(1) << 40.0 << "° C";
1
2673
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 desired output is: 0.555504 of an inch foreach 12.55
2
2315
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
2498
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 argc, _TCHAR* argv) { float value = 0.063397526741027832;
7
12504
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
1811
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
5063
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 meaningful, because it exceed the double number's precision limit. Even if I setprecision to be 100, shall it truncate the number to be of 15(or 16) digits?
1
4881
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 how it was before and now it's not working. I keep getting the error message where setprecision is undefined. Can anyone help plz. #include <iostream> using namespace std; float convertFtoC(int temperature); int roundUpNextMultiple(int...
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9182
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.