Help. Sorting trouble
Feb 24, 2013 at 11:33am UTC
Hi,
Help me with this. All I wanted to do is to sort the car names according to their mileage. And display the output with their car names.
This is what I've made
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
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
int j;
class car
{
public :
string make,model,colour,reg;
int year,mileage[5],i;
public :
car();
void Car();
void getmake();
void getmodel();
void getcolour();
void getreg();
void getyear();
void getmileage();
void sorting();
};
car a;
int temp;
car::car()
{
make=" " ;
model=" " ;
colour=" " ;
reg=" " ;
year=0;
mileage[5]=0;
}
void car::getmake()
{
cout<<" Please Enter Car's manufacturer\t" ;
cin>>make;
}
void car::getmodel()
{
cout<<"\nPlease enter Car's model\t" ;
cin>>model;
}
void car::getcolour()
{
cout<<"\nPlease Enter Car's colour\t" ;
cin>>colour;
}
void car::getreg()
{
cout<<"\n Please enter Car's registration Number\t" ;
cin>>reg;
}
void car::getyear()
{
cout<<"\nPlease Enter the Year of Purchasing\t" ;
cin>>year;
}
void car::getmileage()
{
cout<<"\nPlease Enter 5 Car's Mileage\t" ;
for (i=0;i<5;i++)
cin>>mileage[i];
}
void car::sorting()
{
for (i=0;i<5;i++)
{
for (j=i+1;j<5;j++)
{
if (a.mileage[i]<a.mileage[j])
temp=a.mileage[i];
a.mileage[i]=a.mileage[j];
a.mileage[j]=temp;
}}
}
void car::Car()
{
getmake();
getmodel();
getcolour();
getreg();
getyear();
getmileage();
cout<<"\nThe current car is " <<make<<model<<colour<<" with Registration Number " <<reg<<"purchased in the year " <<year<<" With " ;
sorting();
for (i=0;i<5;i++)
cout<<mileage[i]<<" miles..\n" ;
}
main()
{
a.Car();
getch();
}
I need to take 5 car details and sort them according to their Mileage..
Thanks in Advance
Feb 24, 2013 at 12:04pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void car::sorting()
{
for (i=0;i<5;i++)
{
for (j=i+1;j<5;j++)
{
if (a.mileage[i]<a.mileage[j])
{
temp=a.mileage[i];
a.mileage[i]=a.mileage[j];
a.mileage[j]=temp;}
}
}
}
You forgot the braces around if in car::sorting().
Feb 24, 2013 at 12:26pm UTC
Thanks for the help sam806. that really helped me
i've maede some modification in the code
But the Problem is I was able to sort mileage of the cars but not to the specified car that it was supposed to be. here is the 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
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
//int j;
class car
{
public :
string make,model,colour,reg;
int year,mileage,i;
public :
car();
void Car();
void getmake();
void getmodel();
void getcolour();
void getreg();
void getyear();
void getmileage();
void sorting();
void display();
};
car a;
int temp;
car::car()
{
make=" " ;
model=" " ;
colour=" " ;
reg=" " ;
year=0;
mileage=0;
}
void car::getmake()
{
cout<<" Please Enter Car's manufacturer\t" ;
cin>>make;
}
void car::getmodel()
{
cout<<"\nPlease enter Car's model\t" ;
cin>>model;
}
void car::getcolour()
{
cout<<"\nPlease Enter Car's colour\t" ;
cin>>colour;
}
void car::getreg()
{
cout<<"\n Please enter Car's registration Number\t" ;
cin>>reg;
}
void car::getyear()
{
cout<<"\nPlease Enter the Year of Purchasing\t" ;
cin>>year;
}
void car::getmileage()
{
cout<<"\nPlease Enter Car's Mileage\t" ;
//for(i=0;i<5;i++)
cin>>mileage;
}
/*void car::sorting()
{
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i].mileage<a[j].mileage)
temp=a.mileage[i];
a.mileage[i]=a.mileage[j];
a.mileage[j]=temp;
}}
}*/
void car::Car()
{
getmake();
getmodel();
getcolour();
getreg();
getyear();
getmileage();
//cout<<"\nThe current car is "<<make<<model<<colour<<" with Registration Number "<<reg<<"purchased in the year "<<year<<" With ";
// sorting();
//for(i=0;i<5;i++)
//cout<<mileage<<" miles..\n";
}
void car::display()
{
cout<<"\n\tThe details according to their Mileage" ;
cout<<model<<"\t" <<make<<"\t" <<"\t" <<colour<<"\t" <<reg<<"\t" <<year<<"\t" <<mileage;
}
main()
{
int i,j;
car a[5];
//a[6].sorting()
//{
//}
for (i=0;i<5;i++)
a[i].Car();
for (i=0;i<5;i++)
{
for (j=i+1;j<5;j++)
{
if (a[i].mileage<a[j].mileage)
{temp=a[i].mileage;
a[i].mileage=a[j].mileage;
a[j].mileage=temp;
}}}
for (i=0;i<5;i++)
a[i].display();
getch();
}
Feb 24, 2013 at 12:26pm UTC
Please ignore the comments
Feb 24, 2013 at 12:50pm UTC
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
main()
{
int i,j;
car a[5];
//a[6].sorting()
//{
//}
for (i=0;i<5;i++)
a[i].Car();
for (i=0;i<5;i++)
{
for (j=i+1;j<5;j++)
{
if (a[i].mileage<a[j].mileage)
{
car temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}}}
for (i=0;i<5;i++)
a[i].display();
getch();
}
You are sorting mileages. You have to sort cars.
Topic archived. No new replies allowed.