Parallel Arrays

I'm stuck on a project I'm supposed to do for class having to do with parallel arrays.

What I'm supposed to do:
• Write a program that tracks the number of Star Wars figures owned by a collector.
• The program should use two parallel arrays of size 5: an array of strings that holds the Stars Wars character name and an array of integers that holds the number of figures for each character.
• The program should have the user enter the 5 character names.
• The program should then read through the character name array to prompt the user to enter the number of figures for that character.
• Once the array has been populated, display the contents of the array, the total figures in the collection, the character name with the highest number, and the character name with the lowest number.
• Input Validation: Do not accept negative values for number of figures.
• Produce output as shown below.

My program runs up until it asks for "Character 4" and then it crashes so I'm unable to see if the rest of my code is right.

This is what I have so far:


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

using namespace std;

int main() {
	string characters[5];
	int figures[5];
	int i;
	int mostFigs = 0;
	int leastFigs =99;
	int totalFigs = 0;

	cout << "***************Star Wars Collection***************" << endl << endl;

	//Character Names
	cout << "Enter Star Wars Character Names" << endl;
	for (i = 1; i <= 5; ++i) {
		cout << "	Character " <<i<<": ";
		getline(cin, characters[i]);

	}
	cout << endl;

	//Number of figures
	cout << "Enter the number of figures for each Star Wars character in the collection" << endl;
	do {
		for (i = 1; i <= 5; ++i) {
			cout << "	" << characters[i] << " : ";
			cin >> figures[i];
			if (figures[i] > mostFigs) {
				mostFigs = figures[i];
			}
			if (figures[i] < leastFigs) {
				leastFigs = figures[i];
			}
		}
	} while (figures > 0);
	cout << endl;

	//Collection Report
	cout << "Star Wars Collection Report" << endl << endl;
	cout << "Character		Collection Count" << endl;
	cout << "---------------------------------" << endl;
	cout << characters[1] << "		" << figures[1] << endl;
	cout << characters[2] << "		" << figures[2] << endl;
	cout << characters[3] << "		" << figures[3] << endl;
	cout << characters[4] << "		" << figures[4] << endl;
	cout << characters[5] << "		" << figures[5] << endl;
	cout << endl;

	for (i = 1; i <= 5; i++) {
		totalFigs += figures[i];
	}

	cout << "Total Figures:		" << totalFigs << endl << endl;

	cout << "Most Collected Character: " << mostFigs << endl;
	cout << "Least Collected Character: " << leastFigs << endl;
}
Last edited on
Array indices start at 0 and end at N-1, where N is the size of the array.

So your loop indices on lines 19, 29 and 53 will cause you to go outside array bounds.
Okay. The online examples I would see did start at 0 but the reason I started at i was to get the cout << characters[i] to start at "Characters 1".
Hello @mudkipass,
There are two quick fixes to this if you want numbering to the screen in our normal counting system.
(1) You could offset the output to the screen by writing << i+1 << on line 20.
(2) Alternatively, you could just have an array of size 6, so that the final index is 5, and you just ignore the array elements at index 0. Since 5 is a bit of a "magic number", I would sometimes convey my intention in code by writing
1
2
3
const int MAX = 5;
string characters[1+MAX];
int figures[1+MAX];


What you do is entirely personal preference, but just make sure that you don't access arrays beyond bounds. Anything could happen, depending on what is in the following memory slot. Russian roulette isn't a good way of coding.
Gotcha. I ended up just doing cout << character << i+1 << endl; That seems to be working. I'm just not sure if it will affect the array in any way?
It won't affect the array in the slightest. It's just up to you to remember that the number you wrote to the screen is 1 higher than the index in the array.

I'd still get away from having magic numbers though. I don't know how many Star Wars characters there are, but cinematically there seems to be an ever-increasing number of films. It makes it easier to change if you have a single variable indicating array size.

Or, of course, you could use expansible vector<type>.
Got it. Now the only issue I'm having with the program is that at the end Most Collected Character and Least Collected Character are showing the corresponding value. How do I get it to show the character instead?
For Most-Collected-Character, loop through whatever index range you have decided on and output characters[i] if figures[i] == mostFigs. (There may be more than one modal character, obviously).

Then do the same for leastFigs.


Looking back at your original code (which may have changed since), I haven't a clue what your do-while statements (lines 28 and 39) are supposed to do. I should remove them.
Last edited on
Yeah I got rid of the do while loop. I thought I needed it. Also, I'm confused on what you're saying. I'm not sure what you're trying to say?
Topic archived. No new replies allowed.