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

  • 本文关键字:计算 问题 小数点 显示 c++
  • 更新时间 :
  • 英文 :


我正在参加C 在线类(可能是一个错误(,我正在尝试弄清楚如何更正此代码。我不得不从头开始编写一个程序,我对自己做的不正确感到非常困惑。

这是应该出来的:

BlockQuote欢迎来到名字的地毯计算器!

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

以英尺的形式输入房间的长度:10

以英尺的形式输入房间的宽度:10

输入每码地毯的价格:3.49

输入填充费用:35

                                         Carpet estimate for bedroom

房间的总平方英尺:100

房间的总正码:12

需要地毯的总成本:41.88

总人工成本($ 6.03/yd(:72.36

填充费用:35.00

小计:149.24

税:8.50

总计:157.74

blockquote

其他一些规则是:房间的平方码是通过将总平方英尺除以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 << "nn 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 << "nn 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 << "nn 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;

} 

如果您使用的是小数点,则需要使用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

因为您的编译器将在十进制的前面使用整个数字,而不是在其之后使用任何内容。

我弄清楚了,唯一的问题是税收是由一分钱减少的。我不知道为什么,但是足够接近。

最终代码:

    // 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 << "nn 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 << "nn" << setw(20) << "" << "Carpet estimate for " << roomName; // displays text and the room name
    cout << "nn Total square feet of the room: " << squareFeet; //displays the square feet calculation **CORRECT
    cout << "nn Total square yards of the room: " << squareYard; // displays the square yards calculation **CORRECT
    cout << "nnn Total cost of the carpet needed: " << totalCarpetCost; // displays the carpet cost **INCORRECT
    cout << "nn Total cost of labor (at $6.03/yd): " << laborCost; // displays the labor cost 
    cout << std::fixed << std::setprecision(2) << "nn Padding Cost: " << paddingCost; // displays the padding cost 
    cout << std::fixed << std::setprecision(2) << "nnn Subtotal: " << subtotal; // displays the subtotal
    cout << std::fixed << std::setprecision(2) << "nn Tax: " << taxCost; // displays the tax calculation
    cout << std::fixed << std::setprecision(2) << "nn Total: " << totalCost; // displays the final total cost
    cout << "nn"; // line break

    system("PAUSE"); 
  return 0;

}

最新更新