-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後の開始ブロックの前にセミコロンがあるため、キーワード (ie. 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 に答える