0
Assign each track runner a roster number and calculate the average age of the team. As output the program should print the roster in three columns like in the following format: Roster # Name Age 1 First Name 18 2 Second Name 19 3 Third Name 19 4 Fourth Name 20 5 Fifth Name 18 The average age of the team is 18.80.
Your source code must make use of the following A looping structure (for, do-while, while) A struct Use string data type for text variables Validate user input and ask for corrections if invalid input detected Use formatted output for the team roster
1 Respuesta
+ 1
// includes
struct People{
string new_name;
double new_age;
};
bool check_name(string name){
/* bla bla Just check name - return true if incorrect, false if is ok. */
}
int main() {
vector < People > vec;
string name;
double age, sum=0;
int N;
do{
cin.clear();
cin.sync();
cout << "How many people?: ";
cin >> N;
while(!cin.good() || N <= 0);
while(N-- > 0){
cout << "Enter your name: ";
getline(cin, name);
cout << "Enter your age: ";
cin >> age;
if(!cin.good() || check_name() || age <= 0){
cin.clear();
cin.sync();
N++;
}else{
People *p = new People;
p->new_name = name;
p->new_age = age;
vec.push_back(p);
delete p;
}}
cout << "Roaster #\t\tName\t\tAge" << endl;
for(int i = 0; i < vec.size(); i++){
cout << i+1 << "\t\t" << vec[i].new_name << "\t\t" << vec[i].new_age << endl;
sum+=vec[i].new_age;
}
cout << "The average age of the team is" << sum/vec.size() << endl;
return 0;
}
Try it.