C ++程序计算房间的地毯成本

时间:2013-11-21 06:00:38

标签: c++ function cout

目前我在C ++课程中,正在学习函数。我们的任务是创建一个程序来计算总成本,包括每个房间的每平方英尺的价格,人工和税收,由用户的计数输入给出。这是我的程序,我无法编译,我不知道为什么。感谢任何帮助我的人,我非常感谢。

例如,输入: 2 2 2 2 2 2 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;
}

4 个答案:

答案 0 :(得分:0)

在功能中

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)`

答案 1 :(得分:0)

这里发生了很多事情,其中​​最重要的是解决函数的返回值,如@john所说。仅就您的数学而言,考虑1个2英尺x 2英尺的房间,每平方英尺2个成本 - 您的安装价格为installation = (2 * 4) + (.35 * 4) + 0,即9.4。稍后在您的计划中,您添加税, price = 9.4 * .05 + 9.4,它给你9.87。不确定你提到的11.2来自哪里。

答案 2 :(得分:0)

如果你从函数的末尾开始返回除了void之外的其他东西,那么程序就会有未定义的行为,即它可以做任何想做的事情。产生除预期之外的其他产出是可以采取的最温和形式。

以下是您应该对代码进行的更改:

  1. 声明为返回值的任何函数都应该有一个return语句(main()是一个例外,它不会绝对需要return,尽管它返回int但是仍然使用return 0;)是一种很好的风格。
  2. 您应该验证所有输入实际上是否成功,例如,使用以下内容:

    if (std::cin >> length >> width >> costpersquarefeet) {
        // process the entered data
    }
    else {
        std::cerr << "ERROR: failed to read inoyt!\n";
    }
    

    我可能会使用更详细的chrck,并告诉用户我期待的输入。

  3. 使用全局变量!极少数情况下它们是必要的,甚至你最好使用不同的方法! (即在函数内部将它们定义为static变量并以某种方式访问​​它们;您可以在必要时了解全局变量,这可能是在将来的某个时期。)

答案 3 :(得分:0)

                            #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
相关问题