error C2360: initialization of 'j' is skipped by 'case' label

本文探讨了在switch-case结构中正确定义变量的方法。为了避免错误,变量应当定义在case外部或者在case内部使用大括号包裹。这有助于提高代码的可读性和维护性。

switch case 分支结构里面,你不能直接在case分支里面定义变量,要么定义到case外面,要么在case分支下面加一个大括号
这样就可以避免上述错误

#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' labelError C2360 initialization of 'target' is skipped by 'case' labelError C2361 initialization of 'found' is skipped by 'default' labelError C2361 initialization of 'target' is skipped by 'default' label 这些报错信息
04-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值