Implementing your own sort.

Hello comrades, I am having trouble trying to output only the names and gpa that are inputted. For example, If I were to input two student info, Johnny Chan 87.9 David Ruffman 95.6, the output will have Johnny Chan 87.9 followed by 49 copies of David Ruffman 95.6. It's frustrating because I have to assume no more than 50 students. I'd appreciate your help, i'm trying to learn c++.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#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;
}
Your sorting algorithm is flawed. Here's one that should be easy to implement: http://en.wikipedia.org/wiki/Selection_sort
The duplication of the last student throughout the remainder of the array sounds like it may be an error in the swap function. To avoid it, You might want to either write your own swap function, and/or keep an independent counter of how many students have actually been entered, and only process that many in the sorting and output.
Do you know if I could implement, for example, list.size() at the end in output, so that only the names inputted will be outputted if the input is less than 50 students? My grasp on writing functions or implementing bubble sort is weak still.
if you keep a counter of how many are entered, you can use that counter to limit the output.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	int numStudents = 0;

	for( int i = 0; i < 50; i++ )
	{
		cout << "Enter student info (last first grade) or 0 to exit";
		cin >> last;
		if( last == "0" )
			break;
		cin >> first;
		cin >> grade;
		list[i].lastName = last;
		list[i].firstName = first;
		list[i].gpa = grade;
		numStudents++;
	}
Last edited on
The code to implement the selection sort is right on the wiki page. all you really need to do is replace the array name and test condition in their example with yours.
Topic archived. No new replies allowed.