ユーザーが文字や文字列ではなく整数を入力した場合、プログラムをチェックインする必要があります。文字は実質的に整数であるため、それほど悪くはありませんが、ユーザーが一連の文字を入力すると、おかしくなります。
私はこの機能を作りました
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;
}