Я пишу программу, в которой хранится информация о студентах (ID, имя, майор, cpa и оценка). Когда я его компилирую, ошибок нет, и он работает гладко, пока не выведет результат после успешного ввода информации. Когда я отлаживаю, я уверен, что информация была успешно вставлена, и ошибка в функции display
. Мне очень нужна ваша помощь, чтобы выяснить, в чем ошибка.
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;
class Student
{
public:
string student_id;
string name;
string major;
float gpa;
char assessment;
Student()
{
student_id = "";
name = "";
major = "";
gpa = 0.0;
assessment = 'F';
}
void addinfo(string id, string student_name, string student_major, float student_gpa)
{
this->student_id = id;
this->name = student_name;
this->major = student_major;
this->gpa = student_gpa;
}
char assessstudent()
{
if (this->gpa >= 8.0)
return 'A';
if (this->gpa < 8.0 && this->gpa >= 6.0)
return 'B';
if (this->gpa < 6.0 && this->gpa >= 4.0)
return 'C';
if (this->gpa < 4.0 && this->gpa >= 2.0)
return 'D';
if (this->gpa < 2.0)
return 'F';
}
};
void swap(float* a, float* b)
{
float temp;
temp = *a;
*a = *b;
*b = temp;
}
void sortwithgrade(Student* array, int r, int l)
{
float temp = array[r].gpa;
int m = (r + l) / 2;
int i = l+1;
int j = r ;
if (r > l)
{
while (1)
{
while (array[++i].gpa <= temp)
;
while (array[--j].gpa >= temp)
;
if (i >= j)
break;
swap(array[i], array[j]);
}
swap(array[i], array[r]);
sortwithgrade(array, l, m - 1);
sortwithgrade(array, m + 1, r);
}
}
void display(Student* array, int max)
{
int i = 0;
printf("%-20s%-20s%-20s%-20s%-20s\n", "Student ID", "Student name", "Student Major", "Student gpa", "Student Assess");
while (i != max)
{
printf("%-20s%-20s%-20s%-20.2f%c\n", array[i].student_id, array[i].name, array[i].major, array[i].gpa, array[i].assessment);
i++;
}
}
int main()
{
Student list[10];
cout << "How many students do you want to add?\n";
int max;
cin >> max;
getchar();
int i = 0;
for (i = 0; i < max; i++)
{
cout << "Student number " << i << endl;
cout << "ID: ";
string stdid;
getline(cin, stdid);
cout << "Name: ";
string stdname;
getline(cin, stdname);
cout << "Major: ";
string stdmajor;
getline(cin, stdmajor);
cout << "GPA: ";
float gpa;
cin >> gpa;
getchar();
list[i].addinfo(stdid, stdname, stdmajor, gpa);
char stdassess = list[i].assessstudent();
list[i].assessment = stdassess;
}
sortwithgrade(list, 0, max - 1);
display(list, max);
return 0;
}