我一直在用 C 構建一個選單驅動的控制臺,我目前正在使用 switch-case 作為我的選項,但現在我被困在了 switch-case 中。
這是場景:
設想
解釋:在主選單中輸入無效選項后,它給出了一個錯誤提示用戶重新輸入他們想要的選項,現在我的問題是當用戶第二次輸入正確的選項時,它會回圈回到主選單而不是將其重定向到下一個選單。
我的目標:直接從默認選單進入第二個選單,而不重新顯示主選單。
我的部分代碼:
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int choice;
int booknumber;
int booktitle;
int author;
int datepublished;
int e = 0;
void menu();
void inputbook();
void searchbook();
void borrowbook();
void exit();
//CLASS
class Books
{
public:
int booknumber;
string booktitle;
string author;
string datepublished;
Books(const int booknumber, const string booktitle, const string author, const string datepublished) : booknumber(booknumber), booktitle(booktitle), author(author), datepublished(datepublished) {}
};
//MAIN
int main()
{
while (true)
{
cout << endl;
if (e == 1)
{
break;
}
menu ();
}
return 0;
}
//MENU
void menu()
{
cout << "Welcome to DLC Library System\n";
cout << "Final Project in Advance Programming\n\n";
cout << "PROGRAMMER\n";
cout << "ME\n\n";
cout << "====================================\n";
cout << "[1] -------- Input Book ------------\n";
cout << "[2] -------- Search Book -----------\n";
cout << "[3] -------- Borrow Book -----------\n";
cout << "[4] -------- Exit Program ----------\n";
cout << "====================================\n";
cout << "Input your choice (Number Only): ";
cin >> choice;
switch (choice)
{
case 1:
inputbook ();
break;
case 2:
searchbook ();
break;
case 3:
borrowbook ();
break;
case 4:
exit();
break;
default:
while (choice < 1 || choice > 4)
{
cout << "Wrong Option\n";
cout << "Input your choice (Number Only): ";
cin >> choice;
if (choice < 1 || choice > 4)
{
continue;
}
}
}
}
// INPUT BOOK
void inputbook ()
{
int booknumber;
string booktitle;
string author;
string datepublished;
cout << "INPUT NEW BOOK\n\n";
cout << "Book Number: \n";
cin >> booknumber;
cout << "Book Title: \n";
cin >> booktitle;
cout << "Author: \n";
cin >> author;
cout << "Date Publish: \n";
cin >> datepublished;
Books(booknumber,booktitle, author, datepublished);
cout << "====================================\n";
cout << "[1] -------- Try Again? ------------\n";
cout << "[2] -------- Return to Menu --------\n";
cout << "[3] -------- Exit Program ----------\n";
cout << "====================================\n";
cout << "Input your choice (Number Only): ";
cin >> choice;
switch (choice)
{
case 1:
inputbook ();
break;
case 2:
menu ();
break;
case 3:
exit();
default:
cout << "Wrong Option";
}
}
uj5u.com熱心網友回復:
避免重復代碼是個好主意。在這里,您有一個本質上是輸入回圈的默認情況,而您可以在一開始就完成該輸入回圈。所以你寫它的方式,你仍然需要一個圍繞整個事情的回圈,加上更多的邏輯,這使得代碼更難閱讀,更容易出錯。
為什么不簡單:
cout << "====================================\n";
cout << "[1] -------- Input Book ------------\n";
cout << "[2] -------- Search Book -----------\n";
cout << "[3] -------- Borrow Book -----------\n";
cout << "[4] -------- Exit Program ----------\n";
cout << "====================================\n";
int choice;
bool validInput = false;
while (!validInput)
{
cout << "Input your choice (Number Only): ";
if (!(cin >> choice)) {
std::cerr << "Aborted\n";
return;
}
validInput = (choice >= 1 && choice <= 4);
if (!validInput) {
std::cout << "Invalid input\n";
}
}
switch(choice)
{
// ...
}
如果您愿意,現在由您來決定您的輸入例程是否更健壯。請注意,如果輸入失敗,我已經退出了該功能。這可能來自流錯誤,但也可能是用戶輸入了非整數值。
相反,您可能希望使用將輸入作為字串讀取std::getline,然后將其轉換為整數,std::stoi或決議來自 a 的值std::istringstream。
uj5u.com熱心網友回復:
反而
繼續;
試著打電話
輸入簿();
所以它不會回去。這是您面臨的一個問題,因為您再次呼叫了“繼續”的 switch-case。這就是為什么當用戶輸入您剛剛設定的可接受的 int 范圍時它會回傳選單的原因。
uj5u.com熱心網友回復:
只需修改如下代碼,并在進入開關之前處理有效輸入驗證,這樣您就可以輕松緩解您遇到的問題!
cin >> choice;
while (choice < 1 || choice > 4)
{
cout << "Wrong Option\n";
cout << "Input your choice (Number Only): ";
cin >> choice;
if (choice < 1 || choice > 4)
{
continue;
}
}
switch (choice)
{
case 1:
inputbook ();
break;
case 2:
searchbook ();
break;
case 3:
borrowbook ();
break;
default:
exit();
break;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/486722.html
下一篇:python字串和內部條件
