2016-05-12 105 views
1

我想要做的是基本上寫一個函數,它將一個參數作爲文件名,如果有多個文件,它只會將文件名作爲參數,並且它應該使用函數重複執行此操作。我怎樣才能做到這一點? 謝謝。如何將文件作爲參數傳遞給C++?

TXT文件都是這樣 例如,sorular.txt:

//什麼是最擁擠的國家在世界上?
//中國
//美國
//德國
//澳大利亞
//中國

int main(){ 

    string array [5]; 
    string line; 
    string answer; 
    static int trueCount = 0; 
    static int falseCount = 0; 
    ifstream file("/Users/User/QuizMaker/Quiz Maker V2/sorular.txt"); 

    if(file.is_open()){ 
     cout << "Questions are loading... Please wait.."<<endl<<" ."<<endl<<" ."<<endl<<" ."<<endl; 
     while (!file.eof()) { 
      for (int i = 0; i<6; i++) { 
       getline(file,array[i]); 
      } 

      for (int a = 0; a<5; a++) { 
        cout << array[a] << endl; 
      } 
      cin >> answer; 

      if(answer == "C" || answer == "c") { 
       cout << true; 
       trueCount++; 
      } 
      else falseCount++; 
     } 

     cout << "You answered "<<trueCount << " questions as true" << endl; 
     cout << "You answered "<<falseCount << " questions as false" << endl; 
     file.close(); 
    } else cout << " not ıoen"; 

    cin.get(); 

    return 0; 
} 
+2

呃......我不認爲你應該在你的學習任務中進一步發展。起初,你只需**就可以做基本的編程教程/課程,瞭解算法,處理器和類似的東西。之後,你可以做一些練習,然後**然後**你可以嘗試自己這樣的東西。 C和C++很容易學習,但**非常**很難掌握...做**不**只是跳過你打算做的幾個步驟......否則你將失敗。首先從基本算法開始,瞭解數組,數據類型,指針......這非常重要 – specializt

+0

當然,您可以接受來自命令行的參數。 main(int argc,char ** argv)'argv'的'argc'和'argv'是這些參數的路由。 – Niall

+1

[順便說一句](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong)。 – user657267

回答

0

首先我要說,那array不能容納的元素012345。它只分配了5個元素。


這可能會擴展你的視野,但我認爲正確的解決方案是一個類。所以我會一起破解這個,如果你有興趣,你可以研究我做了些什麼,如果你不明白的東西可以隨意在評論中提問。

class foo : public iterator<input_iterator_tag, string> { 
    const initializer_list<const char*> _files; 
    decltype(_files)::const_iterator _filesIt; 
    ifstream _file; 
    string _line; 

    void get() { 
     string array[6]; 
     auto i = begin(array); 

     while (i != end(array) && getline(_file, *i)) { 
      advance(i, 1); 
     } 

     if (i == end(array)) { 
      _line = accumulate(cbegin(array), cend(array), string(), [](const auto& a, const auto& b) { return a.empty() ? b : a + '\n' + b; }); 
     } else if(++_filesIt != cend(_files)) { 
      _file.close(); 
      _file.open(*_filesIt); 
      get(); 
     } 
    } 
public: 
    foo() : _filesIt(cend(_files)) {} 
    foo(foo& rhs) : _files(rhs._files), _filesIt(next(cbegin(_files), distance(cbegin(rhs._files), rhs._filesIt))), _line(rhs._line) { 
     if (_filesIt != cend(_files)) { 
      _file.open(*_filesIt); 
      _file.seekg(rhs._file.tellg()); 
     } 
    } 
    foo(const initializer_list<const char*>&& files) : _files(files), _filesIt(cbegin(_files)), _file(*_filesIt) { get(); } 
    const string& operator*() const { return _line; } 
    const foo& operator++() { 
     get(); 
     return *this; 
    } 
    const foo operator++(int) { 
     foo result; 

     get(); 
     return result; 
    } 
    bool operator==(const foo& rhs) { return distance(_filesIt, cend(_files)) == distance(rhs._filesIt, cend(rhs._files)); } 
    bool operator!=(const foo& rhs) { return distance(_filesIt, cend(_files)) != distance(rhs._filesIt, cend(rhs._files)); } 
}; 

雖然這個班似乎不堪重負,但它大大簡化了你試圖做的其他事情。有了這門課程,其餘的代碼將會顯示如下:

auto true_count = 0; 
auto false_count = 0; 

for_each(foo({"/Users/User/QuizMaker/Quiz Maker V2/sorular.txt", "foo.txt", "bar.txt"}), foo(), [&](const auto& i) { 
    string answer; 

    cout << i << endl; 
    cin >> answer; 

    if(answer == "C" || answer == "c") { 
     cout << 1; 
     true_count++; 
    } else { 
     false_count++; 
    } 
}); 
cout << "You answered "<< trueCount << " questions as true" << endl << "You answered " << falseCount << " questions as false" << endl; 
相關問題