Hello Horror,
You define your arrays with a size of 50. Why all the wasted space? I realize that the "5" may be for testing, but there is a better way to cut down on wasted space. Using a vector would be a better choice or something like this:
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
|
int main()
{
constexpr size_t MAXSIZE{ 5 };
int votes[MAXSIZE]{ 100, 233, 800, 12, 150 };
std::string candidate[MAXSIZE]{ "Johnson","Miller","Duffy","Robinson","Ashtony" };
double percent[MAXSIZE]{};
int sum{}; // <--- Initialize your variables.
int counter{};
//int index{}; // <--- These are best defined in the for loops.
//int x{};
//int i;
std::cout << "Enter in the Candidate's name (Space between) then enter their votes" << std::endl;
//counter = 0; // <--- Not needed because "counter" is zeroed in the for loop.
for (counter = 0; counter < MAXSIZE; counter++)
{
//std::cin >> candidate[counter];
//std::cin >> votes[counter];
// <--- Needs a way out so you do not go through all 50.
//Find the sum.
sum += votes[counter]; // <--- Doing this here avoids the next for loop
}
|
This way changing the value on line 3 will change the value of "MAXSIZE" anywhere it is used in the program.
By defining "counter" outside the for loop you can use it in other for loops, i.e., (in the middle part as < counter), to use only the part of the array that has been used and not the whole.
As a note: the first for loop could be written as:
for (counter = 0, sum = 0; counter < MAXSIZE; counter++)
This way you can initialize or set two or more variables at one time.
Adding to the "sum" inside the for loop is easier than creating a for loop just to get the total votes.
For line 16 I have found it better to prompt for each input separately as many users are gormless when following instructions.
It is up to you to decide if this should be before the for loop or inside the for loop. Keep in mind that if you actually enter fifty names and votes the original prompt will scroll off the screen.
Hope that helps,
Andy