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
|
#include <iostream>
#include <string>
using namespace std;
class Person {
private:
string name,eye_color;
int age, weight;
bool likes_cuny;
public:
Person(); // Default CTOR
//Initializes string name taking a value from user
void Display();
// Precondition : name is assigned through constructor, age, weight and eye color are assigned through
//set values function
// Postcondition : Will print Person's information
string getName();
//Precondition: takes name assigned through constructor.
//Postcondition: returns name (string) to user.
int getAge();
//Precondition: takes in value assigned to priv member 'age'
//PostCondition: returns that value to user.
int getWeight();
//Precondition: takes in value assigned to priv member 'weight'
//PostCondition: returns that value to user.
string getColor();
//Precondition: takes in value assigned to priv member 'eye_color'
//PostCondition: returns that value to user.
void setValues();
//Precondition: Reads in age, weight and eyecolor from user.
//PostCondition: Function allocates values to priv data age, weight and eyecolor.
};
void swap(int &a, int& b);
//Precondition: function takes two variables and their values
//PostCondition: swaps the values of the variables
void mySort (int A[], int s);
//Precondition: function takes an array and its size.
//PostCondition: starting at the first index, it compares the values stored in those indexes
// and if the current value is higher than the starting index, it will swap their value
// Order of the array will be in descending order.
int main()
{
int amount;
cout << "How many people are we initializing? Min 2 ";
cin >> amount;
int AgeArr[amount]; //Array of size = to amount, will be used later to store the different ages.
for (int i =1; i<=amount;i++){ //loop that defines objects and sets their values/displays
Person personi;
personi.setValues();
personi.Display();
for (int j = 0; j<amount; j++){ //loop that attempts to assign value to index of ageArr by using getAgefunction
AgeArr[j] = personi.getAge();
if (AgeArr[j] < AgeArr[j+1]) //Helps sort the array in descending order
mySort (AgeArr,amount);
// in case there are equal values it will let the user know. Here's the problem!
//it repeats the name of the same person twice. Guessing it's due to post increment, but i can't really see it.
if (AgeArr[j] == AgeArr[j+1])
cout << personi.getName() << " & " << personi.getName << " have the same age: " << AgeArr[j+1];
}
}
return 0;
}
//Defining the constructor
Person::Person() {
cout << "Enter name of the person: ";
cin >> this->name;
}
//Display function definition
void Person::Display(){
cout << name << " is " << age << " years old and weights " << weight << " lbs."
<< " His/Her eyes are color " << eye_color << "." <<endl;
}
//getAge function definition
int Person::getAge(){
return age;
}
//getWeight fucntion definition
int Person::getWeight(){
return weight;
}
//getColor Function Definition
string Person::getColor(){
return eye_color;
}
//getName function definition
string Person::getName(){
return name;
}
//setValues function definition
void Person::setValues(){
cout<<"Enter " << name << "'s age: ";
cin >> age;
cout<<"Enter " << name << "'s weight: ";
cin >> weight;
cout << "Enter " << name << "'s eye color: ";
cin >> eye_color;
}
//swap function definition
void swap(int &a, int &b){
int temp = a;
a = b;
b = temp;
}
//sort fucntion definition
void mySort (int A[], int s){
for (int i = 0; i < s-1; i ++){
for (int j = 0; j< s-i-1; j++)
if (A[i] < A[i+1]){
swap(A[i], A[i+1]);
}
}
}
|