-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask1.cpp
More file actions
122 lines (113 loc) · 3.31 KB
/
Task1.cpp
File metadata and controls
122 lines (113 loc) · 3.31 KB
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
120
121
122
//CS211 Lab 7
//Task 1
//Max Davy
#include <iostream>
#include <string>
using namespace std;
/*Task 1:
Build a basic Zoo Animal Tracker. The zoo has 5 animals. Track each animal's name, age, and health rating (out of 100).
Requirements:
• Define a struct Animal.
• Use an array of 5 Animal objects.
• Use a function to input data, another to print a health report.
• Optionally let the user “treat” a sick animal by improving its health rating*/
struct animal{
string name;
int age;
int health;
string species;
};
animal createAnimal(string name, int age, int health, string species);
animal createAnimal();
void displayAnimal(animal animal1);
animal updateAnimal(animal animal1);
int main() {
animal animals[5];
cout<<"Welcome to super handy zoo animal tracker\n";
cout<<"You will be asked to enter information for five animals\n";
for(int i = 0;i<5;i++){
cout<<"Animal "<<i+1<<":\n";
animals[i] = createAnimal();
}
char input = 'd';
int selected = 0;
do {
cout<<"What do you want to do?\n\td: display animal\n\tu: update animal\n\tq: quit\n";
cin>>input;
switch(input) {
case 'd':
cout<<"Okay, what animal do you want?\n";
cin>>selected;
if(selected>5||selected<1){
cout<<"That isn't a valid selection\n";
} else {
displayAnimal(animals[selected-1]);
}
break;
case 'u':
cout<<"Okay, what animal do you want?\n";
cin>>selected;
if(selected>5||selected<1){
cout<<"That isn't a valid selection\n";
} else {
animals[selected-1]=updateAnimal(animals[selected-1]);
}
break;
default:
cout<<"Okay, closing application.\n";
}
}while (input != 'q');
return 0;
}
animal createAnimal(string name, int age, int health, string species) {
animal animal1;
animal1.name = name;
animal1.age = age;
animal1.health = health;
animal1.species = species;
return animal1;
}
animal createAnimal() {
animal animal1;
cout<<"Enter animal name:\n";
cin>>animal1.name;
cout<<"Enter animal age:\n";
cin>>animal1.age;
cout<<"Enter animal health (1-100):\n";
cin>>animal1.health;
cout<<"Enter animal species:\n";
cin>>animal1.species;
return animal1;
}
void displayAnimal(animal animal1){
cout<<"Animal \""<<animal1.name<<"\" stats:\n";
cout<<"\tAge: "<<animal1.age<<endl;
cout<<"\tHealth: "<<animal1.health<<endl;
cout<<"\tSpecies: "<<animal1.species<<endl;
}
animal updateAnimal(animal animal1) {
char input;
cout<<"What do you want to update?\n\ta: age\n\tn: name\n\th: health\n\ts: species\n";
cin>>input;
switch(input) {
case 'a':
cout<<"Enter animal age:\n";
cin>>animal1.age;
break;
case 'n':
cout<<"Enter animal name:\n";
cin>>animal1.name;
break;
case 'h':
cout<<"Enter animal health:\n";
cin>>animal1.health;
break;
case 's':
cout<<"Enter animal species:\n";
cin>>animal1.species;
break;
default:
cout<<"That wasn't one of the options. Returning you to main menu.";
}
return animal1;
}