43 lines
1016 B
C++
43 lines
1016 B
C++
|
#include <iostream>
|
||
|
using namespace std;
|
||
|
|
||
|
void printRow(string name, string species, int age){
|
||
|
cout << "Pet: " << name << " is a " << species << " and is " << age << " year(s) old" << endl;
|
||
|
}
|
||
|
|
||
|
int main(){
|
||
|
const int arraySize = 5;
|
||
|
string petName[arraySize];
|
||
|
string petSpecies[arraySize];
|
||
|
int petAge[arraySize];
|
||
|
// populate arrays
|
||
|
for(int i=0; i < arraySize; i++){
|
||
|
cout << "Pet name: ";
|
||
|
cin >> petName[i];
|
||
|
cout << "Pet species: ";
|
||
|
cin >> petSpecies[i];
|
||
|
cout << "Pet age: ";
|
||
|
cin >> petAge[i];
|
||
|
}
|
||
|
|
||
|
// modify this program to:
|
||
|
// ask for a pet species
|
||
|
// and a maximum age
|
||
|
// and print only entries that match that
|
||
|
// hint: put an if-statement in the for loop below
|
||
|
int maxAge = 0;
|
||
|
string species;
|
||
|
|
||
|
cout << "Max age: ";
|
||
|
cin >> maxAge;
|
||
|
cout << "Species: ";
|
||
|
cin >> species;
|
||
|
//print arrays:
|
||
|
for(int i=0; i < arraySize; i++){
|
||
|
if(petAge[i] <= maxAge && petSpecies[i] == species){
|
||
|
printRow(petName[i],petSpecies[i],petAge[i]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|