QUANTITY DISCOUNT
10-19 20%
20-49 30%
50-99 40%
100 or more 50%
编写一个程序,询问售出的单位数量并计算购买的总成本。输入验证:确保单元数大于 0"
这是我到目前为止所拥有的:
#include <iostream>
#include <string> //String class- a string of text
#include <iomanip> //Required for setw= the field width of the value after it
using namespace std;
int main()
{
double sales, charges, numOfUnits = 0,
rateA = .20, rateB = .30, rateC = .40, rateD = .50;
//Set the numeric output formatting:
cout << fixed << showpoint << setprecision(2);
cout << "Enter the quantity for your order: ";
cin >> sales;
// Determine the discount:
double PRICE=99.0;
if (sales >= numOfUnits)
if (sales >= 10 && sales <= 19 )
rateA;
charges = PRICE - rateA *sales;
if (sales >= 20 && sales <= 49)
rateB;
charges = PRICE - rateB *sales;
if (sales >= 50 && sales <= 99)
rateC;
charges = PRICE - rateC *sales;
if (sales > 100 )
rateD;
charges = PRICE - rateD *sales;
cout << "Your total price for this quantity is: $" <<charges
<< " per unit."<< endl;
cout << "That is an invalid number. Run the program again\n "
<< "and enter a number greater than\n"
<< numOfUnits << ".\n";
}
编译后,输出没有给我正确的答案。也许我的数学是错误的,或者我的流程是关闭的?有什么建议么?
我不希望任何人为我写这个,但也许给我一些指示