炸金花棋牌注册送3金币:C++case怎么使用?

来源:百度文库 编辑:中科新闻网 时间:2024/05/05 20:37:00
E:\MATE\Plan_define_achieve.cpp(87) : error C2360: initialization of 'infile' is skipped by 'case' label
E:\MATE\Plan_define_achieve.cpp(70) : see declaration of 'infile'

//==哪位能根据经验谈谈,case中使用须注意的问题,至少要提到在case序列语句中定义变量会不会出错...==>(skipped by 'case')

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
部分代码:
switch(sel)
     {
     case 'A'://打开本周计划文件
          ifstream infile;
          char *file;
          file=connect(path,"database.dat");//获取文件名
          infile.open(file);//打开文件
          infile.seekg(0,ios::end);//将文件指针指向首部
          char str[10];
          infile.read(str,10);//获取要打开的文件名
          filename=new char [strlen(str)+1];//分配内存
          strcpy(filename,str);
          infile.close();
          openfile(); // 打开文件,文件名为filename,为类的private指针变量
          break;
     case 'B':
 
      需要用到{}吗?case语句序列不是没有这个语法吗?
      我希望用case判断,条件成立,再执行相应语句.还有,我在不同case里定义了不同需求的变量,不能在switch上定义所有.请问有没有好的解决方法?

你这里case 里面语句很多,你应该用{} 括起来.
case语句中是可以定义变量的.

多个CASE连在一起,头几个是不能定义变量的,可以把变量定义在switch上面