Arrays small program

I have to:
Creates an array of 5 that will accept the names of five animals
Creates an array of 5 that will store a list of five animal ages
Accept the names and ages of one to five animals.
stopping at either 5 or when the person enters the word STOP, stop or Stop.
Displays the following (5 points)
a. name of the Program author and the Authors major
b. Displays a blank line
c. Displays a list of animals and their ages.

This is what I have so far. It's not displaying the names and age and crashes.
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
  #include <iostream>
#include <string>
#include <iomanip>

using namespace std; 

int main() {
	const int NAME = 5;
	const int AGE = 5;
	string animal[NAME];
	int animals[AGE] = { 2, 4, 6, 8, 10 };

	cout << "Enter the names of five animals: " << endl; 
	for (int i = 0; i < NAME; ++i) {
		cout << "Enter name " << i + 1 << "'s name: "; 
		getline(cin, animal[i]);
	}

	cout << "Name of author: XXXX"; 
	cout << "Major: Computer Science; Public Health Science";
	cout << "Animal";
	for (int j = 0; j < NAME; ++j) {
		cout << "#" << j + 1; 
		cout << animal[NAME];
	}

	for (int y = 0; y < AGE; ++y) {
		cout << "#" << y + 1;
		cout << animals[AGE];
	}

	system("pause");
	return 0;
}


Any help would be appreciated
Last edited on
cout << animal[NAME]; is wrong.
name is 5, and your array is 0-4 (5 total)
and logically, besides that (that is the crash reason) it should be
cout << animal[j];

and you repeat this mistake.
Thank you for your response jonnin. So I corrected everything and it works perfectly now. However, I'm not sure how to add in the stop part. "Accept the names and ages of one to five animals.
stopping at either 5 or when the person enters the word STOP, stop or Stop."

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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main() {
	const int NAME = 5;
	const int AGE = 5;
	string animal[NAME];
	int animals[AGE] = { 2, 4, 6, 8, 10 };

	cout << "Enter the names of five animals: " << endl;
	for (int i = 0; i < NAME; i++) {
		cout << "Enter animal #" << i + 1 << ": ";
		getline(cin, animal[i]);
	}

	cout << "Name of author: XXXXXXX" << endl;
	cout << "Major: Computer Science;" << endl;
	cout << "Animal" << endl;
	cout << "# \tname \tage" << endl;
	for (int j = 0; j < NAME; j++) {
		cout << j + 1 << "\t";
		cout << animal[j] << "\t";
		cout << animals[j] << endl;
	}

	system("pause");
	return 0;
}
I added in

1
2
	if (animal[i] == "STOP" || "Stop" || "stop") {
			break;


inside the first loop but it doesn't work
1
2
if (animal[i] == "STOP" || animal[i] == "Stop" || animal[i] == "stop") {
			break;


You have to compare animal[i] with each string
Got it!Thank you so much
I have to do the same with vectors. And I'm pretty much done, however, do you know what's wrong with line 41?

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
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
	
	vector<string> names(5); 
	vector<int> age(5);
	string animals;
	int animal_age;

	cout << "Enter the names of five animals: " << endl;
	for (decltype(names.size()) i = 0; i < names.size(); ++i) {
		cout << "Enter animal #" << i + 1 << ": ";
		cin >> animals;
		names.push_back(animals);
		names[i] = animals;

		if (animals == "STOP" || animals == "Stop" || animals == "stop") {
			break;
		}
	}

	cout << "Enter the ages of the five animals: " << endl;
	for (decltype(age.size()) i = 0; i < age.size(); ++i) {
		cout << "Enter age of" << i + 1 << ": ";
		cin >> animal_age;
		age.push_back(animal_age);
		age[i] = animal_age;
	}

	cout << "Name of author: XXXXX" << endl;
	cout << "Major: Computer Science; Public Health Science" << endl;
	cout << "Animal" << endl;
	cout << "# \tname \tage" << endl;
	for (int j = 0; j < names.size(); j++) {
		cout << j + 1 << "\t";
		cout << animals[j] << "\t";
		cout << animal_age[j] << endl;
	}

	system("pause");
	return 0;
}

	
Last edited on
Line 41 : animal_age is not an array. You can look at the line 12.

int animal_age;
I fixed that but, when i ask to enter the name and age, it doesn't stop after 5 entries. It only stops when I type stop.
Line 18 :

names.push_back(animals);

Why do you want to extend the size of the vector, while you wanted the size of the vector to be 5 from the start? It is safe to remove this line.
Last edited on
In the directions it says:
Create a Vector that will accept the names of five animals
Create a Vector that will store a list of five animal ages
Accept the names and ages of one to five animals. (5 points)
stopping at either 5 or when the person enters the word STOP, stop or Stop.
You MUST use push_back when adding items to the vector.
You MUST use push_back when adding items to the vector.

If that is the case, you can start with empty vectors.

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
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {

        // You could just use the number 5 everywhere, it is my suggestion.
	
	vector<string> names(5);  // Empty vectors don't need this
	vector<int> age(5);
	string animals;
	int animal_age;

	cout << "Enter the names of five animals: " << endl;
	for (int i = 0; i < 5; ++i) {
		cout << "Enter animal #" << i + 1 << ": ";
		cin >> animals;
		names.push_back(animals);

		if (animals == "STOP" || animals == "Stop" || animals == "stop") {
			break;
		}
	}

	cout << "Enter the ages of the five animals: " << endl;
	for (int i = 0; i < 5; ++i) {
		cout << "Enter age of" << i + 1 << ": ";
		cin >> animal_age;
		age.push_back(animal_age);
	}

	cout << "Name of author: XXXXX" << endl;
	cout << "Major: Computer Science; Public Health Science" << endl;
	cout << "Animal" << endl;
	cout << "# \tname \tage" << endl;
	for (decltype(names.size()) j = 0; j < names.size(); j++) {
		cout << j + 1 << "\t";
		cout << names[j] << "\t";
		cout << age[j] << endl;
	}

	system("pause");
	return 0;
}
Last edited on
Mantorr22: you're checking for STOP/stop/Stop after this entry has already been pushed back into names. Perhaps you meant to be doing this before push_back or, pop_back names if STOP/stop/Stop though this is more drawn out?
gunnerfunner : If the OP sees it, he would like to move the code a little bit so that it fixes the problem. It is not that hard though.
Got it! It works if I do the stop line before push_back. Thank you all
It is not that hard though.

doesn't matter whether it's hard or not, what matters is whether the order of steps is logically sound
Last edited on
Topic archived. No new replies allowed.