以C+显示小数点+;,计算问题 我正在攻读C++在线课程(可能是个错误),我正试图找出如何纠正这个代码。我不得不从头开始写一个程序,我完全被弄糊涂了,我做的不对
这是应该出来的: Blockquote欢迎使用name的地毯计算器 输入要铺地毯的房间的名称:卧室 输入房间的长度(以英尺为单位):10 输入房间的宽度(以英尺为单位):10 输入每码地毯的价格:3.49 输入填充成本:35以C+显示小数点+;,计算问题 我正在攻读C++在线课程(可能是个错误),我正试图找出如何纠正这个代码。我不得不从头开始写一个程序,我完全被弄糊涂了,我做的不对,c++,C++,这是应该出来的: Blockquote欢迎使用name的地毯计算器 输入要铺地毯的房间的名称:卧室 输入房间的长度(以英尺为单位):10 输入房间的宽度(以英尺为单位):10 输入每码地毯的价格:3.49 输入填充成本:35 Carpet estimate for bedroom 房间总面积:100平方英尺 房间总面积:12平方码 所需地毯的总成本:41.88 劳动力总成本(6.03美元/码):72.36 填充成
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评估
#包括
使用名称空间std;
int main()
{
字符串roomName;//声明
室内长度;
室内宽度;
整数平方英尺;
国际地毯价格;
内部填充成本;
国际总成本;
成本税税率=0.057;
国际劳工成本;
劳动力成本率=6.03;
整数平方码;
整数小计;
国际税收成本;
总成本;
cout roomName;//存储房间名称
cout>roomLength;//存储长度
cout>roomWidth;//存储宽度
cout>price;//存储价格
cout>paddingCost;//存储填充成本**不起作用**
SquareFoots=roomLength*roomWidth;//通过乘以长度和宽度查找平方英尺
squareYard=(squareFeet/9)+1;//查找平方英尺并向上取整到下一个码
Total地毯成本=地毯价格*平方码;//通过将输入地毯价格乘以平方码来查找地毯成本
laborCost=squareYard*LABOR_RATE;//通过将码数乘以6.03的人工费率来计算人工成本
小计=(总地毯成本+填充成本+人工成本);//通过添加人工成本、地毯成本和填充成本来查找小计
taxCost=小计*税率;//通过小计乘以.057(5.7%)的税率来查找税收成本
totalCost=taxCost+subtotal;//添加小计和税以查找总成本
cout如果使用小数,则需要使用double
而不是int
。double
是一种浮点类型。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
inta=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 << "\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;
}
//约翰·迈耶学习单元2评估
#包括
#包括
使用名称空间std;
int main()
{
字符串roomName;//声明
双室长;
双室宽;
双平方英尺;
双倍价格;
双重填充成本;
双重总成本;
常数双倍税率=0.057;
双重人工成本;
施工双人工率=6.03;
整数平方码;
双倍小计;
双重征税成本;
总成本加倍;
cout-price;//存储价格
cout>paddingCost;//存储填充成本(这最初不起作用,因为价格是int,而不是double)
SquareFoots=roomLength*roomWidth;//通过乘以长度和宽度查找平方英尺
//squareYard=(平方英尺/9)+1;//(实现3x3房间之前的原始代码无法正常工作)
squareYard=squareFeet/9;//查找平方码
如果((int)平方英尺%9!=0)//如果余数除以9后不等于0,则。。。
{squareYard++;}/…四舍五入1
Total地毯成本=地毯价格*平方码;//通过将输入地毯价格乘以平方码来查找地毯成本
laborCost=squareYard*LABOR_RATE;//通过将码数乘以6.03的人工费率来计算人工成本
小计=(总地毯成本+填充成本+人工成本);//通过添加人工成本、地毯成本和填充成本来查找小计
taxCost=小计*税率;//通过小计乘以.057(5.7%)的税率来查找税收成本
totalCost=taxCost+subtotal;//添加小计和税以查找总成本
你能不能回顾一下int是什么以及整型算法是如何工作的?我使用了老师提供的所有东西。没有提到这些,但感谢你的精明回答。这句话现在给了我一个问题:好吧,我在顶部添加了#include,这似乎解决了我的一个问题,现在我只需要知道如何正确地使用e执行小数点限制将其放在前面真是太棒了!这就解决了问题,并使填充成本输入正常工作。我必须将我的计算四舍五入到小数点后2位,这是下一个问题,以及我的“总人工成本”行仍然显示72而不是72.36使用#include
无法解决另一个不需要使用setprecision的问题(2)不止一次。因为当你做一次setprecision时,它会自动为它下面的所有其他代码做。你可以看到这是的,对不起,这不是我的最终代码,我删除了它,因为我意识到声明在更改之前是正确的:P再次感谢你的帮助!