在 C++ 菜单中使用 While 循环切换大小写
Switch Case with While Loop in C++ Menu
我一直在用 C++ 构建一个菜单驱动的控制台,我目前使用 switch-case 作为我的选项,但现在我陷入了 switch case。
场景如下:
SCENARIO
说明:
在主菜单中输入无效选项后,会出现错误,提示用户重新输入他们想要的选项,现在我的问题是用户第二次尝试输入正确的选项, 它循环回到主菜单而不是将其重定向到下一个菜单。
我的目标:不重新显示主菜单,直接从默认进入二级菜单。
我的部分代码:
#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";
}
}
最好避免重复代码。在这里,您有一个默认情况,它本质上是一个输入循环,而您可以在开始时完成该输入循环。所以按照你写的方式,你仍然需要一个围绕整个事情的循环,加上更多的逻辑,这使得代码更难阅读和更多 bug-prone.
为什么不简单:
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)
{
// ...
}
现在,如果您愿意,可以让您的输入例程更加健壮。请注意,如果输入失败,我已经退出了该函数。这可能来自流错误,但也可能是用户输入了 non-integer 值。
您可能希望使用 std::getline
将输入读取为字符串,然后使用 std::stoi
将其转换为整数或解析 std::istringstream
.[=14 中的值=]
改为
continue;
试着打电话
inputbook();
所以它不会回去。
这是您面临的问题,因为您再次调用了 switch-case,即“继续”。这就是为什么当用户输入您刚刚设置的可接受的 int 范围时它返回菜单的原因。
只需修改如下代码,在进入开关前处理有效输入验证,这样就可以简单的缓解你遇到的问题!
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;
}
我一直在用 C++ 构建一个菜单驱动的控制台,我目前使用 switch-case 作为我的选项,但现在我陷入了 switch case。
场景如下:
SCENARIO
说明: 在主菜单中输入无效选项后,会出现错误,提示用户重新输入他们想要的选项,现在我的问题是用户第二次尝试输入正确的选项, 它循环回到主菜单而不是将其重定向到下一个菜单。
我的目标:不重新显示主菜单,直接从默认进入二级菜单。
我的部分代码:
#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";
}
}
最好避免重复代码。在这里,您有一个默认情况,它本质上是一个输入循环,而您可以在开始时完成该输入循环。所以按照你写的方式,你仍然需要一个围绕整个事情的循环,加上更多的逻辑,这使得代码更难阅读和更多 bug-prone.
为什么不简单:
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)
{
// ...
}
现在,如果您愿意,可以让您的输入例程更加健壮。请注意,如果输入失败,我已经退出了该函数。这可能来自流错误,但也可能是用户输入了 non-integer 值。
您可能希望使用 std::getline
将输入读取为字符串,然后使用 std::stoi
将其转换为整数或解析 std::istringstream
.[=14 中的值=]
改为
continue;
试着打电话
inputbook();
所以它不会回去。 这是您面临的问题,因为您再次调用了 switch-case,即“继续”。这就是为什么当用户输入您刚刚设置的可接受的 int 范围时它返回菜单的原因。
只需修改如下代码,在进入开关前处理有效输入验证,这样就可以简单的缓解你遇到的问题!
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;
}