2013-02-19 32 views
0

我寫了代碼,它的工作原理除了總數是錯誤的。它應該乘以distanceRate的速度,並增加每個成本來使總和,但它沒有這樣做。任何幫助,將不勝感激。簡單的C++輸入文件和if語句

#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; 
} 
+0

爲什麼要將客戶編號添加到包數? – 2013-02-19 01:10:51

+5

all if語句,比如'if(500 billz 2013-02-19 01:11:33

+1

你也可以考慮改變'while'條件。 'while(inFile >> customerNumber >> packageWeight >> distance)'會像你期望的那樣完成循環,並且一旦遇到不正確的輸入就停止處理。 – WhozCraig 2013-02-19 01:23:50

回答

3

,我在摘要中看到一些問題,你給我如下:

  1. 正如在評論中指出billz,你的if語句是無效的。聲明if(0 < distance <= 500)沒有達到你期望的水平,它從左到右評估,所以你有0 < distance(可以說評估結果爲true),那麼你有true <= 1000這是不會給你認爲會的結果。這實際上需要分解成兩個單獨的比較,如distance > 0 && distance < 500

  2. 正如我在我的評論中指出的那樣,您將客戶編號添加到包數中,這很可能總會給包計數帶來錯誤的值。如果你的客戶號碼是1,2,3,4,那麼你聲稱包裹數量實際上只有4個(如果我誤解了這個領域的目的,請原諒我)。

  3. 對於distanceRate,您沒有默認值,但您仍然在可能會給出意外結果的操作(可能未初始化)中使用它(如您所見)。在你的其他方面,你應該給它一個虛擬的價值,這樣你就可以保證它永遠都會被設置。你也重置它,所以如果它被設置爲4,然後下一個距離測試失敗並且輸入else,那麼對變量的另一個計算是4,而不是它的默認值。你應該初始化你打算使用的任何變量,除非你有明確的理由不要在初始化時給它一個值,並且任何時候你在一個循環中使用一個變量,你應該在循環開始時重置它的值。

附註(EDIT)


我不建議使用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 /時/爲/做......而在/ etc執行多個語句...你需要一個代碼塊。我想(根據您的解釋),你做的事:

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

,編譯器只能看到你有嵌套的循環中distanceRate = 0,在cout說法其實不是別人的一部分,但先前塊的一部分的代碼。你需要在這裏使用一個代碼塊。

+0

你是什麼意思,在3)給它一個數值。我修正了第一個問題(我不知道爲什麼我首先這樣做),我不知道你是什麼意思2)。當我在else中設置distanceRate = 0時,它會在每行之後打印出無效距離。感謝您的幫助 – user1807815 2013-02-19 01:36:30

+0

因爲某些原因,我應該使用系統(「暫停」),因爲大學計算機設置很奇怪 – user1807815 2013-02-19 01:40:47

+0

@ user1807815您對變量'packageCount'的使用使得您立即認爲您要計算總數已處理的包;但是,您將加入的客戶編號可以是除1之外的任何數值(引用您的行'packageCount + = customerNumber')。你不能將代碼封裝在一個塊中(看看我的第二個編輯)。最後,你不必「使用'system(」pause「)',我保證你可以繞過上面的例子。檢查[this](http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong)。 – 2013-02-19 16:08:43

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條件的方式,它不會做你認爲它做的事。