gpt4 book ai didi

c++ - 简单的 c++ cout 语句,一行有格式问题,这是什么?

转载 作者:行者123 更新时间:2023-11-28 08:22:25 25 4
gpt4 key购买 nike

这是我的代码:

#include <iomanip>
#include <fstream>
#include <iostream>
using namespace std;

int main ()
{
ifstream fin;

fin.open("Celsius.txt");

if (!fin.good()) throw "I/O error";

double myC;
fin >> myC;

fin.close();

ofstream fout;
fout.open("Fahrenheit.txt");
if (!fout.good()) throw "I/O error";

double myAnswer = (myC * 1.80) + 32;
fixed;
cout << myC << " Celsius equals " << setprecision(3) << myAnswer << " Fahrenheit" << endl;
fout << myC << " Celsius equals " << setprecision(3) << myAnswer << " Fahrenheit" << endl;

fout.close();
}

好吧,我只是错过了一些完整的基本神经元,我似乎在理解这一点上遇到了一些障碍。

  • 格式化输入回显为-2,
  • -2 表示不使用一位小数位格式化输出。
  • 不要只格式化输出的输入值。

    fixed;
    cout << myInput << " should not be formatted, but " << setprecision(3) << myOutput << " should be" << endl;

那不留下吗:

myInput 是未格式化的输入回显并且 myOutput 被格式化为一位小数?

最佳答案

您必须在输出流中包含 fixed,如下所示:

cout << myC << " Celsius equals " << fixed << setprecision(3) << myAnswer << " Fahrenheit" << endl;

setprecision 将指定小数点后的小数位数。所以 3 会产生一个像 72.000 这样的数字,或者在你的情况下 1 会把它设置为 72.0

关于c++ - 简单的 c++ cout 语句,一行有格式问题,这是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5320878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com