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
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>
#include <functional>
#include <algorithm>
#include <vector>
using namespace std;
class People
{
public:
string name, rank;
float age, weight;
bool People::operator<(const People& other) const
{
return (rank > other.rank);
}
} ppl[100];
int main()
{
int z, i;
string w, x, y, t;
cout << "How many people are there? ";
getline (cin, w);
stringstream(w) >> z;
cout << " \n";
for (i=0; i<z; i++)
{
cout << "What is his name? ";
getline (cin, ppl[i].name);
cout << "How old is he? ";
getline (cin, x);
stringstream(x) >> ppl[i].age;
cout << "How much does he weigh? ";
getline (cin, y);
stringstream(y) >> ppl[i].weight;
cout << " \n";
}
cout << "Now, how do you want to rank the people, age or weight)? " << endl;
getline (cin, t);
cout << " \n";
for (i=0; i<z; i++)
{
ppl[i].rank = t;
}
std::sort( &ppl[0], &ppl[z] );
for (i=0; i<z; i++)
{
cout << ppl[i].name << " is " << ppl[i].age << " years old and weighs " << ppl[i].weight << " pounds. \n";
}
return 0;
}
|