SetPrecision and ShowPoint

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • victoryusami
    New Member
    • Feb 2008
    • 1

    #1

    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

    [CODE=cpp]#include <iostream>
    #include <iomanip>
    using namespace std;

    char const METRIC = 'M'; // Input read as metric
    char const ENGLISH = 'E'; // Input read as english
    char const QUIT = 'Q'; // Input read to quit program
    char const QUIT2 = 'q'; // Input read to quit program
    float const MINDISTANCE = 16.00; // Minimum value for tripdistance
    float const MAXDISTANCE = 2800.00; // Maximum value for tripdistance
    float const MINPRICE = 0.75; // Minimum value for volume
    float const MAXPRICE = 5.00; // Maximum value for volume
    float const GASMINIMUM = 0.00; // Minimum value for gas
    float const LITPERGAL = 3.79; // Conversion factor for English <-> Metric (lit to gal)
    float const KILPERMIL = 1.61; // Conversion factor for English <-> Metric (kil to mil)

    int main()
    {
    char start; // Input to start program; metric, english, or quit
    float volume; // Input for gas volume
    float price; // Input for gas price
    float distance; // Input for total trip distance
    float volumeM; // Input stored conversion: volume from English to Metric
    float volumeE; // Input stored conversion: volume from Metric to English
    float priceM; // Input stored conversion: price from English to Metric
    float priceE; // Input stored conversion: price from Metric to English
    float tripM; // Input stored conversion: trip distance from English to Metric
    float tripE; // Input stored conversion: trip distance from Metric to English
    float milpergal; // Input stored computation for ML/Gal
    float kmperliter; // Input stored computation for KM/Liter
    float tripcost; // Input stored for total cost of trip

    cout << "The program calculates mileage and other statistics" << endl
    << "for a road trip in both Metric and English units."
    << endl << endl;

    cout << "Enter the type of measurement units" << endl << "'M' for Metric"
    << " or 'E' for English or" << endl << "'Q' or 'q' for Quit: ";
    cin >> start;

    while ( start != QUIT && start != QUIT2 )
    {
    if ( start == METRIC || start == ENGLISH )
    {
    cout << endl << "Please enter the volume of gas consumed: ";
    cin >> volume;
    while ( volume <= GASMINIMUM )
    {
    cout << "Please reenter the volume gas consumed"
    << endl << "(it must be greater than 0.00): ";
    cin >> volume;
    }
    cout << endl << "Please enter the price of gas per unit volume: ";
    cin >> price;
    while ( price <= MINPRICE || price >= MAXPRICE )
    {
    cout << "Please reenter Fuel Price (it must be higher"
    << endl << "than 0.75 and lower than 5.00) :";
    cin >> price;
    }
    cout << endl << "Please enter the trip distance: ";
    cin >> distance;
    while ( distance < MINDISTANCE || distance >= MAXDISTANCE )
    {
    cout << "Please reenter a trip distance greater than"
    << endl << "or equal to 16.00 and less than 2800.00: ";
    cin >> distance;
    }
    cout << endl << setw(10) << "MILES" << setw(10) << "KMETERS"
    << setw(10) << "$/Gal" << setw(10) << "$/Lit"
    << setw(10) << "ML/Gal" << setw(10) << "KM/Liter"
    << setw(12) << "Trip Cost" << endl;
    tripcost = price * volume;
    if ( start == METRIC )
    {
    tripE = distance / KILPERMIL;
    priceE = price * LITPERGAL;
    volumeE = volume / LITPERGAL;
    kmperliter = distance / volume;
    milpergal = tripE / volumeE;
    cout << showpoint
    << setw(10) << setprecision(3) << tripE
    << setw(10) << setprecision(3) << distance
    << setw(10) << setprecision(2) << priceE
    << setw(10) << setprecision(2) << price
    << setw(10) << setprecision(3) << milpergal
    << setw(10) << setprecision(3) << kmperliter
    << setw(12) << setprecision(2) << tripcost << endl
    << endl;
    }
    else
    {
    tripM = distance * KILPERMIL;
    priceM = price / LITPERGAL;
    volumeM = volume * LITPERGAL;
    kmperliter = tripM / volumeM;
    milpergal = distance / volume;
    cout << showpoint
    << setw(10) << setprecision(3) << distance
    << setw(10) << setprecision(3) << tripM
    << setw(10) << setprecision(2) << price
    << setw(10) << setprecision(2) << priceM
    << setw(10) << setprecision(3) << milpergal
    << setw(10) << setprecision(3) << kmperliter
    << setw(12) << setprecision(2) << tripcost << endl
    << endl;
    }
    cout << "Enter the type of measurement units" << endl
    << "'M' for Metric" << " or 'E' for English or"
    << endl << "'Q' or 'q' for Quit: ";
    cin >> start;
    }
    else if ( start == QUIT || start == QUIT2 )
    cout << endl << endl << "Thanks for using the program!";
    else while ( start != METRIC && start != ENGLISH )
    {
    cout << "Please reenter the type of measurement units"
    << endl << "'M' for Metric or 'E' for English: ";
    cin >> start;
    }
    }
    cout << endl << endl << "Thanks for using the program!";
    return 0;
    }[/CODE]

    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.
    Last edited by Ganon11; Feb 29 '08, 03:00 PM. Reason: Please use the [CODE] tags provided.
  • Simonius
    New Member
    • Feb 2008
    • 47

    #2
    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.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by Simonius
      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.

      Comment

      Working...