c++:学生记录程序
C++: Student Record Program
我急需帮助。我应该使用结构体和向量创建一个学生记录。在程序中,我应该有:
-
对于给定的学生,通过姓名或学号标识(a)在记录中输入一个新学生。(假设没有完成任何课程。)(b)平均绩点(c)成绩单,即所修课程的清单,包括学分和成绩(d)记录新完成的课程
-
按姓名排序的所有修过某门课程的学生名单。
-
按平均成绩排序的所有学生及其平均成绩的列表,在试用期,即平均绩点<2.0 .
这是我到目前为止所拥有的…我似乎有麻烦的用户输入如何读取它到我的结构
using namespace std;
struct courses {
string courseName;
int courseNum;
double credit;
char grade;
};
struct students {
string name;
int id;
vector<courses> c;
};
int main() {
// variables
string name;
char selector;
students s;
courses d;
vector<students> student;
vector<courses> course;
// (1) create a menu: (a) user input, (b) echo record (with overall gpa), (c) failed students
do {
// prompt for user input
cout << "Enter Q to (Q)uit, (C)reate new student record, (S)how all record(s) on file, show students on (P)robation: ";
cin >> selector;
selector = toupper(selector);
switch (selector) {
// (a) ask and get for user input:
// student info first
// courses second
case 'C':
// variables within case C
char answer;
char answerAddAnotherCourseEntry;
char answerToAnotherStudentRecord;
do {
cout << "Enter your name and student ID number: ";
cin >> s.name >> s.id;
student.push_back(s);
do {
cout << "Do you want to create a student course entry ('y' or 'n')? ";
cin >> answer;
answer = toupper(answer);
cout << "Enter your course number, course name, grade received and credit worth: ";
cin >> d.courseNum >> d.courseName >> d.grade >> d.credit;
course.push_back(d);
cout << "Add another student course entry ('y' or 'n'): ";
cin >> answerAddAnotherCourseEntry;
answerAddAnotherCourseEntry = toupper(answerAddAnotherCourseEntry);
} while (answer == 'N');
cout << "Add another student record ('y' or 'n'): " << endl;
cin >> answerToAnotherStudentRecord;
answerAddAnotherCourseEntry = toupper(answerToAnotherStudentRecord);
} while (answerToAnotherStudentRecord == 'N');
break;
// (b) echo record of vectors
// sort by name
case 'S':
if (student.empty()) {
cout << "nSorry, no records exist in the database.";
}
else
for (int count = 0; count < student.size(); count++) { //For Loop to Display All Records
cout << "Student name: " << student[count].name << "ID Number: " << student[count].id << endl;
count++;
// another for loop i think
for (int i = 0; i < course.size(); i++) {
cout << "Course info: " << " " << course[i].courseNum << " " << course[i].courseName << " " << course[i].credit << " " << course[i].grade << endl;
i++;
}
}
cout << endl;
break;
// (c) separate failed student into another vector
// sort by gpa
case 'P':
break;
} // bracket closing switch
} // bracket closing do while
while (selector != 'q' && selector != 'Q'); // first do while
我看到的问题是,你试图将用户输入直接存储在字符串中,例如在结构体s
的变量name
中:
cout << "Enter your name and student ID number: ";
cin >> s.name >> s.id;
不能编译。相反,您可以将名称存储在临时字符数组中,然后将其复制到s.name
。
char studentName[128];
cout << "Enter your name and student ID number: ";
cin >> studentName >> s.id;
s.name = studentName;
如果你想将输入直接存储在字符串中,也可以使用getline()。
此外,您可以尝试直接输出变量student[count].name
(这是一个字符串)到cout:
cout << "Student name: " << student[count].name << "ID Number: " << student[count].id << endl;
首先将字符串转换为字符数组,使此编译:
cout << "Student name: " << student[count].name.c_str() << "ID Number: " << student[count].id << endl;
相关文章:
- 为什么Qt Creator的应用程序输出不能从spdlog记录器打印
- 逐行跟踪和记录 c++ 程序的执行
- 使用完美记录进行分析Linux应用程序
- 在不存在的记录上调用 remove 会使程序崩溃
- 在后台记录 Eclipse IDE 调用的应用程序列表
- CUDA 程序不测量执行时间:cuda事件记录
- 尝试将数据从记录读取到程序(C++)时难以捉摸的错误
- 多线程应用程序中的独立日志记录
- 运行此程序时的垃圾数据(学生记录程序)
- 如何使用doxygen记录应用程序源文件
- 尝试在hash_map中查找记录时应用程序崩溃
- 无法从我创建的 c 二进制程序中删除记录
- 程序不计算记录数
- c++ 从文件中读取双倍,但如果程序遇到字符串,则记录
- 流记录程序VLC
- 这是C ++程序领域中最流行的开源日志记录软件/程序
- 制作一个简单的c++键盘记录程序
- 如何制作键盘记录程序
- c++:学生记录程序
- c++简单的键盘记录程序