c++ - 何时何地使用 switch 语句而不是嵌套的 if-else 语句?
问题描述
我有一个关于结构的问题C++
,我写了下面的代码,我想给每个学生分配不同的课程,我一直在尝试做不同的作业,但找不到正确的方法。
这是我的代码:
#include <iostream>
using namespace std;
struct Course {
string CourseCode;
string CourseName;
string LecturerName;
int CourseNumber;
};
struct Student {
string StudentID;
string StudentName;
string Nickname;
Course info;
};
void menu(void) {
cout << "=================================" << endl;
cout << " MENU " << endl;
cout << "=================================" << endl;
cout << " 1- Add new course" << endl;
cout << " 2- Display course offered" << endl;
cout << " 3- Add new students" << endl;
cout << " 4- Display student list" << endl;
cout << " 5- Modify student name" << endl;
cout << " 6- Display student's name after modification" << endl;
cout << " 7- Exit Program" << endl;
cout << "=================================" << endl << endl;
}
int main()
{
Student* ptr[5];
Student details[5];
for (int i = 0; i < 2; i++) {
ptr[i] = &details[i];
}
int m, option, stuNo, courNo, n;
menu();
cout << "Choose an option from the menu: ";
cin >> option;
while (option != 7)
{
if (option == 1) {
cout << "\nHow many courses do you want to enter? [Maximum 2]: ";
cin >> courNo;
for (m = 0; m < courNo; m++) {
cout << "\nEnter course " << m + 1 << " details: " << endl;
cout << "Course Code: ";
cin >> ptr[m]->info.CourseCode;
cout << "Course Name: ";
cin >> ptr[m]->info.CourseName;
cout << "Lecturer Name: ";
cin >> ptr[m]->info.LecturerName;
}
}
else if (option == 2) {
cout << "\nThe courses details you entered are:" << endl;
for (m = 0; m < courNo; m++) {
cout << "\nCourse " << m + 1 << ": " << endl;
cout << "Course Code: " << ptr[m]->info.CourseCode << endl;
cout << "Course Name: " << ptr[m]->info.CourseName << endl;
cout << "Lecturer Name: " << ptr[m]->info.LecturerName << endl;
}
}
else if (option == 3) {
cout << "\nHow many students do you want to enter? [Maximum 5]: ";
cin >> stuNo;
for (m = 0, n=0; m < stuNo; m++, n++) {
cout << "\nEnter student " << m + 1 << " details: " << endl;
cout << "ID: ";
cin >> ptr[m]->StudentID;
cout << "Name: ";
cin >> ptr[m]->StudentName;
cout << "Nickname: ";
cin >> ptr[m]->Nickname;
cout << "Course number from the list: ";
cin >> n; // This gives same output for both students
}
}
else if (option == 4) {
cout << "\nThe students details you entered are:" << endl;
for (m = 0; m < stuNo; m++) {
cout << "\nStudent " << m + 1 << ": " << endl;
cout << "ID: " << ptr[m]->StudentID << endl;
cout << "Name: " << ptr[m]->StudentName << endl;
cout << "Nickname: " << ptr[m]->Nickname << endl;
cout << "Course " << ptr[n-1]->info.CourseName<< endl;
}
}
else if (option == 5) {
cout << "\nEnter the student number that has been saved in option 1,2: ";
cin >> n;
cout << "\nEnter the new name: ";
cin >> ptr[n-1]->StudentName;
}
else if (option == 6) {
cout << "\nStudent "<< n <<" new name: ";
cout << ptr[n - 1]->StudentName;
cout << endl;
}
else
cout << "Invalid number! Please re-enter a choice from the menu." << endl;
cout << "=====================================================" << endl;
cout << "\nChoose an option from the menu: ";
cin >> option;
}
if (option == 7)
cout << "End of program!" << endl;
}
我知道根据我的代码分配给学生的课程的输出将是相同的,因为我只有n
,如何更改它以使其对每个学生都不同
这是输出:
=================================
MENU
=================================
1- Add new course
2- Display course offered
3- Add new students
4- Display student list
5- Modify student name
6- Display student's name after modification
7- Exit Program
=================================
Choose an option from the menu: 1
How many courses do you want to enter? [Maximum 2]: 2
Enter course 1 details:
Course Code: 111
Course Name: co1
Lecturer Name: lect1
Enter course 2 details:
Course Code: 222
Course Name: co2
Lecturer Name: lect2
=====================================================
Choose an option from the menu: 3
How many students do you want to enter? [Maximum 5]: 2
Enter student 1 details:
ID: 1111
Name: student1
Nickname: stud1
Course number from the list: 2
Enter student 2 details:
ID: 2222
Name: student2
Nickname: stud2
Course number from the list: 1
=====================================================
Choose an option from the menu: 4
The students details you entered are:
Student 1:
ID: 1111
Name: student1
Nickname: stud1
Course co2
Student 2:
ID: 2222
Name: student2
Nickname: stud2
Course co2
=====================================================
Choose an option from the menu: 7
End of program!
解决方案
问题是您将可用Course
s 和Student
s 的列表存储在同一个列表中。您应该有一个单独的Course
s 和Student
s 列表,然后根据需要让这两个列表相互交叉引用。
例如,改为将 的成员Course info;
更改为,然后您可以在需要时在s 列表中查找。Student
std::string CourseCode;
CourseCode
Course
尝试更多类似的东西:
#include <iostream>
#include <string>
#include <limits>
using namespace std;
struct Course {
string CourseCode;
string CourseName;
string LecturerName;
int CourseNumber; // <-- what is this for???
};
struct Student {
string StudentID;
string StudentName;
string Nickname;
string CourseCode;
};
void menu() {
cout << "=================================" << endl;
cout << " MENU " << endl;
cout << "=================================" << endl;
cout << " 1- Add new course" << endl;
cout << " 2- Display course offered" << endl;
cout << " 3- Add new students" << endl;
cout << " 4- Display student list" << endl;
cout << " 5- Modify student name" << endl;
cout << " 6- Display student's name after modification" << endl;
cout << " 7- Exit Program" << endl;
cout << "=================================" << endl << endl;
}
string getCourseName(const string &CourseCode, const Course *courses, int courNo) {
for(int m = 0; m < courNo; ++m) {
if (courses[m].CourseCode == CourseCode)
return courses[m].CourseName;
}
return "";
}
int main()
{
Student students[5];
Course courses[2];
int n, m, option, stuNo = 0, courNo = 0, modNo = 0;
menu();
do
{
cout << "Choose an option from the menu: ";
cin >> option;
switch (option) {
case 1:
if (courNo >= 2) {
cout << "\nCourse list is full";
break;
}
cout << "\nHow many courses do you want to enter? [Maximum " << 2 - courNo << "]: ";
cin >> n;
for (m = 0; m < n; ++m) {
cout << "\nEnter course " << m + 1 << " details: " << endl;
cout << "Course Code: ";
cin >> courses[courNo].CourseCode;
cout << "Course Name: ";
cin >> courses[courNo].CourseName;
cout << "Lecturer Name: ";
cin >> courses[courNo].LecturerName;
++courNo;
}
break;
case 2:
cout << "\nThe courses details you entered are:" << endl;
for (m = 0; m < courNo; ++m) {
cout << "\nCourse " << m + 1 << ": " << endl;
cout << "Course Code: " << courses[m].CourseCode << endl;
cout << "Course Name: " << courses[m].CourseName << endl;
cout << "Lecturer Name: " << courses[m].LecturerName << endl;
}
break;
case 3:
if (stuNo >= 5) {
cout << "\nStudent list is full";
break;
}
cout << "\nHow many students do you want to enter? [Maximum " << 5 - stuNo << "]: ";
cin >> n;
for (m = 0; m < n; ++m) {
cout << "\nEnter student " << m + 1 << " details: " << endl;
cout << "ID: ";
cin >> students[stuNo].StudentID;
cout << "Name: ";
cin >> students[stuNo].StudentName;
cout << "Nickname: ";
cin >> students[stuNo].Nickname;
cout << "Course number from the list: ";
cin >> students[stuNo].CourseCode;
++stuNo;
}
break;
case 4:
cout << "\nThe students details you entered are:" << endl;
for (m = 0; m < stuNo; ++m) {
cout << "\nStudent " << m + 1 << ": " << endl;
cout << "ID: " << students[m].StudentID << endl;
cout << "Name: " << students[m].StudentName << endl;
cout << "Nickname: " << students[m].Nickname << endl;
cout << "Course: " << students[m].CourseCode << " " << getCourseName(students[m].CourseCode, courses, courNo) << endl;
}
break;
case 5:
cout << "\nEnter the student number that has been saved in option 1,2: ";
cin >> n;
if (n < 1 || n > stuNo) {
cout << "\nInvalid student number";
break;
}
cout << "\nEnter the new name: ";
cin >> students[n-1].StudentName;
modNo = n;
break;
case 6:
if (modNo < 1 || modNo > stuNo) {
cout << "\nNo student modified";
break;
}
cout << "\nStudent " << modNo << " new name: ";
cout << students[modNo-1].StudentName << endl;
modNo = 0;
break;
case 7:
break;
default:
cout << "Invalid number! Please re-enter a choice from the menu." << endl;
break;
}
cout << "=====================================================" << endl;
}
while (option != 7);
cout << "End of program!" << endl;
}
推荐阅读
- c# - 在 mvc 控制器上向开发人员隐藏模型变量/属性的值
- html - 初学者。为我自己的试验和错误制作了一个简单的网站。使用主机。无法获取用户注册页面输入以保存到某种类型的文件
- r - 如何在 readr/tidyverse 中设置 encoding/fileEncoding 选项?
- json - 发出从 Swift 发送 POST 请求的问题
- python - 在 Python 中通过一次调用在 PL/SQL 中插入多个值
- python - Pandas - 如何计算数据框中的连续出现次数?
- java - 如何建立处理器模块之间的通信?
- c# - 从 SQL Server 代理作业将文件上传到 Azure Datalake 不起作用
- c# - 鼠标移动时更新表单文本
- hbase - Apache Atlas 不会从 Zookeeper 的单独集合开始