我对编程和开发基本的学生管理系统不熟悉。系统首先要求用户输入数据。 6件事; ID,姓名,大学,系,学期和GPA。数据输入工作正常。数据输入完成后,将出现一个带有三个选项的菜单。一种是创建新的学生资料。在这种情况下,当用户输入学生的姓名时,程序会突然终止。我在所有有意义的地方都使用了cin.ignore,但是会发生相同的问题。任何帮助,将不胜感激。谢谢。代码如下。注意:不允许使用结构和/或向量。
#include<iostream>
#include<string>
using namespace std;
int main()
{
cout<<"------------------------------------- Welcome to student management system! ----------------------------------------------"<<endl;
cout<<"------------------------------------------------DATA INPUT----------------------------------------------"<<endl;
cout<<"Enter the number of students you want to enroll: ";
int size;
int n=0;
int counter=0;
cin>>size;
int *student_ids=new int[size];
int *semester=new int[size];
float *cgpa=new float[size];
string *names=new string[size];
string *uni=new string[size];
string *dept=new string[size];
int *ptr_id,*ptr_semester;
string *ptr_names,*ptr_uni,*ptr_dept;
float *ptr_cgpa;
cout<<"Enter their ids: "<<endl;
ptr_id=student_ids;
for(int i=0;i<size;i++)
{
cin>>*ptr_id;
ptr_id++;
}
ptr_names=names;
cout<<"Enter their names: "<<endl;
cin.ignore();
for(int i=0;i<size;i++)
{
getline(cin,*ptr_names);
ptr_names++;
}
ptr_uni=uni;
cout<<"Enter their Universities: "<<endl;
for(int i=0;i<size;i++)
{
getline(cin,*ptr_uni);
ptr_uni++;
}
ptr_dept=dept;
cout<<"Enter their departments: "<<endl;
for(int i=0;i<size;i++)
{
getline(cin,*ptr_dept);
ptr_dept++;
}
ptr_semester=semester;
cout<<"Enter their semester: "<<endl;
for(int i=0;i<size;i++)
{
cin>>*ptr_semester;
ptr_semester++;
}
ptr_cgpa=cgpa;
cout<<"Enter the GPA: "<<endl;
for(int i=0;i<size;i++)
{
cin>>*ptr_cgpa;
ptr_cgpa++;
}
cout<<"-----------------------------------------------DATA INPUT COMPLETE----------------------------------------------------------- "<<endl;
int choice;
char again='y';
while(again=='y'||again=='Y')
{
cout<<"Press 1 to Create a Student Profile"<<endl;
cout<<"Press 2 to Search a Student"<<endl;
cout<<"Press 3 to Delete a Student record"<<endl;
cin>>choice;
switch(choice)
{
case 1: //CREATING STUDENT PROFILE
cout<<"Enter the number of profiles you want to create: ";
cin>>n;
cout<<"Enter ID(s): ";
for(int i=size;i<size+n;i++)
{
cin>>*ptr_id;
ptr_id++;
}
cout<<student_ids[1];
cout<<student_ids[0];
cout<<"Enter name(s): ";
cin.ignore();
for(int i=size;i<size+n;i++)
{
getline(cin,*ptr_names);
cin.ignore();
ptr_names++;
}
cout<<names[1]<<endl;
cout<<"Enter University: ";
for(int i=size;i<size+n;i++)
{
getline(cin,*ptr_uni);
ptr_uni++;
}
cout<<"Enter Department(s): ";
for(int i=size;i<size+n;i++)
{
getline(cin,*ptr_dept);
ptr_dept++;
}
cout<<dept[2]<<endl;
cout<<"Enter semester(s): ";
for(int i=size;i<size+n;i++)
{
cin>>*ptr_semester;
ptr_semester++;
}
cout<<"Enter CGPA: ";
for(int i=size;i<size+n;i++)
{
cin>>*ptr_cgpa;
ptr_cgpa++;
}
cout<<"------------------------------------------------SUCCESS-------------------------------------------------------------";
cout<<endl;
break;
case 2: //SEARCHING
int searching_method;
cout<<"Press 1 to search by ID."<<endl;
cout<<"Press 2 to list all students."<<endl;
cin>>searching_method;
switch(searching_method)
{
case 1: //SEARCHING BY ID
cout<<"Enter the student ID: ";
int search;
cin>>search;
for(int i=0;i<size+n;i++)
{
if(student_ids[i]==search)
{
counter++;
cout<<"Name: "<<names[i]<<endl;
cout<<"University: "<<uni[i]<<endl;
cout<<"Department: "<<dept[i]<<endl;
cout<<"Semester: "<<semester[i]<<endl;
cout<<"CGPA: "<<cgpa[i]<<endl;
break;
}
}
if(counter==0)
{
cout<<"No student found!"<<endl;
}
break;
case 2: //LIST OF ALL STUDENTS
for(int i=0;i<size+n;i++)
{
cout<<"Student ID: "<<student_ids[i]<<endl;
cout<<"Name: "<<names[i]<<endl;
cout<<"University: "<<uni[i]<<endl;
cout<<"Department: "<<dept[i]<<endl;
cout<<"Semester: "<<semester[i]<<endl;
cout<<"CGPA: "<<cgpa[i]<<endl;
cout<<endl;
cout<<endl;
}
break;
}
//case 3: //DELETEING A STUDENT ID
}
}
system("pause");
return 0;
}
因此,在程序开始时,您要求用户输入学生人数的大小。当您完成对“数据库”中学生的初始化时。现在,您尝试添加的下一个学生,在ptr_names数组中没有更多空间了,因为您已在初始化步骤中将其填满。
您可以做什么:
-如果您知道可以创建的最大学生人数,请将大小设置为该人数
-更好的解决方案是将std :: vector用于动态大小的容器。
为了使您的代码更好,您可以制作这样的学生结构或课程
struct student{
int id;
int semester;
float cgpa;
std::string name;
std::string uni;
std::string dept;
}
这样,您可以使用类似如下的向量来存储所有学生:
std::vector<student> all_students;