C++ 使用ifstreamwhile循环,如何显示输入错误并在下一行继续?

C++ 使用ifstreamwhile循环,如何显示输入错误并在下一行继续?,c++,while-loop,ifstream,C++,While Loop,Ifstream,如果我的输入文件以字母开头,它将停止while循环,因为它无法重写int1,但我知道如何检测到这一点并显示错误消息,说明workinfile>>int1不起作用,然后继续循环 cin>>filename; ifstream workingfile(filename); while (workingfile>>int1>>int2>>string1>>string2) { cout<<int1<<int2

如果我的输入文件以字母开头,它将停止while循环,因为它无法重写
int1
,但我知道如何检测到这一点并显示错误消息,说明
workinfile>>int1
不起作用,然后继续循环

cin>>filename;
ifstream workingfile(filename);

while (workingfile>>int1>>int2>>string1>>string2) {
    cout<<int1<<int2<<string1<<string2<<endl;
    linenumread++;
}

我想检测它何时命中无效输入,显示错误消息,然后继续文件中的下一行。

对于这种输入,通常最好读取完整的一行,然后从该行提取值。如果无法解析该行,可以报告该行的失败,然后从下一行开始继续

看起来是这样的:

std::string line;
while (std::getline(workingfile, line)) // Read a whole line per cycle
{
    std::istringstream workingline(line); // Create a stream from the line
    // Parse all variables separately from the line's stream
    if(!(workingline>>int1))
    {
       cout<<"Error first value is not an integer"<<endl;
       continue;
    }
    if(!(workingline>>int2)
    {
       cout<<"Error second value is not an integer"<<endl;
       continue;
    }
    // ^^^^ a.s.o. ...
    cout<<int1<<int2<<string1<<string2<<endl;
    linenumread++;
}
std::字符串行;
while(std::getline(workingfile,line))//每个周期读取一整行
{
std::istringstream workingline(line);//从该行创建流
//从行的流中分别分析所有变量
如果(!(工作线>>int1))
{

cout对于此类输入,通常最好读取完整的一行,然后从该行中提取值。如果无法解析该行,则可以报告该行的失败,然后从下一行开始继续

看起来是这样的:

std::string line;
while (std::getline(workingfile, line)) // Read a whole line per cycle
{
    std::istringstream workingline(line); // Create a stream from the line
    // Parse all variables separately from the line's stream
    if(!(workingline>>int1))
    {
       cout<<"Error first value is not an integer"<<endl;
       continue;
    }
    if(!(workingline>>int2)
    {
       cout<<"Error second value is not an integer"<<endl;
       continue;
    }
    // ^^^^ a.s.o. ...
    cout<<int1<<int2<<string1<<string2<<endl;
    linenumread++;
}
std::字符串行;
while(std::getline(workingfile,line))//每个周期读取一整行
{
std::istringstream workingline(line);//从该行创建流
//从行的流中分别分析所有变量
如果(!(工作线>>int1))
{

cout while循环将在任何无效输入时停止。最好的方法是在循环条件中使用
std::getline()
读取完整的行,并使用
std::istringstream
解析字符串并检查每个值。欢迎使用堆栈溢出!您的代码不完整;特别是,它似乎缺少
main()
函数和至少一个
#include
。请输入您的代码,使其成为您的问题的一部分,然后我们可以尝试复制并解决它。您还应该阅读。@TobySpeight这里并不需要提供完整的代码。给定的示例中的问题非常明显。我今天看到了更糟糕的问题。while循环我将在任何无效输入时停止。最好的方法是在循环条件中使用
std::getline()
读取完整的行,并使用
std::istringstream
解析字符串并检查每个值。欢迎使用堆栈溢出!您的代码不完整;特别是,它似乎缺少
main()
函数和至少一个
#include
。请输入您的代码,这样它就是您的问题的一部分,然后我们可以尝试复制并解决它。您还应该阅读。@TobySpeight这里不需要提供完整的代码。给定的示例中问题非常明显。我今天看到了更糟糕的问题。我确实尝试过也使用getline方法,但我无法将它们拆分为单独的变量,我不知道istringstream,我要去查一下,谢谢。我只是想让你知道这对我的项目有很大帮助,我正在浏览我的项目,非常感谢。我实际上也尝试过使用getline方法,但我无法拆分我不知道istringstream我要去查一下谢谢我只是想让你知道这对我的项目有很大的帮助,我正在浏览我的项目,非常感谢你。
std::string line;
while (std::getline(workingfile, line)) // Read a whole line per cycle
{
    std::istringstream workingline(line); // Create a stream from the line
    // Parse all variables separately from the line's stream
    if(!(workingline>>int1))
    {
       cout<<"Error first value is not an integer"<<endl;
       continue;
    }
    if(!(workingline>>int2)
    {
       cout<<"Error second value is not an integer"<<endl;
       continue;
    }
    // ^^^^ a.s.o. ...
    cout<<int1<<int2<<string1<<string2<<endl;
    linenumread++;
}