What\'s wrong with this code? I don\'t know why it\'s not working #include <iost
ID: 3800274 • Letter: W
Question
What's wrong with this code? I don't know why it's not working
#include <iostream>
using namespace std;
class People {
std::string name;
double height;
public:
void setName(string name) {
this->name = name;
}
void setHeight(double height) {
this->height = height;
}
double getHeight() {
return height;
}
std::string getName() {
return name;
}
};
int main()
{
People people[100];
string name;
double height;
int tallest = 0;
int smallest = 0;
for (int i = 0; i<100; i++) {
cout << " Enter name";
cin >> name;
cout << " Enter height";
cin >> height;
people[i].setName(name);
people[i].setHeight(height);
if (people[tallest].getHeight() < height) {
tallest = i;
}
if (people[smallest].getHeight() > height) {
smallest = i;
}
}
cout << " The tallest person is " << people[tallest].getName();
cout << " The smallest person is " << people[smallest].getName();
return 0;
}
Explanation / Answer
#include <iostream>
using namespace std;
class People {
string name;
double height;
public:
void setName(string name) {
this->name = name;
}
void setHeight(double height) {
this->height = height;
}
double getHeight() {
return height;
}
string getName() {
return name;
}
};
int main()
{
string name;
double height;
double tallest;
double smallest;
int no_of_persons;
int tallindex,smallindex;
cout<<"How many persons height you want to compare :";
cin>>no_of_persons;
People people[no_of_persons];
for (int i = 0; i<no_of_persons; i++) {
cout << " Person "<<i+1<<" name :";
cin >> name;
cout << " Person "<<i+1<<" height :";
cin >> height;
people[i].setName(name);
people[i].setHeight(height);
cout<<endl;
}
tallest=people[0].getHeight();
smallest=people[0].getHeight();
for(int i=0;i<no_of_persons;i++)
{
if (tallest<people[i].getHeight())
{
tallest=people[i].getHeight();
tallindex=i;
}
if (smallest>people[i].getHeight())
{
smallest=people[i].getHeight();
smallindex=i;
}
}
cout << " The tallest person is " << people[tallindex].getName()<<endl;
cout << " The smallest person is " << people[smallindex].getName()<<endl;
return 0;
}
___________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.