【发布时间】:2017-10-27 07:38:56
【问题描述】:
我已经编写了这个语句。
- 编写一个程序来获取 3 件商品的单价和数量。程序应输出总计的小计。如果总数大于 10,000.00,应给予 5% 的折扣。使用具有返回类型的函数,如下所述。 一种。名称:输入,返回类型:void 说明:取单价的价值和一件物品的数量。 湾。名称:calculateSubTotal,返回类型:double 描述:计算特定项目的小计。 C。名称:打印 返回类型:无效 说明:应打印最后一张账单,包括小计、总计和折扣
#include <iostream>
using namespace std;
void netTotal1(double &uPrice1,int &qty1)
{
double amount1;
cout<<"Enter Unit Price of First Item : ";
cin>>uPrice1;
cout<<"Enter Quantity : ";
cin>>qty1;
amount1=uPrice1*qty1;
}
void netTotal2(double &uPrice2,int &qty2)
{
double amount2;
cout<<"Enter Unit Price of Second Item : ";
cin>>uPrice2;
cout<<"Enter Quantity : ";
cin>>qty2;
amount2=uPrice2*qty2;
}
void netTotal3(double &uPrice3,int &qty3)
{
double amount3;
cout<<"Enter Unit Price of Third Item : ";
cin>>uPrice3;
cout<<"Enter Quantity : ";
cin>>qty3;
amount3=uPrice3*qty3;
}
double tot(double &amount1,double &amount2,double &amount3)
{
double total;
total=amount1+amount2+amount3;
cout<<"\nNet Amount of First Item : "<<amount1;
cout<<"\nNet Amount of First Item : "<<amount1;
cout<<"\nNet Amount of First Item : "<<amount1;
cout<<"\nTotal of All Items : "<<total;
}
double discount( double &total)
{
double dsc;
if (total>10000)
{
dsc=(total*0.05);
}
else
{
dsc=0;
}
return dsc;
}
double subTot(double &total,double &dsc)
{
double subTotal;
subTotal=total-dsc;
cout<<"\nDiscount : "<<dsc;
cout<<"\nSub Total is : " <<subTotal;
}
int main()
{
double uPrice1,uPrice2,uPrice3,total,amount1,amount2,amount3,dsc,subTotal;
int qty1,qty2,qty3;
netTotal1(uPrice1,qty1);
netTotal2(uPrice2,qty2);
netTotal3(uPrice3,qty3);
tot(amount1,amount2,amount3);
discount(total);
subTot(total,dsc);
}
很遗憾,输出中有错误。
Enter Unit Price of First Item : 10
Enter Quantity : 10
Enter Unit Price of Second Item : 10
Enter Quantity : 10
Enter Unit Price of Third Item : 10
Enter Quantity : 10
Net Amount of First Item : 5.2668e+267
Net Amount of First Item : 5.2668e+267
Net Amount of First Item : 5.2668e+267
Total of All Items : 7.14983e+281
Discount : 1.20132e-306
Sub Total is : -1.00361e-306
Process returned 0 (0x0) execution time : 5.957 s
Press any key to continue.
【问题讨论】:
-
使用调试器逐行执行代码时,您观察到了什么?所有变量都具有预期值吗?
-
您的主要问题是没有阅读和遵循说明。按照说明做,不要即兴发挥。