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;
}
|