how to create a sort function ???


I want to sort information of some cars according to their ( ID )
but i can not create a function that do this work( sort ) for me !
can you help me to solve it ??
and also in showing information and search them in the file i have a big truble

i dont know why my functions dont work please answer me fast

here is my code

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
  // final project.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <math.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
					
using namespace std;
struct Pdate  // Produce date
{
	int year;
	int month;
	int day;
};
struct Car
{
	char name[30];
	char ID[10];
	char color[30];
	Pdate date;
};

char showMenu();
void getInfo(Car s[]);
void getInfoFile();
void showInformation();
void search();
void Editinfo();
void main()
{
	while (true)
	{
		char s;
		while(1)
		{
			s=showMenu();
			switch(s)
			{
			case '1' :
				system ("cls");
				getInfoFile();
				break;
			case '2' :
				showInformation();
				break;
			case '3' :
				search();
				break;
			case '4' :

				break;
			case '5' :
				return;
			default:
				cout<<"\n\n\n\t\t\t Error !!!!!! " ;
				_getch();
				break;
			}//switch
		}// 
	}
}//main

/*void getInfo(Car s[])
{
	static int index=0;
	cout<<" Enter Name :  " ;
	cin.getline(s[index].name,30);
	cin.getline(s[index].name,30);
	cout<<" What Is The Color of Car :  ";
	cin>>s[index].color;
	cout<<" Enter ID :  ";
	cin>>s[index].ID;
	cout<<" Enter production date [ year , month , day ] : ";
	cin>>s[index].date.year>>s[index].date.month>>s[index].date.day;
	index++;
}*/
void getInfoFile()
{
	Car s;
	fstream CarsInfo("cars.txt",ios::app);
	cout<<"\n	Enter Name of car: ";
	cin.getline(s.name,30);
	cin.getline(s.name,30);
	cout<<"\n	What Is The Color of Car : ";
	cin>>s.color;
	cout<<"\n	Enter ID of car: ";
	cin>>s.ID;
	cout<<"\n	Enter production date [ year , month , day ] : ";
	cin>>s.date.year>>s.date.month>>s.date.day;
	CarsInfo<<s.name<<" * " <<s.color<<" * "<<s.ID<<" * "<<s.date.year<<" "<<s.date.month<<" "<<s.date.day<<"\t\n\n";
}
char showMenu()
{
	char usreSelect;
	system("cls");
	cout<<"\n	";
	cout<<"\n	1 : Get Information " ;
	cout<<"\n	2 : Show information" ;
	cout<<"\n	3 : Search Information ";
	cout<<"\n	4 : Edit Information " ;
	cout<<"\n	0 : For Exit ";
	cout<<"\n\n	Enter your Selection  ::  ";
	cin>>usreSelect;
	return usreSelect;
}
void showInformation()
{
	char temp[20];
	int i=1;  //  for showing rows
	system("cls");
	fstream fp("cars.txt",ios::in);
	cout<< "Row	NAME	color	ID \n";
	while(! fp.eof() ) 
	{
		cout<<" "<<i++<<" ";
		fp>>temp;
		cout<<setw(10)<<temp<<" ";
		fp>>temp;
		fp>>temp;
		cout<<setw(10)<<temp<<" ";
		fp>>temp;
		fp>>temp;
		cout<<setw(10)<<temp<<endl;
}

_getch();
}
void search()
{
	system("cls");
	Car s[10];
	fstream CarsInfo("cars.txt",ios::in);
	for (int i = 0; i < 5; i++)
	{
			
			CarsInfo.open("cars.txt",ios::in);
			char w[10];
			cout<<"\n	enter id of wanted car : ";
			cin.getline(w,10,'0');
			if(strcmp(s[i].ID,w)==0)
			{
				cout<<"\n !! car found !! see informations below !!";
				cout<<"\n\n	"<<s[i].name<<" * "<<s[i].color<<" * "<<s[i].ID<<" * "
					<<s[i].date.year<<" "<<s[i].date.month<<" "<<s[i].date.day<<" !!";
	
			}
			else
				cout<<"\n	there is no car with entered info !?!";

	}

	_getch();
}

and this is the out put but just void getInfoFile() do his job right !!!!!


        1 : Get Information
        2 : Show information
        3 : Search Information
        4 : Edit Information
        0 : For Exit

        Enter your Selection  ::    


Last edited on
Do you know how to "Bubble sort" ?
No I dont know what is it !!
can you explain it for me ??
Here is a program that sorts integers in ascending order (the comments in the code explain the algorithm):

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
#include <iostream>

// in an array of integers 'a' of size 'sz' (sz>0),
// find and return the position of the smallest element
std::size_t position_of_smallest_element( const int a[], std::size_t sz )
{
    std::size_t pos_smallest = 0 ;

    for( std::size_t i = 1 ; i < sz ; ++i )
        if( a[pos_smallest] > a[i] ) pos_smallest = i ;

    return pos_smallest ;
}

// in an array of integers 'a' of size 'sz' (sz>0),
// bring the smallest element to the front
void bring_smallest_element_to_front( int a[], std::size_t sz )
{
    std::size_t pos_smallest = position_of_smallest_element( a, sz ) ;

    // swap smallest element with the one at the front (at position zero)
    int temp = a[0] ;
    a[0] = a[pos_smallest] ;
    a[pos_smallest] = temp ;
}

// sort an array of integers 'a' of size 'sz'
void sort( int a[], std::size_t sz )
{
    if( sz < 2 ) return ; // only zero or one element, nothing to be done

    // bring the smallest element of the array to the front
    bring_smallest_element_to_front( a, sz ) ;

    // a[0] already contains the smallest elenent of the array
    // all that remains to be done is: sort the remaining elements of the array
    // the remaining elents start at a+1, and have a size of sz-1
    sort( a+1, sz-1 ) ;
}

int main()
{
    int a[] = { 6, 2, 8, 3, 7, 6, 1, 9, 7, 4, 2, 5, 3 } ;
    for( int v : a ) std::cout << v << ' ' ;
    std::cout << '\n' ;

    sort( a, sizeof(a) / sizeof( a[0] ) ) ;

    for( int v : a ) std::cout << v << ' ' ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/914606d8b30ab61a
thank you
Topic archived. No new replies allowed.