【发布时间】:2016-09-08 00:55:12
【问题描述】:
我想做的基本上是编写一个函数,该函数将参数作为文件名,如果有多个文件,它将只将文件名作为参数,并且应该使用函数重复执行此操作。我怎样才能做到这一点? 谢谢。
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;
}
【问题讨论】:
-
当然,您可以接受来自命令行的参数。
main(int argc, char** argv)中的argc和argv是通往这些论点的途径。 -
请问如何写一个以
std::string为参数的函数?我觉得没有什么可以帮你的。你应该只学习最基本的。 -
非常感谢您的建议。我实际上也在学习它们。但是,在其中一个项目中,我需要做这种事情。你对这个问题的解决方案是什么?我真的错过了哪里?谢谢@specializt
-
@oguzdumanoglu:请忽略specializt 的建议。在 C++ 中,指针并不那么重要。对于数组,使用
std::array<std::string, 5>而不是您现在拥有的数组类型std::string[5]。
标签: c++ file loops input ifstream