C++ 简单C++;输入文件和if语句

C++ 简单C++;输入文件和if语句,c++,if-statement,file-io,C++,If Statement,File Io,我写的代码,它的工作,除了总数是错误的。它应该将distanceRate乘以比率,再加上每一项成本,得出总数,但它没有这样做。任何帮助都将不胜感激 #include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; int main() { //Declare Variables ifstream inF

我写的代码,它的工作,除了总数是错误的。它应该将distanceRate乘以比率,再加上每一项成本,得出总数,但它没有这样做。任何帮助都将不胜感激

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
    //Declare Variables
    ifstream inFile;

    double packageWeight;
    double distance;
    double totalCharge = 0;
    double rate;
    double distanceRate;

    int customerNumber;
    double shippingCharge;
    int packageCount = 0;


    inFile.open("shipping.txt");
    if(inFile)
    {
        cout << "Customer   Package   Shipping" << endl;
        cout << "Number     Weight    Distance" << endl;

        while(!inFile.eof())
        {
            inFile >> customerNumber;
            inFile >> packageWeight;
            inFile >> distance;

            if(0 < packageWeight <= 2)
                rate = 1.10;
            else if(2 < packageWeight <=6)
                rate = 2.20;
            else if(6 < packageWeight <= 10)
                rate = 3.70;
            else if(10 < packageWeight <=20)
                rate = 4.80;
            else
                cout << "Invalid package weight" << endl;

            if( 0 < distance <= 500)
                distanceRate = 1;
            else if( 500 < distance <= 1000)
                distanceRate = 2;
            else if(1000 < distance <= 1500)
                distanceRate = 3;
            else if(1500 < distance <= 2000)
                distanceRate = 4;
            else
                cout << "Invalid distance" << endl;

            packageCount += customerNumber;
            shippingCharge = rate * distanceRate;
            totalCharge += shippingCharge;

            cout << fixed << setprecision(2) << showpoint;
            cout << setw(2) << customerNumber
            << right << setw(14) << packageWeight
            << setw(13) << distance
            << endl;

        } //End of while loop

        cout << "\nPackage shipped : " << packageCount << endl;
        cout << "Total Charge : $" << totalCharge << endl;
        inFile.close();
    }
    else
    {
        cout << "Could not open file" << endl;
    }
    system("pause");
    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
//声明变量
河流充填;
双包装重量;
双倍距离;
双倍总电荷=0;
双倍费率;
双距离率;
国际客户编号;
双重运费;
int packageCount=0;
infle.open(“shipping.txt”);
如果(填充)
{
包装重量;
填充>>距离;

如果(0
  • 正如billz在评论中指出的,您的if语句是无效的。该语句
    if(0
    
  • 正如billz在评论中指出的,你的if语句是无效的 阅读eof(),它不会做你可能认为它会做的事情

    if( 0 < distance <= 500) // all the if statements are incorrect
    if(distance>0 && distance<=500) // correct
    
    if(0
    
    if( 0 < distance <= 500) // all the if statements are incorrect
    if(distance>0 && distance<=500) // correct
    

    <代码> >(0)距离0 & &为什么你将客户号添加到包计数?所有的if语句,如<代码>如果(500)距离,你也可以考虑改变你的<代码>而 -条件相当大。<代码>(iFuff>客户编号>包装重量>距离)会像你希望的那样完成这个循环,并且一旦遇到不正确的输入就停止处理。为什么要将客户号添加到包计数?所有的if语句,比如<代码>如果(500)你也可以考虑改变你的<代码>而<代码> >条件相当大。<代码>(infle>>customerNumber>>packageWeight>>distance)
    将像您希望的那样完成该循环,并在遇到不正确的输入时立即停止处理。在3中给它一个数字值是什么意思。我修复了第一个(我不知道当初为什么这么做),我不知道2是什么意思)。当我在else中设置distanceRate=0时,它会在每行之后打印出无效的距离。感谢您的帮助我应该使用系统(“暂停”)由于某种原因,因为大学的计算机是被安装的weird@user1807815您使用变量
    packageCount
    会让人立即认为您想要计算处理的包裹总数;但是,您将客户编号相加,可以是除
    1
    1以外的任何数值(参考您的行
    packageCount+=customerNumber
    )。您不能将代码包装在一个块中(请看我的第二次编辑)。最后,您不必“必须”使用
    system(“暂停”)
    ,我保证您可以处理上面的示例。签出。在3中给它一个数字值是什么意思。我修复了第一个(我不知道我为什么要这么做),我不知道你说的2是什么意思。当我在else中设置distanceRate=0时,它会在每行之后打印出无效的距离。谢谢你的帮助我应该使用系统(“暂停”)由于某种原因,因为大学的计算机是被安装的weird@user1807815您使用变量
    packageCount
    会让人立即认为您想要计算处理的包裹总数;但是,您将客户编号相加,可以是除
    1
    1以外的任何数值(参考您的行
    packageCount+=customerNumber
    )。您不能将代码包装在块中(查看我的第二次编辑)。最后,您不必“必须”使用
    system(“暂停”)
    ,我保证您可以使用上面的示例。请检查。
    if (someValueIsTrue) {
        executeThisFunction();
        alsoThisFunction();
    }
    
    if (blah)
        // ....
    else
        distanceRate = 0;
        cout << "Invalid Distance";
    
    !inFile.eof()  // incorrect
     inFile.good() // correct
    
    if( 0 < distance <= 500) // all the if statements are incorrect
    if(distance>0 && distance<=500) // correct