C++程序计算房间的地毯费用
C++ Program calculate carpet cost of rooms
目前我在一个C++类中,正在学习函数。我们的任务是创建一个程序来计算总成本,包括用户输入的每个房间的每平方英尺价格、劳动力和税收。这是我的程序,我无法编译,也不知道为什么。感谢任何帮助我的人,我非常感谢。
例如,输入:2.2 22 2
应给出输出:2 2 22.4
但它正在打印这个:
2 2 9.87
我不知道出了什么问题。。我看了我所有的公式,它们看起来都是正确的。一个尺寸为2*2的房间,每平方英尺2英镑,总成本应该是11.2英镑,我很困惑。也许我设置的功能不正确?
#include <iostream>
#include <iomanip>
using namespace std;
void getdata (int&, int&, float&);
float InstalledPrice (int , int, float );
float totalprice (float);
void printdata (int , int, float);
int length, width, count, x;
float installation, costpersqfoot, price;
int main()
{
cout << "Enter the amount of rooms.n";
cin >> x;
for(count = x; count != 0; count --)
{
getdata (length, width, costpersqfoot);
InstalledPrice (length, width, costpersqfoot);
totalprice (installation);
if (count ==1)
{
printdata (length, width, price);
}
}
}
void getdata(int & length, int & width, float & costpersqft)
{
cin >> length >> width >> costpersqft;
}
float InstalledPrice (int a, int b, float c)
{
installation = 0;
const float LABOR_COST = 0.35;
float sqfoot;
sqfoot = length * width;
installation = (costpersqfoot * sqfoot) + (LABOR_COST * sqfoot) + installation;
}
float totalprice(float installation)
{
const float TAX_RATE = 0.05;
price = (installation * TAX_RATE) + installation;
}
void printdata(int length, int width, float price)
{
cout << length << " " << width << " " << price << endl;
}
在函数中
float InstalledPrice (int a, int b, float c)
每次执行该函数时,都会重置安装值。
installation = 0;
请尝试删除该行,并将其插入主函数中的for循环之前。
installation = 0;
for(count = x; count != 0; count --)
使用代码中的常量,它将打印19.74
(4*2)+(4*0.35)=每个房间9.4,两个房间18.8。含税,(18.8*1.05)=19.74)`
这里发生了很多事情,其中最重要的是像@john所说的那样处理函数的返回值。不过,就你的数学而言,考虑一个2英尺x 2英尺的房间,每平方英尺2个——你的安装价格是installation = (2 * 4) + (.35 * 4) + 0
,也就是9.4。稍后在您的程序中,您将添加税款,price = 9.4 * .05 + 9.4
,它给你9.87。不确定你提到的11.2是从哪里来的。
如果你从返回void
以外的函数的末尾掉下来,程序就会有未定义的行为,也就是说,它可以做任何它想做的事情。生产出超出预期的其他产品是最温和的形式。
以下是您应该更改的代码:
- 任何声明返回值的函数都应该有一个
return
语句(main()
是一个例外,虽然它返回int
,但它并不绝对需要return
,但仍然使用return 0;
是一种好的风格) -
您应该验证所有输入实际上都是成功的,例如,使用以下内容:
if (std::cin >> length >> width >> costpersquarefeet) { // process the entered data } else { std::cerr << "ERROR: failed to read inoyt!n"; }
我可能会使用更详细的chrck,并告诉用户我期望的输入。
- 不要使用全局变量!在极少数情况下,它们是必要的,甚至你最好使用不同的方法!(即,将它们定义为函数中的
static
变量,并以某种方式访问它们;您可以在必要时了解全局变量,这可能在相当长的将来发生)
#include <iostream>
#include <time.h>
#include <stdlib.h>
// This program is writen by KHUMBELO DIFFERENCE MUTHAVHINE from Ha- Vhangani . Khumbelo is a programmer
using namespace std;
// call function calculateCarpetSize
void CalculateCarpetSize (int &length, int &width, int &carpetSize)
{
carpetSize = length * width;
}
// call function calculateCarpetCost
void CalculateCarpetCost (int &carpetSize , float &sellingPrice , float &carpetCost )
{
float tax = 0.14;
carpetCost = carpetSize * sellingPrice * tax;
}
// call function calculateLabourCost
CalculateLabourCost(int &carpetSize , int &labour, float &labourCost)
{
labourCost = carpetSize * labour;
}
bool qualifyForDiscount(int &customerNo){
if (customerNo <=9999)
{
void computeDiscount (float &carpetCost );
return true ;
}
else{return false;
}
}
// call function computeDiscount
void computeDiscount (float &carpetCost, float &discountPercentage )
{
cout << "Enter the percetage discout: "<< endl;
cin >> discountPercentage ;
carpetCost = carpetCost * discountPercentage;
}
// call function printCustomerStatement
void printCustomerStatement( )
{
cout.setf(ios::fixed);
cout << endl;
cout << "CROSWELL CARPET STORE" << endl;
cout << "STATEMENT" << endl;
}
int main()
{
int customerNo;
string customerName, customersurname;
float roomLength;
float roomWidth, discountPercentage;
float sellingPrice;
float carpetCost;
float labourCost;
float discount;
int carpetSize;
int rooLength, roonWidth;
int labour = 24;
//float labourCost;
cout.setf(ios::fixed);
cout.precision(2);
cout << "nPlease enter the following information: ";
cout << "n Customer FIRST name: ";
cin >> customerName;
cout << "n Customer LAST name: ";
cin >> customersurname;
cout << "n Customer number: ";
cin >> customerNo;
cout << "n The length of the room: ";
cin >> roomLength;
cout << "n The width of the room: ";
cin >> roomWidth;
cout << "n The carpet selling price: ";
cin >> sellingPrice;
// call function calculateCarpetSize
CalculateCarpetSize (rooLength, roonWidth, carpetSize);
// call function calculateCarpetCost
CalculateCarpetCost (carpetSize ,sellingPrice, carpetCost );
// call function calculateLabourCost
CalculateLabourCost(carpetSize , labour, labourCost);
if (customerNo<= 9999){
bool qualityForDiscout(customerNo);
computeDiscount (carpetCost, discountPercentage );
}
// call function computeDiscount
// call function printCustomerStatement
// printCustomerStatement(customerName, customerNo, carpetCost, labourCost, discount);
printCustomerStatement();
cout << "n Customer name: ";
cout << customerName << customersurname<<endl;
cout << "n Customer number: ";
cout << customerNo <<endl;
cout << "n The length of the room: ";
cout << roomLength <<endl;
cout << "n The width of the room: ";
cout<< roomWidth <<endl;
cout << "n The carpet selling price: ";
cout<< "sellingPrice:" << sellingPrice <<endl;
return 0;
} // end main
相关文章:
- 用C++程序计算圆锥体的体积、球体的体积、八边形的面积和两点之间的距离
- 3-3. 编写一个程序来计算每个不同单词在其输入中出现的次数
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- OpenGL - 在 NDC 中计算位置适用于着色器,但不适用于'regular'程序
- C++用于计算移动平均线的程序
- 我的程序中的值计算不正确,即使支持数学是正确的
- 我的C++级数计算程序出了什么问题?被困了好几个星期
- 为什么程序没有正确计算值
- 计算条款总和的程序?c++
- 计算有多少人执行我的C++应用程序
- 3 函数加主函数 C 程序计算和显示阶乘
- 通过给定输出,程序计算中使用了多少个项
- 创建一个读取的程序计算C 文件中的行数
- C++程序计算房间的地毯费用
- 程序计算、类型转换错误
- C程序计算CRC
- C++程序计算热量指数,但没有提供预期结果
- 如何用c++程序计算cpu风扇转速
- c++程序计算最大公约数
- 程序计算英里旅行和MPH