我的菜单系统有不同的菜单编号。
用户必须先选择选项1,然后才能使用选项2到5。
如何实现此错误消息?
这是我的简化代码:
do{
switch(menu){
case 1:
cout<<"Input "<<input<< "numbers: ";
for(int i=0;i<input;input++){
listInput(&array[i]) }}
cout<<"\n\nEnter r/R to return to the menu: ";
cin>>r;
system("cls");
break;
case 2:
cout<<"Calculate Sum"; //if menu!=case 1 first
//my operations //cout<<"Error message";
break; //something like this??
default:
cout<<"Invalid Input";
}}
while(r=='r'||r=='R');
答案 0 :(得分:4)
如果输入数字是强制性的,请不要让用户选择做错。一种选择是将案例1代码移动到getnumbers
函数中并执行类似的操作;
getnumbers(&array); // Force the user to input numbers before getting the
// option to select the wrong option
...menu input
do {
switch(menu){
case 1:
getnumbers(&array);
break;
case 2:
cout<<"Calculate Sum"; //if menu!=case 1 first
//my operations //cout<<"Error message";
break;
...
答案 1 :(得分:1)
创建一个全局变量(比如标志)并将其设置为0
在案例1中,将变量设置为1(flag = 1
)
在其余情况下,如果flag不是1,则只显示错误并中断。
do{
int flag = 0;
switch(menu){
case 1:
cout<<"Input "<<input<< "numbers: ";
for(int i=0;i<input;input++){
listInput(&array[i]) }}
cout<<"\n\nEnter r/R to return to the menu: ";
cin>>r;
system("cls");
flag = 1;
break;
case 2:
if (flag == 0)
{
cout <<"Choose 1 first";
break;
}
cout<<"Calculate Sum"; //if menu!=case 1 first
//my operations //cout<<"Error message";
break; //something like this??
default:
cout<<"Invalid Input";
}}
while(r=='r'||r=='R');