cin.clear()和cin.ignore()不会工作

时间:2016-12-09 16:00:05

标签: c++ string performance function

我对此问题进行了大量研究并找到了一些答案并更正了我的代码,但仍然无法让cin.clear()cin.ignore()工作。这是我所指的代码的一部分:

if(cityname1 == "Error" || cityname2 == "Error")
{
    cout << "**********ERROR! PLEASE ENTER 0-5 FOR YOU LOCATIONS**********" << endl << endl;
    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    detailLoop();
}
else
{
    cout << "The distance from " << cityname1 << " to " << cityname2;
    cout << " is approximately " << cityarray[start][finish] << " miles." << endl << endl;
}

如果用户输入0-5以外的数字,则输出错误消息,但如果他们输入字符,则代码进入无限循环。在做了研究之后,我觉得这段代码应该可行,但不幸的是我无法解决。另外,我是初学者,所以如果有人对这个写的方式有建议,请批评!!!如果有帮助,这是整个功能。我做了#include<limits>

void detailLoop()
{
//initializes array(letters in comments stand for city;
//array values are the distances)
int cityarray[6][6] = {
    //DB-DB,DB-G,DB-J,DB-M,DB-Tall,DB-Tampa
    0,97,90,268,262,130,

    //G-DB,G-G,G-J,G-M,G-Tall,G-Tampa
    97,0,74,337,144,128,

    //J-DB,J-G,J-J,J-M,J-Tall,J-Tampa
    90,74,0,354,174,201,

    //M-DB,M-G,M-J,M-M,M-Tall,M-Tampa
    268,337,354,0,475,269,

    //Tall-DB,Tall-G,Tall-J,Tall-M,Tall-Tall,Tall-Tampa
    262,144,174,475,0,238,

    //Tampa-DB,Tampa-G,Tampa-J,Tampa-M,Tampa-Tall,Tampa-Tampa
    130,128,201,269,238,0
    };

//starting location
int start = 0;
//destination
int finish = 0;
//strings used to print city names
string cityname1;
string cityname2;
//menu block
cout << "Choose locations from the menu below:" << endl;
cout << "0 - Daytona Beach" << endl;
cout << "1 - Gainesville" << endl;
cout << "2 - Jacksonville" << endl;
cout << "3 - Miami" << endl;
cout << "4 - Tallahassee" << endl;
cout << "5 - Tampa" << endl << endl;

//user input for starting city
cout << "Enter your starting location: > "; cin >> start;

//user input for ending city
cout << "Enter your destination: > "; cin >> finish; cout << endl;

//assigns names to cityname1 so it can be printed
switch(start)
{
case 0:
    cityname1 = "Daytona Beach";
    break;
case 1:
    cityname1 = "Gainesville";
    break;
case 2:
    cityname1 = "Jacksonville";
    break;
case 3:
    cityname1 = "Miami";
    break;
case 4:
    cityname1 = "Tallahassee";
    break;
case 5:
    cityname1 = "Tampa";
    break;
default:
    cityname1 = "Error";
    break;
}

//assigns names to cityname2 so it can be printed
switch(finish)
{
case 0:
    cityname2 = "Daytona Beach";
    break;
case 1:
    cityname2 = "Gainesville";
    break;
case 2:
    cityname2 = "Jacksonville";
    break;
case 3:
    cityname2 = "Miami";
    break;
case 4:
    cityname2 = "Tallahassee";
    break;
case 5:
    cityname2 = "Tampa";
    break;
default:
    cityname2 = "Error";
    break;
}

if(cityname1 == "Error" || cityname2 == "Error")
{
    cout << "**********ERROR! PLEASE ENTER 0-5 FOR YOU LOCATIONS**********" << endl << endl;
    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    detailLoop();
}
else
{
    cout << "The distance from " << cityname1 << " to " << cityname2;
    cout << " is approximately " << cityarray[start][finish] << " miles." << endl << endl;
}
}

3 个答案:

答案 0 :(得分:2)

我最终发现所有这些打字的读者都是无用的和疯狂的,试图为阅读互动输入而努力。你基本上最终会与终端和输入缓冲区斗争。

cin.clear()在处理标准输入和解析交互式会话时没有做任何理性的事情。你不应该叫它。

根据cin.getline()重写所有内容并解析生成的字符串,您将能够理解失败模式并修复它们。

答案 1 :(得分:0)

您的代码works fine如果您的NUMBER如下所示:

main()

我怀疑你是在循环中调用int main() { detailLoop(); } 会导致无限循环。

答案 2 :(得分:0)

您应该将cin.fail()与条件一起使用

if(cin.fail()||cityname1 == "Error" || cityname2 == "Error")
{
cout << "**********ERROR! PLEASE ENTER 0-5 FOR YOU LOCATIONS**********" << endl << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
detailLoop();
}