C++ Setprecision 返回错误

C++ Setprecision returning an error

这是我的大部分代码,我需要将最后一个 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++ 编译器抱怨完美的代码时,通常是因为缺少 headers。你是不是忘了#include <iomanip>

#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;