initialization of * is skipped by 'default' label

这样的编译报错已经遇到了很多次,以前总是绕过去,直到今天忍无可忍,准备找一下原因。

 

这种错误经常出现在以下的使用中:

 

 

绕道的方法当然是在func()开头就定义int i; 可这样不是好办法。

 

错误出现的原因是,在switch中的 int i;作用域是整个swtich语句的,当然会被default skip掉了。

 

而通常我们定义int i只是希望在本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' 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值