wanted to ask, can I store my object in a class in a variable so that I can print the ranking name.
Question is write a program that ask for the names of five runners and the time it took each of them to finish a race. The program should display who came in first, second and third place. Only accept positive numbers for the times.
Any modification that I should make ?
#include <iostream>
using namespace std;
class runner{
public:
char name[50];
float time;
void input(){
cout <<"\n Enter the name: ";
cin>> name;
cout <<"\n Enter the time taken to finish the race (mins): ";
cin>> time;
}
};
int main(){
runner runners[5];
for(int i =0;i<5; i ){
runners[i].input();
}
int i,first, second, third, fourth, fifth;
fifth = fourth = third = first = second = INT_MIN;
char firstname, secondname, thirdname, fourthname, fifthname;
for(int i =0;i<5; i ){
if(runners[i].time>first){
fifth = fourth;
fourth = third;
third = second;
second = first;
first = runners[i].time;
}
else if(runners[i].time> second){
fifth = fourth;
fourth = third;
third = second;
second = runners[i].time;
}
else if(runners[i].time>third){
fifth = fourth;
fourth = third;
third = runners[i].time;
}
else if(runners[i].time>fourth){
fifth = fourth;
fourth = runners[i].time;
}
else if(runners[i].time>fifth){
fifth = runners[i].time;
}
}
cout << first <<","<< second <<","<< third <<","<< fourth<< ","<< fifth<<endl;
return 0;
}
CodePudding user response:
Yes, you can.
You can write a custom compare function:
class runner
{
public:
std::string name; // why not std::string?
float time;
void input()
{
std::cout <<"\n Enter the name: ";
std::cin >> name;
std::cout <<"\n Enter the time taken to finish the race (mins): ";
std::cin >> time;
}
};
bool cmp (const runner &lhs, const runner &rhs)
{
return lhs.time < rhs.time;
}
std::ostream& operator<< (std::ostream& out, const runner& run)
{
out << run.name << ' ' << run.time;
}
int main()
{
runner runners[5];
for(int i =0;i<5; i )
{
runners[i].input();
}
std::sort(runners, runners 5, cmp);
for(int i {4}; i >= 0; --i)
{
std::cout << runners[i] << ' ' << i << ',';
}
}
Or you can overload the <
operator:
class runner
{
public:
std::string name;
float time;
void input()
{
std::cout <<"\n Enter the name: ";
std::cin >> name;
std::cout <<"\n Enter the time taken to finish the race (mins): ";
std::cin >> time;
}
};
bool operator< (const runner &lhs, const runner &rhs)
{
return lhs.time < rhs.time;
}
int main()
{
runner runners[5];
for(int i =0;i<5; i )
{
runners[i].input();
}
std::sort(runners, runners 5);
for(int i {4}; i >= 0; --i)
{
std::cout << runners[i] << ' ' << i << ',';
}
}
CodePudding user response:
You can simplify your program by using std::multiset
as shown below:
#include <iostream>
#include <string>
#include <set>
//using namespace std; //don't use this
class runner{
public:
std::string name; //use string instead of array of char
float time;
void input(){
std::cout <<"\n Enter the name: ";
std::cin>> name;
std::cout <<"\n Enter the time taken to finish the race (mins): ";
std::cin>> time;
}
//overload operator< since we are using std::set
bool operator<( const runner &r ) const
{
return ( time < r.time );
}
};
int main(){
std::multiset<runner> runners; //use multiset instead of array
for(int i =0;i<5; i ){
runner tempRunner;
tempRunner.input();
runners.insert(tempRunner);
}
int i = 0;
//print out the details as asked in the assignment question. You can adjust
//the output according to your needs for example if you want to display only first
//3 position details or what if all the first three persons in the multiset have the same timing
for(const runner &tempRunner: runners)
{
std::cout<<tempRunner.name <<" came: "<<i 1<<std::endl;
i;
}
return 0;
}
Note multiset
is used instead of set
because more than one runner can have the same time.