首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

!不知道错在哪

2012-03-29 
求助!不知道错在哪##includeiostreamusing namespace stdconst int strsize10void showmenu()struct

求助!不知道错在哪


##include<iostream>
using namespace std;
const int strsize=10;
void showmenu();
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
int main()
{
showmenu();
bop num1[5]=
{
{"adj jda","dahj dad","sdssds ds",0},
{"yew", "we", "ewe",1},
  {"adkj","sdk","sd",2},
{"sfk","jda","jjjf",1},
{"ash","jaj","sjda",2}
};
char choice;
cin>>choice;
while(choice!='q')
//while() 这个不行啊
{
switch(choice)
{
case 'a':
for(int i=0;i<5;i++)
cout<<num1[i].fullname<<endl;
break;
case 'b':
  for(int j=0;j<5;j++)
cout<<num1[j].title<<endl;
break;
case 'c':
for(int k=0;k<5;k++)
cout<<num1[k].bopname<<endl;
break;
case 'd':
for(int l=0;l<5;l++)
{
if(num1[l].preference==0)choice='a';
else if(num1[l].preference==1)choice='b';
else if(num1[l].preference==2)choice='c';
}
};
cin>>choice;
};
cout<<"BYB!";
return 0;
}

void showmenu()
{
cout<<"please enter one of the following choices:\n"
  "a)display by name b)display by title\n"
"c)display by bopname d)display by prefernce\n"
"q)quit\n";
}
/*void showelem1(bop& elem[j],)
{
for(int i=0;i<5;i++)
cout<<elem.fullname<<endl;*/


D:\新建文件夹\c64.cpp(92) : error C2360: initialization of 'i' is skipped by 'case' label
  D:\新建文件夹\c64.cpp(81) : see declaration of 'i'
 

[解决办法]
将这switch里面的变量i,j,k定义在外面。
[解决办法]
case中是不能够定义变量的!
由于case的执行条件的情况不一样,如果不加大括号限制变量的作用域,很可能定义变量的case没有被执行,而导致引用变量的case却使用了没有定义的变量。因此编译器不允许这样定义变量。
[解决办法]
非要在case中定义的话,也可以将case的执行语句用{}括起来限制下作用域就行了!!
[解决办法]

探讨

case中是不能够定义变量的!
由于case的执行条件的情况不一样,如果不加大括号限制变量的作用域,很可能定义变量的case没有被执行,而导致引用变量的case却使用了没有定义的变量。因此编译器不允许这样定义变量。

热点排行