如果用户输入整数而不是字符或字符串,我需要检查我的程序。 字符并没有那么糟糕,因为它实际上是一个整数,但如果用户输入一个字符序列,那么它只是疯了。如何检查用户是否输入整数
我做了这个功能
int* ask_lung(int* lung)
{
int tmp; // length of a word
cout << "Inserisci la lunghezza della parola da indovinare: ";
cin >> tmp;
if(cin)
{
// Se i è uguale a o minore di 0 allora ritorna all'inizio
if(tmp <= 0)
{
cout << endl << "\tNon puoi inserire 0." << endl << endl;
ask_lung(lung);
}
else
{
// the error is about here, when it reaches this part of the code it keeps showing the first line "Inserisci la lunghezza della parola da indovinare: "
*lung = tmp;
}
}
else ask_lung(lung);
return lung;
}
这是做什么/应该做什么? – asheeshr
while(!(cin >> myInt)){std :: cout <<“您的输入不是int”<< std :: endl} 或者我明白错了什么? – demonking
如果tmp <= 0或一个字符或字符序列它应该打印一个错误,并再次要求输入 – DomeWTF