Sorting not working

Hello :)
I am making a simple program to accept the details of 3 students and to print the values as the user wants. So i have 3 main options and the 3rd option is sort. If you select sort the program asks you in which way do you want to sort it. And there are 4 options in sorting and i am trying to use selection sort here but none of the 4 sorts are working :( . Hope someone can help me with this and tell me what i have done wrong.If you didnt really understand my question please feel free to ask me.

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
 #include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<iomanip.h>
void enter();
void list();
void query();
void sort();
void percent();
void maths();
void science();
void cs();
struct student
 {
   char name[20];
   int rno;
   float m1,m2,m3,percent;
  }s[3];
char choice;

void main()
{
  clrscr();
  int ch;

    enter();
    cout<<"\n\n\n\t|||||*****------------------------------------------*****|||||";
    cout<<"\n\n\t\t\t\tMENU";
    cout<<"\n\t\t\t\tþþþþ";
    cout<<"\n\n\n\t\t| 1. View the whole list of students |";
    cout<<"\n\n\t\t| 2. View your result                |";
    cout<<"\n\n\t\t| 3. Sort                            |";
    cout<<"\n\n\t\t| 4. Exit                            |";
    cout<<"\n\n\n\t|||||*****------------------------------------------*****|||||";
    cout<<"\n\n\n\t  Enter your choice [1/2/3/4] : ";
    cin>>ch;
    if(ch==1)
    list();
    if(ch==2)
    query();
    if(ch==3)
    sort();
    if(ch==4)
    exit(0);
    getch();
}
void enter()
{
 for(int i=0;i<3;i++)
 {
  cout<<"\n\n\tEnter the name of student "<<i+1<<"         : ";
  cin>>s[i].name;
  cout<<"\n\tEnter the roll no of student "<<i+1<<"      : ";
  cin>>s[i].rno;
  cout<<"\n\tEnter the mark in Maths             : ";
  cin>>s[i].m1;
  cout<<"\n\tEnter the mark in Science           : ";
  cin>>s[i].m2;
  cout<<"\n\tEnter the mark in Computer Science  : ";
  cin>>s[i].m3;
  s[i].percent=(s[i].m1+s[i].m2+s[i].m3)/3;
 }
}


void list()
{
 cout<<"\n\n\n\n\n\tROLL NO"<<setw(12)<<"NAME"<<setw(13)<<"MATHS"<<setw(12)<<"SCIENCE"<<setw(8)<<"C.S";
 for(int i=0;i<3;i++)
  cout<<"\n\t  "<<s[i].rno<<setw(16)<<s[i].name<<setw(12)<<s[i].m1<<setw(10)<<s[i].m2<<setw(9)<<s[i].m3<<endl;
}
void query()
{
 int rnum;
 cout<<"\n\n\n\tEnter your roll number : ";
 cin>>rnum;
 for(int i=0;i<3;i++)
  if(rnum==s[i].rno)
  {
   cout<<"\n\n\t NAME                      : "<<s[i].name;
   cout<<"\n\t Roll no                   : "<<s[i].rno;
   cout<<"\n\t Marks in Maths            : "<<s[i].m1;
   cout<<"\n\t Marks in Science          : "<<s[i].m2;
   cout<<"\n\t Marks in Computer Science : "<<s[i].m3;
   cout<<"\n\t Total Percentage          : "<<(s[i].m1+s[i].m2+s[i].m3)/3;
  }
}
void sort()
{
 int ch;
 cout<<"\n\n\n\t|||||*****------------------------------------------*****|||||";
 cout<<"\n\n\n\t\t| 1. Sort according to total percentage          |";
 cout<<"\n\n\t\t| 2. Sort according to marks in Maths            |";
 cout<<"\n\n\t\t| 3. Sort according to marks in Science          |";
 cout<<"\n\n\t\t| 4. Sort according to makrs in Computer Science |";
 cout<<"\n\n\n\t|||||*****------------------------------------------*****|||||";
 cout<<"\n\n\n\t\tEnter your choice [1/2/3/4] : ";
 cin>>ch;
 if(ch==1)
 percent();
 if(ch==2)
 maths();
 if(ch==3)
 science();
 if(ch==4)
 cs();
}
void percent()
{
 flaot temp;
 for(int i=0;i<3-1;i++)
  for(int j=i+1;j<3;j++)
   if(s[i].percent>s[j].percent)
   {
     temp=s[i].percent;
     s[i].percent=s[j].percent;
     s[j].percent=temp;
    }

 cout<<"\n\n\n\t|||||*****------------------------------------------*****|||||";
 cout<<"\n\n\n\t\tSorted list according to total percentage";
 cout<<"\n\n\n\n\n\tROLL NO"<<setw(12)<<"NAME"<<setw(13)<<"MATHS"<<setw(12)<<"SCIENCE"<<setw(8)<<"C.S";
 for(i=0;i<3;i++)
  cout<<"\n\t  "<<s[i].rno<<setw(16)<<s[i].name<<setw(12)<<s[i].m1<<setw(10)<<s[i].m2<<setw(9)<<s[i].m3<<endl;
 cout<<"\n\n\n\t|||||*****------------------------------------------*****|||||";
}
void maths()
{
 float temp;
 for(int i=0;i<2;i++)
  for(int j=i+1;j<3;j++)
   if(s[i].m1>s[j].m1)
   {
     temp=s[i].m1;
     s[i].m1=s[j].m1;
     s[j].m1=temp;
    }

 cout<<"\n\n\n\t|||||*****------------------------------------------*****|||||";
 cout<<"\n\n\n\t\tSorted list marks in Maths";
 cout<<"\n\n\n\n\n\tROLL NO"<<setw(12)<<"NAME"<<setw(13)<<"MATHS"<<setw(12)<<"SCIENCE"<<setw(8)<<"C.S";
 for(i=0;i<3;i++)
  cout<<"\n\t  "<<s[i].rno<<setw(16)<<s[i].name<<setw(12)<<s[i].m1<<setw(10)<<s[i].m2<<setw(9)<<s[i].m3<<endl;
 cout<<"\n\n\n\t|||||*****------------------------------------------*****|||||";
}
void science()
{
 float temp;
 for(int i=0;i<3-1;i++)
  for(int j=i+1;j<3;j++)
   if(s[i].m2>s[j].m2)
   {
     temp=s[i].m2;
     s[i].m2=s[j].m2;
     s[j].m2=temp;
    }

 cout<<"\n\n\n\t|||||*****------------------------------------------*****|||||";
 cout<<"\n\n\n\t\tSorted list according to makrs in Science";
 cout<<"\n\n\n\n\n\tROLL NO"<<setw(12)<<"NAME"<<setw(13)<<"MATHS"<<setw(12)<<"SCIENCE"<<setw(8)<<"C.S";
 for(i=0;i<2;i++)
  cout<<"\n\t  "<<s[i].rno<<setw(16)<<s[i].name<<setw(12)<<s[i].m1<<setw(10)<<s[i].m2<<setw(9)<<s[i].m3<<endl;
 cout<<"\n\n\n\t|||||*****------------------------------------------*****|||||";
}
void cs()
{
 float temp;
 for(int i=0;i<3-1;i++)
  for(int j=i+1;j<3;j++)
   if(s[i].m3>s[j].m3)
   {
     temp=s[i].m3;
     s[i].m3=s[j].m3;
     s[j].m3=temp;
    }

 cout<<"\n\n\n\t|||||*****------------------------------------------*****|||||";
 cout<<"\n\n\n\t\tSorted list according to marks in Computer Science";
 cout<<"\n\n\n\n\n\tROLL NO"<<setw(12)<<"NAME"<<setw(13)<<"MATHS"<<setw(12)<<"SCIENCE"<<setw(8)<<"C.S";
 for(i=0;i<3;i++)
  cout<<"\n\t  "<<s[i].rno<<setw(16)<<s[i].name<<setw(12)<<s[i].m1<<setw(10)<<s[i].m2<<setw(9)<<s[i].m3<<endl;
 cout<<"\n\n\n\t|||||*****------------------------------------------*****|||||";
}
Last edited on
Line 123, 142 etc. for(i=0... should be for(int i=0....
m1, m2, m3, percent are floats and temp should also be float, not int.

1
2
for(int i=0;i<3-1;i++)
for(int j=i+1;j<3;j++)

Using "magic numbers" is considered bad practice, you should really pass number of students as a parameter to a function... For example, because of them you have one logical error on line 161 - you are printing only two students.
Last edited on
Ok so i changed all the int temp; to float temp; and i have updated the code. Also i am planing to pass the number of students as a parameter but this is just for testing. Still the sorting doesnt work even after i have changed temp to float. Now, i can't give for(int i=0... at line 123 as i have already declared int i in line 111 so declareing it again would be multiple declarations. Thanks for the help anyway :D
No. That i on line 111 has local scope only, i. e. only for that specific for loop.

error C2065: 'i' : undeclared identifier
16 times
Last edited on
How is it like that for you? I have no problem in running the program with my current code excepting the fact that sorting doesnt work and when i give for(int i=0... in like 123 i get error saying multiple declaration for i.

You can see that in this link here
http://imgur.com/zwGERQC
Oh, sorry then.
Different compiler, compiling on almost highest level of warnings :D

Btw...
You need to swap whole structures, not just e. g. m1 fields.
I haven't noticed that first time :)
1
2
3
4
5
6
7
8
9
student temp;
 for(int i=0;i<2;i++)
  for(int j=i+1;j<3;j++)
   if(s[i].m1>s[j].m1)
   {
     temp=s[i];
     s[i]=s[j];
     s[j]=temp;
    }

Etc.

Also line 110 misspelling and 161 should be < 3 instead of < 2.
Last edited on
Topic archived. No new replies allowed.