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;
}
呃......我不認爲你應該在你的學習任務中進一步發展。起初,你只需**就可以做基本的編程教程/課程,瞭解算法,處理器和類似的東西。之後,你可以做一些練習,然後**然後**你可以嘗試自己這樣的東西。 C和C++很容易學習,但**非常**很難掌握...做**不**只是跳過你打算做的幾個步驟......否則你將失敗。首先從基本算法開始,瞭解數組,數據類型,指針......這非常重要 – specializt
當然,您可以接受來自命令行的參數。 main(int argc,char ** argv)'argv'的'argc'和'argv'是這些參數的路由。 – Niall
[順便說一句](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong)。 – user657267