【发布时间】:2021-05-15 22:06:03
【问题描述】:
我正在制作一个程序,其中包括制作一个菜单来注册和删除商店中的产品,我只是在设计带有开关的菜单,到那里一切正常,问题是当我输入其他内容时一个数字作为数据(字母或符号)控制台变得疯狂;所有的文字开始闪烁,它不会让我做任何事情(好像它在一个循环中),我必须关闭它。
有什么办法可以避免这种情况吗?所以当我输入一个字母或符号时,它会自动检测为无效并向我显示消息,而不会让控制台发疯?
顺便说一下,我用的是 Visual Studio。
提前致谢:)
#include<iostream>
#include<locale.h>
using namespace std;
int main()
{
int opc;
cout << " WELCOME TO THE STORE \"Happy Shopping\" ";
cout << endl;
cout << "\n1.- Create Order.";
cout << "\n2.- Delate Order.";
cout << "\n3.- list of orders created.";
cout << "\n4.- Exit";
cout << endl;
cout << "\n¿what do you want to do?: "; cin >> opc;
switch (opc)
{
case 1:cout << "\nCreate Order"; break;
case 2:cout << "\nDelate Order"; break;
case 3: cout << "\nlist of orders created"; break;
case 4:exit(EXIT_SUCCESS);
default:
if ((opc != 1) && (opc != 2) && (opc != 3) && (opc != 4))
{
system("cls");
cout << "the option entered is not valid, try again";
return main();
}
}
}
【问题讨论】:
-
顺便说一句,
return main();不是一个好主意。我认为您正在尝试实现递归,但最好不要尝试使用main()。请改用循环。
标签: c++ visual-studio visual-studio-2010 switch-statement