Student Class Compare Id

Write your question here.
Hey everyone,
I am writing a program that takes an input for students id (and several other things) and it has to compare all of them and display in least to greatest order. I don't how I can use my code to make it work. Please help.
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Student Class.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

class student
{
public:
	int age;
	int grade;
	int id;
	string name;
public:
	void setAge (int a)
	{
		age=a;
	}
	void setGrade (int g)
	{
		grade=g;
	}
	void setId (int i)
	{
		id = i;
	}
	void setName (string n)
	{
		name = n;
	}
	int getAge ()
	{
		return age;
	}
	int getGrade ()
	{
		return grade;
	}
	int getId()
	{
		return id;
	}
	string getName()
	{
		return name;
	}
	void print() {
		cout <<left << setw(15) << name << setw(8) << id << setw(8) << grade << age << endl;
	}
};
int _tmain(int argc, _TCHAR* argv[])
{
	student students[5];
	for(int x=0; x<5; x++)
	{
		cout<< "Enter Name"<<endl;
		cin>> students[x].name;
		cout<< "Enter Age"<<endl;
		cin>> students[x].age;
		cout<< "Enter Grade"<<endl;
		cin>> students[x].grade;
		cout<< "Enter Id"<<endl;
		cin>> students[x].id;
	}
	for(int x=0; x<5; x++)
	{
		students[x].print();
	}
	int min;
	int min2;
	int min3;
	int min4;
	int min5;
	for(int y=1; y<6; y++)
	{
	/*int number1 = students[y].id / 10000 ;
	int number2 = students[y].id %10000/1000;
	int number3 = students[y].id %1000 / 100;
	int number4 = students[y].id %100 / 10;
	int number5 = students[y].id %10;*/
	//cout<< number1 << number2 << number3 << number4 << number5<<endl;
		for(int z=0; z<5; z++)
		{
			if (students[z].id < students[z+1].id) 
			{
			}
			/*int number2_1 = students[z].id / 10000;
			int number2_2 = students[z].id % 10000 / 1000;
			int number2_3 = students[z].id %1000 / 100;
			int number2_4 = students[z].id %100 / 10;
			int number2_5 = students[z].id %10;*/
		} 
	}
	return 0;
}


Last edited on
In very easy to understand way,
You just loop and print the least one out, then mark it that you printed so not use to compare next time. Maybe another array or change it to -1;

i.e., use -1,
ID 5 3 4 7 6 -> loop find least and not -1 (3)
print 3, ID = 5 -1 4 7 6 -> loop find least and not -1 (4)
print 4, ID = 5 -1 -1 7 6 -> etc.

This is technique like Selection Sort.

And if you want to order them in array before print. Use keyword Sort Algorithm and learn, very smart to go.
Last edited on
Im sorry I don't understand what your tying to say.
Could you provide code example?
Can anyone else help?
you should use any sorting algorithm like bubble sort or selection sort to arrange the students in order of IDs as you want.

Add this code betwen line 67 and 68. i.e before the loop that prints.
I dont know what lines 72 - 96 are for. In my opinion, you should delete it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*bubble sort algorithm*/
for(int i = 0; i < 5; i++)
{
    for(int j = i + 1; j < 5; j++)
    {
        if(students[i].getId() > students[j].getId())
        {
            /* swap positions in array of students*/
            student temp = students[i];
            students[i] = students[j];
            students[j] = temp;
        }
    }
}
Topic archived. No new replies allowed.