输出错误:C ++地毯商店

时间:2017-04-13 08:18:28

标签: c++

我正在使用C ++编写一个作业,但我对C ++编程语言比较陌生,但是,我的输出中出现了错误。

问题

Crosswell地毯商店的经理要求您编写一个程序来打印客户账单。经理给了您以下信息:

  • 房间的长度和宽度以米和厘米表示。例如,长度可能会报告为16.7米。
  • 商店不出售一米的分数。因此,长度和宽度必须始终向上舍入。
  • 地毯费用等于每平方米地毯成本购买的平方米数。必须将相当于地毯成本14%的销售税添加到账单中。
  • 人工成本等于购买的平方米数R24.00,即每平方米的人工成本。不对劳工征税。
  • 每个客户都有一个五位数字标识,该号码应显示在账单上。由以“0”开头的客户编号标识的大批量客户可以给予折扣。折扣适用于添加销售税前的成本 示例输出如下:

    CROSWELL CARPET STORE STATEMENT
    Customer name : xxxxx
    Customer number : xxxxx
    Carpet price : xx.xx
    Labour : xx.xx
    Subtotal : xx.xx
    Less discount : xx.xx
    Subtotal : xx.xx
    Plus tax : xx.xx
    Total : xx.xx
    

我对这个问题的回答如下:

#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

using namespace std;

int calculateCarpetSize (int length, int width)
{
    int carpetSize;
    carpetSize = ceil(length * width);
    return carpetSize;
}

float calculateCarpetCost (int carpetSize , float sellingPrice)
{
    float carpetCost;
    carpetCost = carpetSize * sellingPrice;
    return carpetCost;
}

float calculateLabourCost(int carpetSize)
{
    float labourCost;
    labourCost = carpetSize * 24.00;
    return labourCost;
}

bool qualifyForDiscount(string customerNo)
{
    string dis = "0";
    if (customerNo.compare(0, dis.length(), dis) == 0)
    {
        return true;
    }
    else{
        return false;
    }

}

float computeDiscount ()
{
    int discountPercentage;
    float discount;
    cout << "Enter the Percetage Discount: ";
    cin >> discountPercentage ;
    discount = discountPercentage / 100;
    return discount;
}

void printCustomerStatement(string customerName, string customerNo, float carpetCost, float labourCost, float discount)
{
    float subtotal = carpetCost + labourCost - discount;
    float vat = subtotal*0.14;
    cout << "\n      CROSWELL CARPET STORE"<<endl;
    cout << "            STATEMENT"<<endl;
    cout << "Customer name   : "<<customerName<<endl;
    cout << "Customer number : "<<customerNo <<'\n'<<endl;
    cout << "Carpet price    : "<<carpetCost<<endl;
    cout << "Labour          : "<<labourCost <<'\n'<<endl;
    cout << "Subtotal        : "<<carpetCost+labourCost<<endl;
    cout << "Less discount   : "<<discount <<'\n'<<endl;
    cout << "Subtotal        : "<<subtotal<<endl;
    cout << "Plus Tax        : "<<vat<<endl;
    cout << "Total           : "<<subtotal - vat<<endl;
}
int main()
{
     string customerName;
     string customerNo;
     float carpetSize;
     float sellingPrice;
     float length;
     float width;
     float carpetCost;
     float labourCost;
     float discount;
    cout <<"ENTER CUSTOMER NAME:";
    cin >>customerName;
    cout <<"ENTER CUSTOMER NUMBER:";
    cin >>customerNo;
    cout <<"ENTER ROOM WIDTH:";
    cin >>width;
    cout <<"ENTER ROOM LENGTH:";
    cin >>length;
    cout <<"ENTER SELLING PRICE:";
    cin >>sellingPrice;
    calculateCarpetSize(length, width);
    calculateCarpetCost(carpetSize, sellingPrice);
    calculateLabourCost(carpetSize);
    if(qualifyForDiscount(customerNo))
    {
        computeDiscount();
    }else
    {
        discount = 0.00;
    }
    printCustomerStatement(customerName, customerNo, carpetCost, labourCost, discount);
    return 0;
}

我认为问题可能是我的函数或我的数据类型,或者两者都有。

修改

我得到了这种输出

ENTER CUSTOMER NAME:Quatban
ENTER CUSTOMER NUMBER:02234
ENTER ROOM WIDTH:20
ENTER ROOM LENGTH:20
ENTER SELLING PRICE:35
ENTER DISCOUNT PERCENTAGE: 2

  CROSWELL CARPET STORE
        STATEMENT
Customer name   : Quatban
Customer number : 02234

Carpet price    : 1.4e+004
Labour          : 9.6e+003

Subtotal        : 2.4e+004
Less discount   : 0

Subtotal        : 2.4e+004
Plus Tax        : 3.3e+003
Total           : 2e+004

1 个答案:

答案 0 :(得分:0)

第一

  

因此,长度和宽度必须总是向上舍入

这意味着您的函数calculateCarpetSize应该更像:

int calculateCarpetSize (float length, float width)
{
    int carpetSize;
    carpetSize = ceil(length) * ceil(width);
    return carpetSize;
}

然后

在你的主要:

calculateCarpetSize(length, width);
calculateCarpetCost(carpetSize, sellingPrice);
calculateLabourCost(carpetSize);

您应该存储这些函数的结果,如:

carpetSize = calculateCarpetSize(length, width);
carpetCost = calculateCarpetCost(carpetSize, sellingPrice);
labourCost = calculateLabourCost(carpetSize);

修改

这实际上是你的输出:

ENTER CUSTOMER NAME:Quatban
ENTER CUSTOMER NUMBER:02234
ENTER ROOM WIDTH:20
ENTER ROOM LENGTH:20
ENTER SELLING PRICE:35
Enter the Percetage Discount: 2

      CROSWELL CARPET STORE
           STATEMENT
Customer name   : Quatban
Customer number : 02234

Carpet price    : 14000
Labour          : 9600

Subtotal        : 23600
Less discount   : 0

Subtotal        : 23600
Plus Tax        : 3304
Total           : 20296

这看起来像你的(预期的?)结果,即使我认为最后一行应该是subtotal + tax而不是subtotal - tax,但我可能错了。