C++ 程序中的奇怪行为
Odd behavior in C++ program
我正在编写一个允许用户指定要打开的输入文件的程序,当我使用不正确的文件名进行测试时,该程序表现得很奇怪,似乎与输入缓冲区有关,但除了使用 getline()
而不是 cin >>
之外,我不知道从哪里开始,但我已经尝试过了。
这是我认为可能有问题的代码:
bool openfile(ifstream&);
string userInput();
int main()
{
// ...
while (!openfile(inputFile))
openfile(inputFile);
string input = userInput();
// ...
}
bool openfile(ifstream &inputFile)
{
string filename;
cout << "Please enter the name of the file or type quit to exit the program: ";
cin >> filename;
cout << endl;
if (filename == "quit")
exit(4);
else
inputFile.open(filename);
if (!inputFile)
{
cout << "The file \"" << filename << "\" could not be opened or does not exist.\n";
return false;
}
return true;
}
string userInput()
{
string englishSentence;
cout << "Please enter a sentence or type quit to exit the program: \n";
getline(cin, englishSentence);
if (englishSentence == "quit")
exit(4);
return englishSentence;
}
这是读取任何输入的两个函数。如您所见,openfile()
首先被调用。任何帮助是极大的赞赏。如果您怀疑我的代码中有其他内容,请告诉我,我会粘贴它。
你可以这样做:
while (!openfile(inputFile));
按照您的方式,如果第一次失败,它会请求输入文件名两次。
大致概括一下问题:
开始循环
- 请求文件名
- 无效文件名
- 请求替换文件名
- Return 开始循环(再次检查)
我从你的代码中看到的一些问题:
int main(); { ... }
没有定义 main
函数。你需要去掉分号,否则它甚至不会编译。
while (!openfile(inputFile)) openfile(inputFile);
不必要地重复 openfile(inputFile)
。如果第一个(在条件中)失败而第二个(在主体中)成功,则将进行第三次调用(在条件中)以检查循环是否应继续。您可能想要的只是 while (!openfile(inputFile)) { }
.
- 您在
openfile
中打开了一个文件,并且在随后的 userInput
中从未使用过它。
while (!openfile(inputFile))
openfile(inputFile);
这样做是每次迭代尝试打开文件两次,只要第一次尝试失败即可。此外,您需要确保 inputFile
在尝试再次打开它之前已关闭,因为您似乎在重复使用同一个文件对象。
当然要先尝试类似的东西:
while (!openfile(inputFile))
;
我正在编写一个允许用户指定要打开的输入文件的程序,当我使用不正确的文件名进行测试时,该程序表现得很奇怪,似乎与输入缓冲区有关,但除了使用 getline()
而不是 cin >>
之外,我不知道从哪里开始,但我已经尝试过了。
这是我认为可能有问题的代码:
bool openfile(ifstream&);
string userInput();
int main()
{
// ...
while (!openfile(inputFile))
openfile(inputFile);
string input = userInput();
// ...
}
bool openfile(ifstream &inputFile)
{
string filename;
cout << "Please enter the name of the file or type quit to exit the program: ";
cin >> filename;
cout << endl;
if (filename == "quit")
exit(4);
else
inputFile.open(filename);
if (!inputFile)
{
cout << "The file \"" << filename << "\" could not be opened or does not exist.\n";
return false;
}
return true;
}
string userInput()
{
string englishSentence;
cout << "Please enter a sentence or type quit to exit the program: \n";
getline(cin, englishSentence);
if (englishSentence == "quit")
exit(4);
return englishSentence;
}
这是读取任何输入的两个函数。如您所见,openfile()
首先被调用。任何帮助是极大的赞赏。如果您怀疑我的代码中有其他内容,请告诉我,我会粘贴它。
你可以这样做:
while (!openfile(inputFile));
按照您的方式,如果第一次失败,它会请求输入文件名两次。
大致概括一下问题:
开始循环
- 请求文件名
- 无效文件名
- 请求替换文件名
- Return 开始循环(再次检查)
我从你的代码中看到的一些问题:
int main(); { ... }
没有定义main
函数。你需要去掉分号,否则它甚至不会编译。while (!openfile(inputFile)) openfile(inputFile);
不必要地重复openfile(inputFile)
。如果第一个(在条件中)失败而第二个(在主体中)成功,则将进行第三次调用(在条件中)以检查循环是否应继续。您可能想要的只是while (!openfile(inputFile)) { }
.- 您在
openfile
中打开了一个文件,并且在随后的userInput
中从未使用过它。
while (!openfile(inputFile))
openfile(inputFile);
这样做是每次迭代尝试打开文件两次,只要第一次尝试失败即可。此外,您需要确保 inputFile
在尝试再次打开它之前已关闭,因为您似乎在重复使用同一个文件对象。
当然要先尝试类似的东西:
while (!openfile(inputFile))
;