Hello I'm having trouble with my program. I have a couple of problems. First of all, the program won't output the name, id, and gpa. It comes out as a blank. Second of all, when I enter the amount of students that I want in the array based list, it keeps on going after it reaches that number. I'm very lost. Thank you in advance for your help.
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
struct Student
{
string name;
int id;
float gpa;
};
void printStudents(Student* student, int nStudents)
{
int i;
for(i=0;i < nStudents; i++)
{
cout << "Name = " << left << setw(30) << student[i].name;
cout.fill('0');
cout << " ID = " << right << setw(7)
<< student[i].id << ", gpa = "
<< student[i].gpa << endl;
cout.fill(' ');
}
}
int main()
{
const int MAX_STUDENTS = 100; //Capacity
int nStudents = 0;
Student student[MAX_STUDENTS];
while(true)
{
// Get how many students.
cout << "How many students are there? : ";
cin >> nStudents;
cin.ignore(1000,10);
if(nStudents == 0)
break;
// Get and save the records.
for( int i=0; i < nStudents; i++)
{
Student aStudent;
cout << "Name: " << endl;
getline(cin, aStudent.name);
cout << "ID: " << endl;
cin >> aStudent.id;
cin.ignore(1000,10);
cout << "GPA: " << endl;
cin >> aStudent.gpa;
cin.ignore(1000,10);
if(nStudents < MAX_STUDENTS)
student[nStudents++] = aStudent;
}
//Sort by gpa.
for(int i=0; i < nStudents; i++)
{
for(int j=1 + i; j < nStudents; j++)
{
if (student[i].gpa > student[j].gpa)
{
Student temp = student[i];
student[i] = student[j];
student[j] = temp;
}
}
}
printStudents(student, nStudents);
}
if(nStudents == 0)
cout << "No students were added." << endl;
}