在C ++中显示小数点,计算问题

时间:2017-06-04 19:57:06

标签: c++

我正在上C ++在线课程(可能是一个错误),我正在试图弄清楚如何纠正这段代码。我不得不从头开始写一个程序而且我完全不知道我做错了什么。

这应该是出现的:

  

Blockquote欢迎使用名称的地毯计算器!

输入要铺地毯的房间名称:卧室

以英尺为单位输入房间的长度:10

以英尺为单位输入房间的宽度:10

输入每码地毯的价格:3.49

输入填充成本:35

                                         Carpet estimate for bedroom

房间的总平方英尺:100

房间总平方码:12

所需地毯的总成本:41.88

总劳动力成本(每年6.03美元):72.36

填充成本:35.00

小计:149.24

税:8.50

总计:157.74

  

块引用

一些额外的规则是:房间的平方码数通过将总平方英尺除以9计算得出任何分数差异的1码。 安装地毯的人工成本为6.03美元/码 税率目前为5.​​7%

这是我目前的代码:

    // John Mayer Learning Unit 2 Assessment

#include <iostream>
using namespace std;


int main()
{
    string roomName; // declarations
    int roomLength;  
    int roomWidth;
    int squareFeet;
    int carpetPrice;
    int paddingCost;
    int totalCarpetCost;
    const int TAX_RATE = .057;
    int laborCost;
    const int LABOR_RATE = 6.03;
    int squareYard;
    int subtotal;
    int taxCost;
    int totalCost;

    cout << "Welcome to John Mayer's Carpet Calculator!"; // displays this at the top
    cout << "\n\n What is the name of the room to be carpeted?"; // asks for the room name
    cin >> roomName; // stores the room name
    cout << "\n What is the length of the room (in feet)?"; // asks for the length
    cin >> roomLength; // stores the length
    cout << "\n What is the width of the room (in feet)?"; // asks for the width
    cin >> roomWidth; // stores the width
    cout << "\n What is the price of the carpet per yard?"; // asks for the price of the carpet
    cin >> carpetPrice; // stores the price
    cout << "\n What is the padding cost?"; // asks for the padding cost 
    cin >> paddingCost; // stores the padding cost **DOES NOT WORK**

    squareFeet = roomLength * roomWidth; // finds square feet by multiplying length and width
    squareYard = (squareFeet / 9) + 1; // finds the square feet and rounds up to the next yard
    totalCarpetCost = carpetPrice * squareYard; // finds the carpet cost by multiplying the input carpet price by the square yardage
    laborCost = squareYard * LABOR_RATE; // finds the labor cost by multiplying the yardage by the labor rate of 6.03
    subtotal = (totalCarpetCost + paddingCost + laborCost); // finds the subtotal by adding the labor cost, carpet cost, and padding cost
    taxCost = subtotal * TAX_RATE; // finds the tax cost by multiplying the subtotal by the tax rate of .057 (5.7%)
    totalCost = taxCost + subtotal; // adds the subtotal and tax to find the total overall cost


    cout << "\n Carpet estimate for " << roomName; // displays text and the room name

    cout << "\n Total square feet of the room: " << squareFeet; //displays the square feet calculation **CORRECT
    cout << "\n Total square yards of the room: " << squareYard; // displays the square yards calculation **CORRECT
    cout << "\n\n Total cost of the carpet needed: " << totalCarpetCost; // displays the carpet cost **INCORRECT
    cout << "\n Total cost of labor (at $6.03/yd): " << laborCost; // displays the labor cost 
    cout << "\n Padding Cost: " << paddingCost; // displays the padding cost 
    cout << "\n\n Subtotal: " << subtotal; // displays the subtotal
    cout << "\n Tax: " << taxCost; // displays the tax calculation
    cout << "\n Total: " << totalCost;  // displays the final total cost
    cout << "\n"; // line break


    system("PAUSE"); 
  return 0;


} 

