注册 登录
编程论坛 C++教室

switch用法中遇到个小问题,求教

xlhcy2014 发布于 2014-03-19 16:58, 510 次点击
#include<iostream>
#include<string>
using namespace std;
int main()
{
char choice;
cin>>choice
switch(choice)

case '1':
    ......
    break;
case '2':
    string str;
    cin>>str;
    break;
case '3'        //这些提示错误:initialization of 'str' is skipped by 'case' label
    .....
    break;

return 0;
}
看到提示,我第一反应就是把case 2放在最后面,编译就通过了。
如果我不打算调准CASE的顺序,该怎么改;
5 回复
#2
天使梦魔2014-03-19 17:17
case 里面有string定义
case3是不是犯贱地调用了还没被case2定义出来的变量
#3
xlhcy20142014-03-19 17:48
在VB环境下
case 1
  string类型的数据,想塞几个就塞几个进去,也不影响后面case的判断
case 2

VB能搞定,C++ 不可能搞不定,求个法子。
#4
天使梦魔2014-03-19 18:02
我以为你写错而已,难道真错?
难道真的是那个case3后面少个:号?????
#5
hubinyes2014-03-19 23:12
case2:
{
   
}
括起来
#6
lonely_212014-03-20 10:06
case 3少冒号了吧
1