gpt4 book ai didi

C++ Setprecision 返回错误

转载 作者:行者123 更新时间:2023-11-28 06:04:02 26 4
gpt4 key购买 nike

这是我的大部分代码,我需要将最后一个 cout 的精度设置为 2,这样我就可以得到小数点后的位置:

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

/*
*
*/
int main(int argc, char** argv) {
int length_room, width_room ;
int length_tile, width_tile ;
int room_ft;
int tile_ft;
int tiles_needed;
double cost;
double cost_room;


cout <<"Enter the length of the room (in feet) ";
cin >> length_room ;
cout << "Enter the width of the room (in feet) " ;
cin >> width_room ;
cout << "Enter the length of the tile (in inches) " ;
cin >> length_tile ;
cout << "Enter the width of the tile (in inches) ";
cin >> width_tile ;
cout << "Enter the amount for each tile " ;
cin >> cost;

length_tile=(length_tile/12);
width_tile=(width_tile/12);
tile_ft=(length_tile*width_tile);
room_ft=(length_room*width_room);
tiles_needed=(room_ft/tile_ft);
cost_room=(cost*tiles_needed);
cout<<endl;
cout<<endl;

cout << "Length of the room " <<length_room<<" ft"<<endl;
cout << "Width of the room "<<width_room<<" ft"<<endl;
cout << "Least number of tiles needed "<<tiles_needed<< " tiles"<<endl; /* Need to round up still */

cout << fixed <<setprecision(2)
<<"Total Cost: $" << cost_room <<endl; /* Need to mod still to 2 decimal places */



return 0;

我不明白为什么它没有编译?

P.S.:如有任何建议,我们将不胜感激。

最佳答案

当 C++ 编译器提示完美的代码时,通常是因为缺少 header 。难道你忘了#include <iomanip>

关于C++ Setprecision 返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32743946/

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