2 个答案:

答案 0 :(得分:0)

如果您使用的是小数,则需要使用double代替intdouble是浮点类型。 int(整数)是一个整数和一个不是分数的数字。因此,使用int作为小数将不起作用

int a = 1.22;
cout << a << endl;
//this will display 1
double b = 1.22;
cout << b << endl;
//this will display 1.22

因为你的编译器会使用小数前面的整数而不是后面的任何内容。

答案 1 :(得分:0)

我明白了,唯一的问题是税收是一分钱。我不知道为什么,但它足够接近。

最终代码:

    // John Mayer Learning Unit 2 Assessment

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


int main()
{
    string roomName; // declarations
    double roomLength;  
    double roomWidth;
    double squareFeet;
    double carpetPrice;
    double paddingCost;
    double totalCarpetCost;
    const double TAX_RATE = .057;
    double laborCost;
    const double LABOR_RATE = 6.03;
    int squareYard;
    double subtotal;
    double taxCost;
    double totalCost;

    cout << setw(15)<<""<< "Welcome to John Mayer's Carpet Calculator!"; // displays this at the top
    cout << "\n\n What is the name of the room to be carpeted? "; // asks for the room name
    cin >> roomName; // stores the room name
    cout << "\n What is the length of the room (in feet)? "; // asks for the length
    cin >> roomLength; // stores the length
    cout << "\n What is the width of the room (in feet)? "; // asks for the width
    cin >> roomWidth; // stores the width
    cout << "\n What is the price of the carpet per yard? "; // asks for the price of the carpet
    cin >> carpetPrice; // stores the price
    cout << "\n What is the padding cost? "; // asks for the padding cost 
    cin >> paddingCost; // stores the padding cost (this didn't originally work because carpetPrice was int, not double

    squareFeet = roomLength * roomWidth; // finds square feet by multiplying length and width
    //squareYard = (squareFeet / 9) + 1; // (Original code before realizing a 3x3 room would not work correctly
    squareYard = squareFeet / 9; // finds square yardage
    if ((int)squareFeet % 9 != 0)  // if the remainder does not equal 0 after dividing by nine, then...
        {squareYard++;} // ...round up 1
    totalCarpetCost = carpetPrice * squareYard; // finds the carpet cost by multiplying the input carpet price by the square yardage
    laborCost = squareYard * LABOR_RATE; // finds the labor cost by multiplying the yardage by the labor rate of 6.03
    subtotal = (totalCarpetCost + paddingCost + laborCost); // finds the subtotal by adding the labor cost, carpet cost, and padding cost
    taxCost = subtotal * TAX_RATE; // finds the tax cost by multiplying the subtotal by the tax rate of .057 (5.7%)
    totalCost = taxCost + subtotal; // adds the subtotal and tax to find the total overall cost
    cout << "\n\n" << setw(20) << "" << "Carpet estimate for " << roomName; // displays text and the room name

    cout << "\n\n Total square feet of the room: " << squareFeet; //displays the square feet calculation **CORRECT
    cout << "\n\n Total square yards of the room: " << squareYard; // displays the square yards calculation **CORRECT
    cout << "\n\n\n Total cost of the carpet needed: " << totalCarpetCost; // displays the carpet cost **INCORRECT
    cout << "\n\n Total cost of labor (at $6.03/yd): " << laborCost; // displays the labor cost 
    cout << std::fixed << std::setprecision(2) << "\n\n Padding Cost: " << paddingCost; // displays the padding cost 
    cout << std::fixed << std::setprecision(2) << "\n\n\n Subtotal: " << subtotal; // displays the subtotal
    cout << std::fixed << std::setprecision(2) << "\n\n Tax: " << taxCost; // displays the tax calculation
    cout << std::fixed << std::setprecision(2) << "\n\n Total: " << totalCost; // displays the final total cost
    cout << "\n\n"; // line break


    system("PAUSE"); 
  return 0;


}