Setting up precision C++

时光怂恿深爱的人放手 提交于 2019-12-25 18:11:10

问题


So, i'm attempting to set the precision for input values in my code. I want the values to be printed with two decimal points afterwards of precision though i'm not exactly sure how.

Here's my code.

#include <iostream>
#include <iomanip>

float uphill, wellD, waterLvl, buckVol;
float buckAscRate, downHill, volume;
float last;
float timeReq;

int scene = 1;





void timeRequired()
{
    std::setw(2);
    std::setprecision(2);

    std::cout << "Scenario " << scene << ":" << std::endl;
    std::cout << "up hill" << "          " << uphill << " sec" << std::endl;
    std::cout << "well diamter" << "          " << wellD << " in" << std::endl;
    std::cout << "water level" << "          " << waterLvl << " in" << std::endl;
    std::cout << "bucket volume" << "          " << buckVol << " cu ft" << std::endl;
    std::cout << "bucket ascent rate" << "          " << buckAscRate << " in/sec" << std::endl;
    std::cout << "down hill" << "          " << downHill << " sec" << std::endl;
    std::cout << "required volume" << "          " << volume << " cu ft" << std::endl;

    timeReq = (uphill + downHill);

    std::cout << "TIME REQUIRED" << "          " << timeReq << " sec" << std::endl;

    std::cout << " " << std::endl;



}

void scenarioCONT()
{
    do
    {
        std::cin >> wellD;
        std::cin >> waterLvl;
        std::cin >> buckVol;
        std::cin >> buckAscRate;
        std::cin >> downHill;
        std::cin >> volume;
        std::cin >> last;

        if (uphill <= 1) uphill = 2;
        if (wellD <= 0) wellD = 1;
        if (waterLvl <= 0) waterLvl = 1;
        if (buckVol <= 0) buckVol = 1;
        if (buckAscRate <= 0) buckAscRate = 1;
        if (downHill <= 0) buckAscRate = 1;
        if (volume <= 0) volume = 1;

        if (last > 1)
        {
            uphill = last;
            scenarioCONT();

        }



    } while (last != 0);



}
void scenario()
{
    do
    {
        std::cin >> uphill;
        std::cin >> wellD;
        std::cin >> waterLvl;
        std::cin >> buckVol;
        std::cin >> buckAscRate;
        std::cin >> downHill;
        std::cin >> volume;
        std::cin >> last;

        if (uphill <= 1) uphill = 2;
        if (wellD <= 0) wellD = 1;
        if (waterLvl <= 0) waterLvl = 1;
        if (buckVol <= 0) buckVol = 1;
        if (buckAscRate <= 0) buckAscRate = 1;
        if (downHill <= 0) buckAscRate = 1;
        if (volume <= 0) volume = 1;

        if (last > 1)
        {
            timeRequired();
            uphill = last;
            scenarioCONT();

        }
            scene++;
            timeRequired();


    } while (last != 0);


}





int main()
{

    scenario();

    system("pause");
}

I've been told to use ionmanip to set the precision, though i'm not 100% on how to do it. Any suggestions?


回答1:


You can use std::setprecision function. The below example is directly taken from http://www.cplusplus.com/reference/iomanip/setprecision/

  double f =3.14159;
  std::cout << std::setprecision(5) << f << '\n';
  std::cout << std::setprecision(9) << f << '\n';



回答2:


If you want to output 2 decimal digits, you should use std::fixed, together with std::setprecision().

Take a look here.

For easier understanding, here is an example: cout << setprecision(2)<< 3.1415; outputs 3.1 (2 digits in total) and

cout << setprecision(2)<<fixed<< 3.1415; outputs 3.14 (2 digits after floating point)



来源:https://stackoverflow.com/questions/47625678/setting-up-precision-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!