Why doesn't This Code Work?

Write your question here.
Hey everyone. My code doesn't output the id in least to greatest order. 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// 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];
	student temp;
	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 start=0; start<5; start++)
		{
			for (int j=0; j<5-start-1; j++)
			{
			if ( students[start].id > students[start + 1].id)
			{
				temp = students[j];
				students[j] = students[j + 1];
				students[j + 1] = temp;
			}
			}
	}
	for(int x=0; x<5; x++)
	{
		students[x].print();
	}
	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;
		
			/*if (students[0].id < students[1].id && students[0].id < students [2].id && students[0].id < students [3].id && students[0].id < students [4].id) 
			{
				cout<< students[0].id;
			}
			if (students[1].id < students[0].id && students[1].id < students [2].id && students[0].id < students [3].id && students[1].id < students [4].id) 
			{
				cout<< students[1].id;
			}
			if (students[2].id < students[0].id && students[2].id < students [1].id && students[2].id < students [3].id && students[2].id < students [4].id) 
			{
				cout<< students[2].id;
			}
			if (students[3].id < students[0].id && students[3].id < students [1].id && students[3].id < students [2].id && students[3].id < students [4].id) 
			{
				cout<< students[3].id;
			}
			if (students[4].id < students[0].id && students[4].id < students [1].id && students[4].id < students [3].id) 
			{
				cout<< students[4].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

Use a Bubble Sort in ascending order to sort your students.
Updated Code But it doesn't work.
Anyone?

Your bubble sort is wrong, thats why it doesnt work.
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

#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 main()
{
	student students[5];
	student temp; // for sort
	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;
	}

	// sort
	for (int i = 0; i < 5; i++)
		for (int j = 0; j < 5 - i - 1; j++)
			if (students[j].id > students[j + 1].id)
			{
				temp = students[j];
				students[j] = students[j + 1];
				students[j + 1] = temp;
			}

	for (int x = 0; x<5; x++)
	{
		students[x].print();
	}
	
	return 0;
}
Still doesn't work
(Code Updated)
Your bubble sort is Wrong.
Also, you should make use of your get functions or remove them. "Even though i did not use them".

This is the bubble sort that should fix your problem

1
2
3
4
5
6
7
8
9
10
11
12
13
/*Bubble sort*/
	for(int start=0; start<5; start++)
	{
		for(int j = start+1; j < 5; j++)
		{
                     if(students[start].id > students[j].id) 
                     {
                          temp = students[start];
                          students[start] = students[j];
                         students[j] = temp;
                     }
	       }
	}
Thank you.
Your welcome
Topic archived. No new replies allowed.