如何正确使用 "getline(cin, input);"
How do I correctly use "getline(cin, input);"
我正在编写一个程序来转换用户提供的文本。我已经在测试程序中单独尝试过这种方法,并且效果很好;但是,当我尝试将其实现到更大的程序中时,用户无法为程序提供要存储的输入。相关代码如下:
int main()
{
string str = "NULL";
int mappings = 0;
readMappings(mappings);
receiveInput(str);
displayInput(mappings, str);
return 0;
}
void readMappings(int &count)
{
ifstream readMappings; // Creates the function "readMappings"
string filename; // Generates the filename variable to search for mapping document
cout << "Enter the filename of the mapping document: ";
cin >> filename; // User enters filename
readMappings.open(filename); // "readMappings" function opens the given mappings document
while (!readMappings.is_open())
{
cout << "Unsble to open file. Please enter a valid filename: "; // If the user enters an invaled filename, the program will ask again
cin >> filename;
readMappings.open(filename);
}
if (readMappings.good()) // Mapping document is successfully opened
{
readMappings >> count; // Reads first line
}
readMappings.close(); // If everything fails in this function, the document will close
}
void receiveInput(string &input)
{
char correctness;
do {
cout << "\nPlease enter the text you would like to be converted to NATO:\n";
getline(cin, input);
cout << "You are about to convert: \"" << input << "\".\nIs this correct? (Y/N)" << endl;
cin >> correctness;
} while (correctness == 'N' || correctness =='n');
}
我认为可能是程序在等待用户的另一个输入,所以我添加了一个我认为它已经填充的变量,但它没有解决我的解决方案。在我看来,问题出在 receiveInput 函数中,但我可能是错的。在此先感谢您的帮助。
此外,我正在使用具有正确参考变量的函数原型。
我看到两个问题:
1) 在调用 std::getline()
.
后,您没有检查 EOF 条件
2) 您将 std::getline
和 >>
运算符混合在一起。现在,这实际上在技术上没有任何问题,但是 std::getline
和 >>
运算符都具有非常 细微差别 语义,当涉及到错误检查和输入消耗时,你需要 100% 正确,才能正确地一起使用它们。
只需将 >>
运算符的用法替换为 std::getline
,这样您就可以专门使用 std::getline
,并确保检查 fail
() 或eof
()。您会在 whosebug.com.
上找到大量有关如何使用 std::getline
正确检查文件结尾和失败条件的示例
我正在编写一个程序来转换用户提供的文本。我已经在测试程序中单独尝试过这种方法,并且效果很好;但是,当我尝试将其实现到更大的程序中时,用户无法为程序提供要存储的输入。相关代码如下:
int main()
{
string str = "NULL";
int mappings = 0;
readMappings(mappings);
receiveInput(str);
displayInput(mappings, str);
return 0;
}
void readMappings(int &count)
{
ifstream readMappings; // Creates the function "readMappings"
string filename; // Generates the filename variable to search for mapping document
cout << "Enter the filename of the mapping document: ";
cin >> filename; // User enters filename
readMappings.open(filename); // "readMappings" function opens the given mappings document
while (!readMappings.is_open())
{
cout << "Unsble to open file. Please enter a valid filename: "; // If the user enters an invaled filename, the program will ask again
cin >> filename;
readMappings.open(filename);
}
if (readMappings.good()) // Mapping document is successfully opened
{
readMappings >> count; // Reads first line
}
readMappings.close(); // If everything fails in this function, the document will close
}
void receiveInput(string &input)
{
char correctness;
do {
cout << "\nPlease enter the text you would like to be converted to NATO:\n";
getline(cin, input);
cout << "You are about to convert: \"" << input << "\".\nIs this correct? (Y/N)" << endl;
cin >> correctness;
} while (correctness == 'N' || correctness =='n');
}
我认为可能是程序在等待用户的另一个输入,所以我添加了一个我认为它已经填充的变量,但它没有解决我的解决方案。在我看来,问题出在 receiveInput 函数中,但我可能是错的。在此先感谢您的帮助。
此外,我正在使用具有正确参考变量的函数原型。
我看到两个问题:
1) 在调用 std::getline()
.
2) 您将 std::getline
和 >>
运算符混合在一起。现在,这实际上在技术上没有任何问题,但是 std::getline
和 >>
运算符都具有非常 细微差别 语义,当涉及到错误检查和输入消耗时,你需要 100% 正确,才能正确地一起使用它们。
只需将 >>
运算符的用法替换为 std::getline
,这样您就可以专门使用 std::getline
,并确保检查 fail
() 或eof
()。您会在 whosebug.com.
std::getline
正确检查文件结尾和失败条件的示例