我的回圈適用于用戶輸入不在串列中的數字但當我插入字符或字串時,回圈不起作用的情況。我想讓它繼續回圈,只要 switch 陳述句大小寫是默認的,不管它是數字還是字符。我試過choice!=int 和choice==string 來創建一個while 回圈,但顯然這是不可能的。
void userDetails();
int main()
{
userDetails();
return 0;
}
void userDetails()
{
cout << "Select a username below:" << endl;
cout << "1. " << firstname lastname << flush << endl;
cout << "2. " << lastname firstname << flush << endl;
cout << "3. " << firstname "254" << flush << endl;
cout << "4. " << lastname "254" << flush << endl;
cout << "5. Enter other username:" << flush << endl;
int choice;
cin >> choice;
switch (choice)
{
case 1:
username = firstname lastname;
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 2:
username = lastname firstname;
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 3:
username = firstname "254";
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 4:
username = lastname "254";
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 5:
cout << "Create a username:" << endl;
cin >> username;
cout << "You have selected " << username << " as your username" << flush << endl;
break;
default:
cout << "Invalid option, please try again!!!!";
break;
}
while (choice > 5)
{
cout << "Select a username below:" << endl;
cout << "1. " << firstname lastname << flush << endl;
cout << "2. " << lastname firstname << flush << endl;
cout << "3. " << firstname "254" << flush << endl;
cout << "4. " << lastname "254" << flush << endl;
cout << "5. Enter other username:" << flush << endl;
cin >> choice;
switch (choice)
{
case 1:
username = firstname lastname;
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 2:
username = lastname firstname;
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 3:
username = firstname "254";
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 4:
username = lastname "254";
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 5:
cout << "Create a username:" << endl;
cin >> username;
cout << "You have selected " << username << " as your username" << flush << endl;
break;
default:
cout << "Invalid option, please try again!!!!";
break;
}
}
while (choice ==0)
{
cout << "Select a username below:" << endl;
cout << "1. " << firstname lastname << flush << endl;
cout << "2. " << lastname firstname << flush << endl;
cout << "3. " << firstname "254" << flush << endl;
cout << "4. " << lastname "254" << flush << endl;
cout << "5. Enter other username:" << flush << endl;
cin >> choice;
switch (choice)
{
case 1:
username = firstname lastname;
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 2:
username = lastname firstname;
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 3:
username = firstname "254";
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 4:
username = lastname "254";
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 5:
cout << "Create a username:" << endl;
cin >> username;
cout << "You have selected " << username << " as your username" << flush << endl;
break;
default:
cout << "Invalid option, please try again!!!!";
break;
}
}
}
uj5u.com熱心網友回復:
您可以使用std::cin.failor std::cin.operator bool(它是 的倒數fail)來確定輸入轉換是否成功。
此外,您的代碼有很多不必要的代碼重復,這可能也不起作用。您需要的是一個無限回圈,只有在確定輸入有效后您才會跳出該回圈。
#include <iostream>
#include <string>
#include <limits>
#include <cctype>
void userDetails();
int main()
{
userDetails();
return 0;
}
void userDetails()
{
std::string firstname{ "John" };
std::string lastname{ "Doe" };
std::string username;
while ( true ) //infinite loop
{
char c;
bool has_invalid_characters = false;
std::cout <<
"Select a username below:\n" <<
"1. " << firstname lastname << '\n' <<
"2. " << lastname firstname << '\n' <<
"3. " << firstname "254" << '\n' <<
"4. " << lastname "254" << '\n' <<
"5. Enter other username\n" <<
"\n" <<
"Please select an option: ";
int choice;
std::cin >> choice;
if ( !std::cin ) //this uses std::cin.operator bool
{
std::cout << "Input must be a number!\n\n";
//clear failure flag
std::cin.clear();
//discard remainder of line from input stream
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
continue;
}
//check whether remainder of line consists only of
//whitespace characters
while ( std::cin.get( c ) && c != '\n' )
{
if ( !std::isspace( static_cast<unsigned char>(c) ) )
{
has_invalid_characters = true;
break;
}
}
if ( has_invalid_characters )
{
std::cout << "Invalid character found after number!\n\n";
//discard remainder of line from input stream
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
continue;
}
switch ( choice )
{
case 1:
username = firstname lastname;
std::cout << "You have selected " << username << " as your username.\n";
break;
case 2:
username = lastname firstname;
std::cout << "You have selected " << username << " as your username.\n";
break;
case 3:
username = firstname "254";
std::cout << "You have selected " << username << " as your username.\n";
break;
case 4:
username = lastname "254";
std::cout << "You have selected " << username << " as your username.\n";
break;
case 5:
std::cout << "Create a username:\n";
std::cin >> username;
std::cout << "You have selected " << username << " as your username.\n";
break;
default:
std::cout << "Invalid option, please try again!\n\n";
continue;
}
//input was valid, so break out of infinite loop
break;
}
}
該程式具有以下行為:
Select a username below:
1. JohnDoe
2. DoeJohn
3. John254
4. Doe254
5. Enter other username
Please select an option: ThisIsAString
Input must be a number!
Select a username below:
1. JohnDoe
2. DoeJohn
3. John254
4. Doe254
5. Enter other username
Please select an option: 34
Invalid option, please try again!
Select a username below:
1. JohnDoe
2. DoeJohn
3. John254
4. Doe254
5. Enter other username
Please select an option: 2abc
Invalid character found after number!
Select a username below:
1. JohnDoe
2. DoeJohn
3. John254
4. Doe254
5. Enter other username
Please select an option: 2
You have selected DoeJohn as your username.
uj5u.com熱心網友回復:
我認為您可以執行以下操作。
- 讀取字串而不是整數。
- 將字串決議為整數。
- 如果有決議錯誤或小于1或大于5,再次運行while回圈。
uj5u.com熱心網友回復:
您可以宣告一個字符并僅在它實際上是數字時才將其用作數字,而不是宣告 anint并將其用作數字,例如(偽代碼):
char str_choice;
cin >> str_choice;
if ((str_choice <= '9') && (str_choice >= 0))
{
int choice = (int)str_choice - (int)'0'; // this part you might need to rework
switch (choice)
...
} else
cout << "You have not entered an integer, so you have lost the game" << endl;
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/430117.html
標籤:C
下一篇:陣列k個可能元素的最大連續和
