您好我正在编写一段代码来展示每个供应商的馆藏。我无法正确显示脂肪含量和基本付款。谁能帮助我指出正确的方向?
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <iomanip>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << "\n\n\n\t\t\tNW-Dairies \n\n";
//date declaration
int totalDrugFails=0, noOfCollections=0;
double pricePerLtr=0.225, basicPayment=0, collectionQty=0, fatContent=0,fatPayment=0, totalPayment=0, paymentPerCollection;
double totalFatPayment=0, penaltyDeducted=0, totalDeducted=0;
char drugResult;
//Floating output values
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
//input data
cout <<"\n\n\tEnter number of collections (Between 10 & 31) : ";
cin>>noOfCollections;
while(noOfCollections < 10 || noOfCollections > 31)
{
cout<<"\n\n\tError! Please enter a number between 10 and 31 : ";
cin>>noOfCollections;
}
//input data
for(int x = 1; x<=noOfCollections; x++)
{
collectionQty=0;
paymentPerCollection=0;
fatPayment=0;
penaltyDeducted=0;
cout <<"\n\n\tEnter collection quanity : ";
cin>>collectionQty;
paymentPerCollection=pricePerLtr*collectionQty;
cout<<"\n\n\tEnter fat Content : (Greater than 0.1 and less than 2.5) : ";
cin>>fatContent;cin.ignore(10,'\n');
while (fatContent < 0.01 || fatContent > 2.5)
{
cout<<"\n\n\tError! Please enter a number between 0.1 and 2.5 : ";
cin>>fatContent;cin.ignore(10,'\n');
}
if(fatContent>0.02 && fatContent<0.05)
{
fatPayment=collectionQty*0.005;
totalFatPayment+=fatPayment;
}
if (fatContent>=0.05 && fatContent<=0.9)
{
fatPayment=collectionQty*0.0075;
totalFatPayment+=fatPayment;
}
else(fatContent>0.9);
{
fatPayment=collectionQty*0.02;
totalFatPayment+=fatPayment;
}
cout<<"\n\n\tDrug Failure (Y or N) : ";
cin>>drugResult;cin.ignore(10, '\n');
drugResult=toupper(drugResult);
//validating that it is only Y or N entered
while (drugResult != 'Y' && drugResult != 'N')
{
cout<<"\n\n\tPlease enter either Y or N : ";
cin>>drugResult;cin.ignore(10, '\n');
drugResult=toupper(drugResult);
}
//if the drug test fails then they lose the payment for that collection
if (drugResult=='Y')
{
penaltyDeducted=paymentPerCollection+fatPayment;
totalFatPayment=0;
basicPayment=0;
totalDrugFails++;
}
basicPayment+=paymentPerCollection;
totalFatPayment+=fatPayment;
totalDeducted+=penaltyDeducted;
totalPayment=basicPayment+totalFatPayment;
}
//Drug fails comes out correctly
//totalDeducted works
//basicPayment is not working as it shoule
cout<<"\n\n\n\tTotal Drug Fails : "<<totalDrugFails;
cout<<"\n\n\n\tTotal deducted : "<<totalDeducted;
cout<<"\n\n\n\tBasic Monthly payment : "<< char (156) << basicPayment;
cout<<"\n\n\n\tTotal fat payment : "<<char (156) << totalFatPayment;
cout<<"\n\n\n\tTotal payment : "<< char (156) << totalPayment;
_getch();
return 0;
}
答案 0 :(得分:3)
这else
完全是假的:
else(fatContent>0.9);
else if
,而不是else
。如果启用了编译器警告,则某些编译器应警告语句(fatContent > 0.9)
无效。 if
关键字(即。else if (fatContent > 0.9);
),else
块实际上也是空的,因为该分号。因此,大括号后面的代码无条件地执行。 如果你按如下方式重写这个嵌套的if-else
,那么事情是否开始正常工作?
if(fatContent>0.02 && fatContent<0.05)
{
fatPayment = collectionQty*0.005;
totalFatPayment += fatPayment;
} else if (fatContent>=0.05 && fatContent<=0.9)
{
fatPayment = collectionQty*0.0075;
totalFatPayment += fatPayment;
} else if (fatContent>0.9)
{
fatPayment = collectionQty*0.02;
totalFatPayment += fatPayment;
}