gpt4 book ai didi

c++ - cin.clear() 和 cin.ignore() 将不起作用

转载 作者:行者123 更新时间:2023-11-28 01:56:23 26 4
gpt4 key购买 nike

<分区>

我对这个问题做了很多研究,找到了一些答案并更正了我的代码,但我仍然无法得到 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;
}
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com