2013-03-04 58 views
2

我正在嘗試從命令行獲取argv []的數字。 換句話說,我試圖達到2從如何從主C++之外的argv []獲取輸入

./calculator -q 2

我目前的設置是沿着線的東西:

#include <iostream> 
using namespace std; 

int check_q(char* argv[]){ 
    float q, qa; 
    if(atoi(argv[1]) == q){ 
     qa = atof(argv[2]); 
    } 
    if(atoi(argv[3]) == q){ 
     qa = atof(argv[4]); 
    } 
    if(atoi(argv[5]) == q){ 
     qa = atof(argv[6]); 
    } 
    if(atoi(argv[7]) == q){ 
     qa = atof(argv[8]); 
    } 
return qa; 
} 

int main(int argc, char *argv[]){ 

    float qa = 0; 
    check_q(argv); 
    cout << qa << endl; 

return 0;} 

任何想法什麼我米做錯了嗎?

+2

在有你的期望沒有得到滿足什麼樣的方式? – 2013-03-04 01:47:08

+2

我認爲你對字符和整數有一些基本的誤解。 「-q」是一個字符串,你應該與strcmp或者它的一個親屬進行比較。 – 2013-03-04 01:48:00

+2

而且你不應該使用==比較int來永遠浮動。 (特別是當其中一個比較項目未初始化時。) – 2013-03-04 01:49:02

回答

2

您沒有檢查argc的值以查看傳遞給程序的參數數量。如果您只傳遞兩個參數,則訪問argv[3]將導致未定義的行爲;所以你必須首先檢查參數的數量。

另外,如果你正在尋找與價值"-q"參數,然後用"-q"比較:

if (std::string(argv[1]) == "-q") 

您將其轉換爲一個數字,與未初始化的變量比較,這不會做任何有用的。

+0

哇。這表明,在一段時間內閃亮你的水晶球_does_有所作爲!好。哎呀。事實證明'-q'來自帖子。我一定很累。 – sehe 2013-03-04 08:12:54

0

關於這段代碼有很多東西沒有很多意義,也不安全或優雅,我不確定你想通過檢查參數1,3 ,5和7,但這裏至少有一個問題:

qamain將永遠不會更新,因爲您不會將check_q的返回值賦值給它。該聲明必須是:

float qa = 0; 
qa = check_q(argv); 

還是真的,只是:

float qa = check_q(argv); 
1

你做任何事情都錯在這裏:

#include <iostream> 
using namespace std; 

int check_q(char* argv[]) 
{ 
    float q, qa;   // you never assign `q` a value, so the following comparisons make no sense 
    if(atoi(argv[1]) == q) // you never check argc in main to determine if argv[whatever] is valid. if the array isn't long enough, this will invoke undefined behavior. 
    { 
     qa = atof(argv[2]); // you're assigning a value to `qa` declared in this function, leaving the one declared in main unchanged. probably not what you intended 
    } 
    // and so on 

    return qa; 
} 

int main(int argc, char *argv[]) 
{ 

    float qa = 0; 
    check_q(argv); // this function never changes the value of `qa` that's declared in main... 
    cout << qa << endl; // ... so this will always print 0 

return 0; 

}

你可能想要做更多的事情:

#include <iostream> 
#include <string> 
#include <vector> 

float check_q(const std::vector<std::string>& args) 
{ 
    if(args[1] == "-q") 
    { 
     return ::atof(args[2].c_str()); 
    } 
    else 
    { 
     return 0.0f; // or some other default 
    } 
} 

int main(int argc, char *argv[]) 
{ 
    const std::vector<std::string> args(argv, argv+argc); 

    if(args.size() >= 3) // argv[0] is usually the name of the executable 
    { 
     std::cout << check_q(argv) << std::endl; 
    } 
    else 
    { 
     std::cout << "not enough args" << std::endl; 
    } 
} 

一旦你有點更有經驗,你會想要使用庫如GNU getoptboost::program_options

2

把它傳遞:

#include <string> 
#include <vector> 
#include <iostream> 
#include <algorithm> 
#include <cassert> 

float check_q(std::vector<std::string> const& args) 
{ 
    int q = 42; 

    for (auto it = args.begin(); it != args.end(); std::advance(it, 2)) 
    { 
     if (std::stoi(*it) == q) 
     { 
      auto next = std::next(it); 
      assert(next != args.end()); 

      return std::stof(*next); 
     } 
    } 

    return 0; 
} 


int main(int argc, const char *argv[]) 
{ 
    const std::vector<std::string> args(argv, argv+argc); 

    // pass it along 
    check_q(args); 
}