Hello. I'm working on an array where I have to sum up Star Wars figures. Code looks like this at the moment:
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
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main() {
string names[6];
int num[6];
int i;
int figSum = 0;
int greaterFigures = 0;
int leastFigures = 0;
cout << "****************** Star Wars Collection*********************" << endl << endl;
cout << " Enter Star Wars Character Names" << endl;
for (i = 1; i <= 5; ++i) {
cout << " Character " << i << " : ";
getline(cin, names[i]);
}
cout << endl;
cout << " Enter number of figures in collection for each Star Wars Character" << endl;
for (i = 1; i <= 5; ++i) {
cout << " " << names[i] << " : ";
cin >> num[i];
}
cout << endl << endl;
cout << " Star Wars Collection Report" << endl << endl;
cout << " Character Collection Count" << endl;
cout << " _________________________________" << endl;
cout << " " << names[1] << setw(14) << num[1] << endl;
cout << " " << names[2] << setw(16) << num[2] << endl;
cout << " " << names[3] << setw(21) << num[3] << endl;
cout << " " << names[4] << setw(16) << num[4] << endl;
cout << " " << names[5] << setw(16) << num[5] << endl;
cout << endl;
for (i = 1; i <= 5; ++i) {
figSum += num[i];
if (num[i] > greaterFigures) {
greaterFigures = num[i];
}
if (num[i] < leastFigures) {
leastFigures = num[i];
}
}
cout << " Total Figures:" << setw(11) << figSum << endl << endl;
cout << " Most Collected Character: " << greaterFigures << endl;
cout << " Least Collected Character: " << leastFigures;
cout << endl << endl;
return (0);
}
| |
Inputs for names in order are: Darth Vader, Chewbacca, Finn, Hans Solo, Boba Fett.
Respective amounts are: 20, 10, 5, 100, 3.
With the setw(), it all lines up properly.
My issues are, how do I arrange the values of the numbers to the right without having to use the setw()? What if I were to put different names, the white space wouldn't be consistent. I also need to show the names of the characters instead of the numbers, but i'm unsure how to show that from the arrays. Lastly, I have to make sure that negative numbers aren't accepted for amounts of figures. I think it has to be a while loop, but it acts strangely when I use that. Help is very appreciated.