#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
class SIX
{
private: struct PERSON
{
string name;
int age;
float gpa;
};
PERSON p[4];
public: //read data into array p
void ReadData()
{
for (int i = 0; i < 4; ++i)
{
cout << "Enter a name and age: ";
cin >> p[i].name >> p[i].age;
}
}
//display array p
void Display()
{
cout << " " << "NAME" << setw(10) << "AGE" << endl;
cout << " " << "-------------------" << endl;
for (int i = 0; i < 4; ++i)
{
cout << " " << p[i].name << setw(10) << p[i].age; cout << endl;
}
}
//find their age average
int AgeAve()
{
int total = 0;
for (int i = 0; i < 4; ++i)
total += p[i].age;
return total / 4;
}
//display the name of those whose age is above average
void DisplayAboveAve()
{
int total = 0;
int AgeAverage;
for (int i = 0; i < 4; ++i)
{
total += p[i].age;
AgeAverage = total / 4;
if (p[i].age > 24)
cout << p[i].name << " ";
}
cout << endl;
}
};
int main()
{
int AgeAverage;
SIX t;
t.ReadData();
cout << " This is the list of people" << endl;
t.Display();
t.AgeAve();
cout << "The following people age is above average ";
cout << endl;
t.DisplayAboveAve();
system("pause");
return 0;
}
Enter a name and age: John 22
Enter a name and age: Mary 19
Enter a name and age: Cynthia 33
Enter a name and age: George 25
This is a list of people
NAME AGE
John 22
Mary 19
Cynthia 33
George 25
The following people age is above the average :
Cynthia George
Press any key to continue . . .
How do I make the ages stay straight like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Enter a name and age: John 22
Enter a name and age: Mary 19
Enter a name and age: Cynthia 33
Enter a name and age: George 25
This is a list of people
NAME AGE
John 22
Mary 19
Cynthia 33
George 25
The following people age is above the average :
Cynthia George
Press any key to continue . . .
#include <iostream>
#include <string>
#include <iomanip>
#include <string.h>
usingnamespace std;
class SIX
{
private: struct PERSON
{
char name[20];//you can extend it later
int age;
float gpa;
};
PERSON p[4];
public: //read data into array p
void ReadData()
{
for (int i = 0; i < 4; ++i)
{
cout << "Enter a name and age: ";
cin >> p[i].name >> p[i].age;
}
}
//display array p
void Display()
{
cout << " " << "NAME" << setw(19) << "AGE" << endl;
cout << " " << "-------------------------" << endl;
for (int i = 0; i < 4; ++i)
{
cout << " " << p[i].name; //now going for strlen;
int siz;
siz=strlen(p[i].name);
for(int p=0;p<(20-siz);p++) //just hope that the size of name is less than 20 or you can increase it accordingly
cout<<" ";
cout<< p[i].age;
cout << endl;
}
}
//find their age average
int AgeAve()
{
int total = 0;
for (int i = 0; i < 4; ++i)
total += p[i].age;
return total / 4;
}
//display the name of those whose age is above average
void DisplayAboveAve()
{
int total = 0;
int AgeAverage;
for (int i = 0; i < 4; ++i)
{
total += p[i].age;
AgeAverage = total / 4;
if (p[i].age > 24)
cout << p[i].name << " ";
}
cout << endl;
}
};
int main()
{
int AgeAverage;
SIX t;
t.ReadData();
cout << " This is the list of people" << endl;
t.Display();
t.AgeAve();
cout << "The following people age is above average ";
cout << endl;
t.DisplayAboveAve();
system("pause");
return 0;
}
PS: I got this idea from the relative velocity principle (in physics)
@programmer007:
There is no reason whatsoever to replace the std::string with a char array. The string offers everything the array has and more. The array is not safe.
Some methods (the reference documentation explains all those manipulators):