initialization of 'a' is skipped by 'case' label

本文探讨了在C++中如何正确使用switch-case结构来定义和初始化变量,并提供了两种有效的解决方案,一种是将变量声明放在外部,另一种是在case块内部使用局部作用域。

一个小问题,以前没发现
现在发出来麻烦知道情况的朋友们解释一下具体情况

#include <iostream.h>
#include <fstream.h>
void main()
{
    int n;
    cin >> n;
    switch(n)
{
case 1:
        int a=1;
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
default :
exit(0);
break;
}
}

我理解的是case里不能定义并且初始化一个变量,vc的提示是
initialization of 'a' is skipped by 'case' label
当你定义的a生命期只能在case 1里,所以case 2和以后的 都就不能识别了

不知道还有没有更好的解释,谢谢

 

A:

方法1:定义在外面
int  a;   
switch(n)
{
case 1:
    a=1
    break;
case 2:
}
方法2:定义局部变量
switch(n)
{
case 1:
    {
        int a=1;
    }
    break;
case 2:
}

转自:http://topic.youkuaiyun.com/u/20071118/20/6E238B82-3C3A-496B-9DF5-9B66C1B11218.html

#include "stdafx.h" #include <iostream> #include <string> #include <vector> using namespace std; class Student { private: string name; float chinese; float math; float english; float average; char grade; public: Student(string n, float c, float m, float e) :name(n), chinese(c), math(m), english(e) { average = (c + m + e) / 3; grade = getGrade(); } char getGrade() const { if (average >= 90) { return 'A'; } else if (average >= 80) { return 'B'; } else if (average >= 70) { return 'C'; } else if (average >= 60) { return 'D'; } else { return 'E'; } } string getName() const { return name; } float getChinese() const { return chinese; } float getMath() const { return math; } float getEnglish() const { return english; } float getAverage() const { return average; } }; int main() { vector<Student> s; int numstudent; cout << "请输入学生人数:"; cin >> numstudent; string name; float chinese; float math; float english; for (int i = 0; i < numstudent; i++) { cout << "请输入第" << i + 1 << "个学生的姓名:"; cin >> name; cout << "请输入第" << i + 1 << "个学生的语文成绩:"; cin >> chinese; while (chinese < 0 || chinese > 100) { cout << "成绩需要在0-100之间,请重新输入语文成绩:"; cin >> chinese; } cout << "请输入第" << i + 1 << "个学生的数学成绩:"; cin >> math; while (chinese < 0 || chinese > 100) { cout << "成绩需要在0-100之间,请重新输入数学成绩:"; cin >> math; } cout << "请输入第" << i + 1 << "个学生的英语成绩:"; cin >> english; while (chinese < 0 || chinese > 100) { cout << "成绩需要在0-100之间,请重新输入英语成绩:"; cin >> english; } s.emplace_back(name, chinese, math, english); } int choice; do { cout << "\n======学生管理系统======" << endl; cout << "1.显示学生的所有信息" << endl; cout << "2.查询学生的信息" << endl; cout << "3.退出系统" << endl; cout << "请输入选项,选择你要进行的操作:"; cin >> choice; switch (choice) { case 1: cout << "\n====学生列表====" << endl; for (const auto& stu : s) { cout << "姓名:" << stu.getName() << " | 平均分:" << stu.getAverage() << " | 等级:" << stu.getGrade() << endl; } break; case 2: string target; cout << "请输入要查询的学生的姓名:"; cin >> target; bool found = false; for (const auto& stu : s) { if (stu.getName() == target) { found = true; cout << "\n====学生详情====" << endl; cout << "姓名:" << stu.getName() << endl; cout << "语文成绩:" << stu.getChinese() << endl; cout << "数学成绩:" << stu.getMath() << endl; cout << "英语成绩:" << stu.getEnglish() << endl; cout << "平均分:" << stu.getAverage() << endl; cout << "等级:" << stu.getGrade() << endl; } } if (!found) { cout << "未找到该学生!" << endl; } case 3: cout << "系统已退出" << endl; break; default: cout << "无效输入,请重新选择!" << endl; } } while (choice!=3); return 0; } 检测代码是否存在问题,并且分析Error (active) transfer of control bypasses initialization of: C2360 initialization of 'found' is skipped by 'case' label; Error C2360 initialization of 'target' is skipped by 'case' label; Error C2361 initialization of 'found' is skipped by 'default' label; Error C2361 initialization of 'target' is skipped by 'default' label 这些报错信息
04-04
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值