First off, I'll admit openly that this is a homework assignment. That being said, my code is VERY close, and hoping for a nudge in the right direction.
The prompt: Write a complete program to read student data from the standard input, sort it by last name / first name , and print out the result to standard output . The student data consists of a last name , first name , and a grade point average (a floating point value ). You should assume no more than 50 students.
SAMPLE INPUT:
Ware Henry 87.2 Dantes Edmond 91.4 Earhart Amelia 92.6
My code accepts user input, swaps them based on last name, then first name, and outputs the list of students and GPA. The online program that tests my code inputs six (6) students data. It sorts them correctly using Swap, but the 5th student is repeated from entries 5-49 (out of 50), before finally outputting student 6. I've searched the forum, but not quite found a previous posting that applies.
I've tried using a 'while' statement in the output loop, but my grasp on booleans is a bit weak still. The attempted while statement is listed at the top of my code. Any assistance would be greatly appreciated.
while((list[i].lastName != list[i + 1].lastName) && (list[i].firstName != list[i + 1].firstName))
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class student
{
public:
string firstName;
string lastName;
float gpa;
student() {};
student(string last, string first, double grade)
{
firstName = first;
lastName = last;
gpa = grade;
};
};
int main()
{
string first;
string last;
float grade;
student list[50];
for(int i = 0; i < 50; i++)
{
cin >> last;
cin >> first;
cin >> grade;
list[i].lastName = last;
list[i].firstName = first;
list[i].gpa = grade;
}
for ( int i = 0; i < 50 - 1; i++)
{
if (list[i].lastName > list[i + 1].lastName)
{
swap (list[i], list[i+1]);
}
}
for ( int i = 0; i < 50 - 1; i++)
{
if (list[i].firstName > list[i + 1].firstName)
{
swap (list[i], list[i+1]);
}
}
for(int i = 0; i < 50 - 1; i++)
{
cout << list[i].lastName << " " << list[i].firstName << " " << list[i].gpa << endl;
}
return 0;
}