2013-06-05 56 views
0

所以我試圖完成一個程序的一部分,我必須找到向量字符串中的最後n-1個單詞,並將源代碼中的下一個單詞移到向量字符串的末尾。在這裏,我試圖編寫'findAndShift'的函數,並使用另一個程序的參數來使用它。參數 - 命令提示符C++

這是我的程序的一部分。

void findAndShift(vector<string>& ngram, string source[],int sourceLength) { 
    if (argc == 2)///default command prompt...not declared? 
    { 
     ifstream infile; 
     infile.open(argv[1], ios::in); 
     if (infile.fail()) 
     { 
      cout << argv[0] << ": "<< argv[i] << "I'm afraid I can't let you do that, Dave." << endl; 
     } 
     else 
     { 
      //get length of n 
      infile.seekg(0, ios::end); 
      const int sourceLength = infile.tellg(); 

      int n = 0; 
      string word; 
      ngram = rand() % sourceLength; 
      while (!infile.eof()) 
      { 
       infile >> source; 
       ++n; 
       if(counter == ngram); 
        word = n; 
      } 
     } 
    } 
    return; 
} 

這是我必須使用的程序。

string source[250000]; 
vector<string> ngram; 
int main(int argc, char* argv[]) { 
    int n, outputN, sl; 
    n = 3; 
    outputN = 100; 
    for (int i = 0; i < argc; i++) { 
     if (string(argv[i]) == "--seed") { 
      srand(atoi(argv[i+1])); 
     } else if (string(argv[i]) == "--ngram") { 
      n = 1 + atoi(argv[i+1]); 
     } else if (string(argv[i]) == "--out") { 
      outputN = atoi(argv[i+1]); 
     } else if (string(argv[i]) == "--help") { 
      help(argv[0]); 
      return 0; } 
    } 
    fillWordList(source,sl); 
    cout << sl << " words found." << endl; 
    cout << "First word: " << source[0] << endl; 
    cout << "Last word: " << source[sl-1] << endl; 
    for (int i = 0; i < n; i++) { 
     ngram.push_back(source[i]); 
    } 
    cout << "Initial ngram: "; 
    put(ngram); 
    cout << endl; 
    for (int i = 0; i < outputN; i++) { 
     if (i % 10 == 0) { 
      cout << endl; 
     } 
     //put(ngram); 
     //cout << endl; 
     cout << ngram[0] << " "; 
     findAndShift(ngram, source, sl); 
    } } 

任何想法?

+0

和什麼問題? – greedybuddha

+0

它只在'main'中可見。 – chris

+0

將argc作爲參數傳入findAndShift? –

回答

0

您必須在程序argc和argv []中傳遞更多參數,因爲argc只是main函數的參數,因此在其他函數中不可見。

這是您的修改代碼。

void findAndShift(vector<string>& ngram, string source[],int sourceLength, int argc, char argv[0], argv[1],argv[i]){ 
    if (argc == 2)///default command prompt...not declared? 
    { 
     ifstream infile; 
     infile.open(argv[1], ios::in); 
     if (infile.fail()) 
     { 
      cout << argv[0] << ": "<< argv[i] << "I'm afraid I can't let you do that, Dave." << endl; 
     } 
     else 
     { 
      //get length of n 
      infile.seekg(0, ios::end); 
      const int sourceLength = infile.tellg(); 

      int n = 0; 
      string word; 
      ngram = rand() % sourceLength; 
      while (!infile.eof()) 
      { 
      infile >> source; 
      ++n; 
      if(counter == ngram); 
      word = n; 
      } 
     } 
    } 
    return; 
} 

主代碼

string source[250000]; 
vector<string> ngram; 
int main(int argc, char* argv[]) { 
    int n, outputN, sl; 
    n = 3; 
    outputN = 100; 
    for (int i = 0; i < argc; i++) { 
    if (string(argv[i]) == "--seed") { 
     srand(atoi(argv[i+1])); 
    } else if (string(argv[i]) == "--ngram") { 
     n = 1 + atoi(argv[i+1]); 
    } else if (string(argv[i]) == "--out") { 
     outputN = atoi(argv[i+1]); 
    } else if (string(argv[i]) == "--help") { 
     help(argv[0]); 
return 0; } 
    } 
    fillWordList(source,sl); 
    cout << sl << " words found." << endl; 
    cout << "First word: " << source[0] << endl; 
    cout << "Last word: " << source[sl-1] << endl; 
    for (int i = 0; i < n; i++) { 
    ngram.push_back(source[i]); 
    } 
    cout << "Initial ngram: "; 
    put(ngram); 
    cout << endl; 
    for (int i = 0; i < outputN; i++) { 
if (i % 10 == 0) { 
    cout << endl; 
} 
//put(ngram); 
//cout << endl; 
cout << ngram[0] << " "; 
findAndShift(ngram, source, sl, argc,argv[0],argv[1],argv[i]); 
} } 

我好久沒有測試它。 如果仍然不行嘗試使用字符*在findAndShift

希望它可以幫助的參數列表....