[try Beta version]
Not logged in

 
array of stract

May 2, 2015 at 2:55pm
Write your question here.I need to enter the desired group and sort alphabetically the names of students in this group.Explain how to do this sort,please.

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
  #include<iostream>
#include<iomanip>
#include<cstring>
using namespace std;
struct student
{
	char name[20];
	char facultet[20];
	int dat;
	int group;
};
int main(){
	int n,i,y,j,g;
	char s[30];
	char tmp[30];
	student m[n];
	cout<<"vedite n"<<endl;
	cin>>n;
	for(i=0;i<n;i++)
	{
		cout<<"name"<<endl;
		cin>>m[i].name;
		cout<<"facultet"<<endl;
		cin>>m[i].facultet;
		cout<<"dat"<<endl;
		cin>>m[i].dat;
		cout<<"group"<<endl;
		cin>>m[i].group;
	}
	cout<<"nygniy facultet=";
	cin>>s;
	for(i=0;i<n;i++)
		if(!strcmp(m[i].facultet,s)){
			cout<<m[i].name<<endl;
		
	}
		
		cout<<"data=";
		cin>>y;
		for(i=0;i<n;i++)
			if(m[i].dat>y){
				cout<<m[i].name<<endl;
			}
			cout<<"enter group";
			cin>>g;
			for(i=0;i<n-1;i++)
				for(j=i+1;j<n;j++)
					if(strcmp(m[i].name,m[j].name)>0)
					{
						strcpy(tmp,m[i].name);
						strcpy(m[i].name,m[j].name);
						strcpy(m[j].name,tmp);
					}
					
					
					for(i=0;i<n;i++)
						cout<<m[i].name<<endl;
return 0;
}Put the code you need help with here.
May 2, 2015 at 7:55pm
Why do you use C-strings instead of std::strings? The latter would simplify the rest of the code.

Your lines 50-52 are wrong, if you are supposed to sort the students. The line 48 correctly determines the need to swap by name, but you have to swap entire student objects, not just their names.
May 3, 2015 at 6:44pm
I have started learning C ++, just three months ago , and not very good at it.
Where can I see examples of this swap ?
May 3, 2015 at 6:59pm
1
2
3
auto temp = m[i];
m[i] = m[j];
m[j] = temp:

However, as long as you have char arrays in your struct (and don't overrride copy constructor and copy assignment) that will fail. The easiest way is to use std::string instead of char arrays.
May 4, 2015 at 9:08am
Thank you for your help and explanations.
Topic archived. No new replies allowed.