-1

嗨,我正在编写一段代码来显示每个供应商的集合。我无法正确显示脂肪含量和基本付款。谁能帮我指出正确的方向?

#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;
}
4

1 回答 1

3

else完全是假的:

     else(fatContent>0.9);
  1. 应该是else if,不是else。如果您启用了编译器警告,一些编译器应该警告该语句(fatContent > 0.9)无效。
  2. 在开始块之前有一个分号,因此即使您确实添加了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;
    }
于 2013-11-10T17:07:24.553 回答