2014-10-28 76 views
0

編輯:: 已解決 - 這是由於誤解了getOpt函數的使用。 我在這裏引用了這個人的資料,堆棧溢出(http://linux.die.net/man/3/getopt)和GNU網站上的getOpt文檔:gnu.org/software/libc/manual/html_node/Example-of-Getopt.html感謝Bill Lynch和Remyabel用於引用前面提到的源材料。使用atof時出現奇怪的輸出(optarg)

當我使用-f變量運行「Football」命令並使用-c時,似乎存在一個問題,但是,我主要關注的是現在只能使用一個。

放置在輸入:

-f -p 16 -a 25 -y 267 -t 1 -i 2 

    Gives out:: 
    pC = 0 
    pC = 32738 
    pY = -1052776240 
    T = 32738 
    I = 1 

現在,這些變量應該只吐出正是我放了進去,因爲我使用的唯一轉換(如下圖所示)是X = ATOF(OPTARG ); 我懷疑這可能與ASCII值有關,儘管我幾乎完全無能爲力。

#include <iostream> 
#include <unistd.h> 
#include <cstdlib> 
#include <time.h> 
#include <stdlib.h> 
#include <cmath> 

using namespace std; 

int main(int argc, char *argv[]) 
{ 
    srand(time(NULL)); 
    double r = (6 + (std::rand() % (8 - 6 + 1)))/10; 
    int c; 
    int pA; 
    int pY; 
    int T; 
    int I; 
    int pC; 

    double mass; 
    double bMass; 
    double dist; 
    double velo; 
    double Cr = .001; 
    double k = .18; 
    double g = 9.8; 
    double CFdraft; 
    double Pair; 
    double Proll; 
    double Psec; 
    double timeTravel = 0.0; 
    double Et; 
    double E; 
    double Eavg = 0; 
    int x = 0; 
    double cT; 
    double cC; 
    double cY; 
    double cI; 
    double PasserRating; 

    while ((c = getopt (argc, argv, "c:m:b:v:d:f:p:a:y:t:i:")) != -1) 
    { 
     if (c == 'f') // There seems to be some kind of misunderstanding with what this is doing 

//與c ==「F」線是有指定哪一組的計算來運行的,所以,需要是//最前面變量在的開頭進行檢查程序。 (c =='p') {p = atof(optarg);

   } 
      if (c == 'a') 
       { 
        pA = atof(optarg); 

       } 
      if (c == 'y') 
       { 
        pY = atof(optarg); 

       } 
      if (c == 't') 
       { 
        T = atof(optarg); 

       } 
      if (c == 'i') 
       { 
        I = atof(optarg); 

       } 
      cout << "pC " << pC << endl; 
      cout << "pC " << pA << endl; 
      cout << "pY " << pY << endl; 
      cout << "T " << T << endl; 
      cout << "I " << I << endl; 
      //Calculations 
      cC = ((pC/pA) - 0/30) * 5; 
      cY = ((pY/pA) - 3) * 0.25; 
      cT = ((T/pA) * 20); 
      cI = ((2.375) - (I/pA) * 25); 

      if (cC <= 0) 
       { 
        cC = 0; 
       } 
      if (cC >= 2.375) 
       { 
        cC = 2.375; 
       } 
      if (cY <= 0) 
       { 
        cY = 0; 
       } 
      if (cY >= 2.375) 
       { 
        cY = 2.375; 
       } 
      if (cT <= 0) 
       { 
        cT = 0; 
       } 
      if (cT >= 2.375) 
       { 
        cT = 2.375; 
       } 
      if (cI <= 0) 
       { 
        cI = 0; 
       } 
      if (cI >= 2.375) 
       { 
        cI = 2.375; 
       } 
      PasserRating = (((cC + cY + cT + cI)/6) * 100); 
      string strPR = "Poor"; 

      if (PasserRating <= 85) 
      { 
       strPR = "poor"; 
      } 
      if (PasserRating > 85) 
      { 
       strPR = "mediocre"; 
      } 
      if (PasserRating > 90) 
      { 
       strPR = "good "; 
      } 
      if (PasserRating > 95) 
      { 
       strPR = "great "; 
      } 
      cout << strPR << " " << PasserRating << endl; 
     } 
     if (c == 'c') 
     { 
      if (c == 'm') 
      { 
       mass = atof(optarg); 

      } 
      if (c == 'b') 
      { 
       bMass = atof(optarg); 

      } 
      if (c == 'd') 
      { 
       dist = atof(optarg); 

      } 
      if (c == 'v') 
      { 
       velo = atof(optarg); 
      } 
      timeTravel = (dist * 1000)/velo; 
      cout << "time:" << timeTravel << endl; 
      cout << "mass " << mass << endl; 
      cout << "bMass " << bMass << endl; 
      cout << "dist " << dist << endl; 
      cout << "velo " << velo << endl; 

      for (x = 0; x < (10); x ++) 
      { 
       CFdraft = r; 
       Pair = k * CFdraft * (pow(velo, 3)); 
       Proll = Cr * g * (mass + bMass) * velo; 
       Psec = Pair + Proll; 
       Et = (Psec * timeTravel); 
       E = E + Et; 
       Eavg = E/timeTravel; 
      } 

      cout << Eavg << " KJ" << endl; 
     } 

    } 
    return 0; 
} 
+0

您是否嘗試打印'optarg'的值? – 2014-10-28 17:08:11

+0

請正確縮進您的代碼。很難看到每個塊在哪裏開始和結束。 – interjay 2014-10-28 17:08:14

+0

另外:爲什麼你選擇使用'atof'?這總是一個不好的選擇。 – 2014-10-28 17:08:54

回答

6

我認真推薦正確縮進你的代碼。如果你沒有,你會看到:

if(c == 'f'){ 
    if (c == 'p') 
    ... 
} 

顯然c不會等於'f''p'在同一時間。

+0

在這種情況之後,是否通過了其餘的旗幟?我認爲c僅僅是當前字符的佔位符 – Whatamia 2014-10-28 17:10:08

+0

@whatamia請閱讀[手冊頁](http://linux.die.net/man/3/getopt),瞭解如何使用'getopt'的正確示例。 – 2014-10-28 17:10:51

+2

@Whatamia:你有一個變量'c'。你首先測試該變量是否等於「f」。如果這是__true__,那麼你就比較一下,看看你剛纔驗證的'c'是否有'f'的值,現在有'p'的值。 – 2014-10-28 17:11:14

3

從未執行您的解析代碼 - 一切都在裏面if(c == 'f')條件,這是隻有在第一次運行循環顯然是真的......所以,你剛剛從內存獲取隨機值。