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

时间:2013-02-19 01:08:05

标签: c++ if-statement file-io

我编写了代码并且它的工作原理除了总数是错误的。它应该将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;
}

2 个答案:

答案 0 :(得分:3)

我在您提供给我的代码段中看到的一些问题如下:

  1. 正如billz在评论中所指出的,您的if语句无效。语句if( 0 < distance <= 500)没有按照您的期望进行,它从左到右进行评估,因此您有0 < distance(假设评估为true),那么您有true <= 1000这不会给你认为会产生的结果。实际上,这需要分为两个单独的比较,如distance > 0 && distance < 500

  2. 正如我在评论中指出的那样,您将客户编号添加到包裹计数中,这很可能总是会给包裹计数带来错误的值。如果您的客户编号为1,2,3,4,那么您声称包裹数量为10时实际上只有4(如果我误解了该字段的用途,请原谅我)。

  3. distanceRate没有默认值,但您仍然在操作中使用它(可能未初始化),这会产生意外结果(如您所见)。在你的其他地方,你应该给它一个虚拟值,以保证它总是被设置。你也可以重置它,所以如果它被设置为4,然后下一个距离未通过测试并输入else,你对变量的另一个计算为4而不是它的默认值。您应该初始化您计划使用的任何变量,除非您有明确的理由不在初始化时给它一个值,并且只要您在循环中使用变量,就应该在循环开始时重置它的值。

    < / LI>

    附加说明(编辑)


    我不建议使用system("pause");因为它在幕后做的比你想要的简单停顿要多得多,我见过的更好的方法是:

    #include <iostream>
    #include <conio.h>
    using namespace std;
    
    int main() {
        cout << "Press any key to continue!";
        _getch();
        cout << "Finished";
    
        return 0;
    }
    

    编辑2


    如果语句可以包含一行要执行的代码块。

    单行:

    if (someValueIsTrue)
        executeThisFunction();
    

    代码块:

    if (someValueIsTrue) {
        executeThisFunction();
        alsoThisFunction();
    }
    

    任何时候你需要在if / else / while / for / do中执行多个语句...而/ etc ...你需要一个代码块。我想(根据你的解释)你做了:

    if (blah)
        // ....
    else
        distanceRate = 0;
        cout << "Invalid Distance";
    

    编译器只看到嵌套在循环中的distanceRate = 0cout语句实际上不是其他部分,而是前一代码块的一部分。您需要在此处使用代码块。

答案 1 :(得分:0)

!inFile.eof()  // incorrect
 inFile.good() // correct

阅读eof()它没有做你想象的那样。

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

你编写if条件的方式,它没有按照你的想法做到。

相关问题