Homework Issues

So I'm writing a program that has the user input five Candidate names and their total votes. The code is as follows:

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

using namespace std;

int main()
{

int votes[50];
string candidate[50];
float percent[50];

int sum;
int counter;
int index;
int x;
int i;

cout << "Enter in the Candidate's name (Space between) then enter their votes" << endl;
counter = 0;

for (counter = 0; counter < 5; counter++)
{
cin >> candidate [counter];  
cin >> votes [counter];

    //Find the sum.
}
		

sum = 0;
for (i = 0; i < 5; i++)
{  
	sum = sum + votes [i];

}
cout << "Total number of votes is: " << sum << endl;
for (x = 0; x < 5; x++)
{
percent [x] = votes [x] * 100.0 / sum;
}

	//Find the largest number of votes in the array

	int maxIndex = 0;
	for (index = 0; index < 5; index++)
	{
		if (votes [maxIndex] < votes [index])
			maxIndex = index;
	}

//Begin the output.

cout << "Candidate" << " " << "# of votes" << "" << "Percent of vote" << endl;
for (counter =0; counter < 5; counter++)
{
	
	cout << candidate [counter] << ", " << votes [counter] << ", " << percent [counter] << endl;
}

cout << "The Winner of the Election is " << candidate [maxIndex] << endl;
 
return 0;
	}


However, the percentage for Duffy and Robinson should be shown as "61.78 for Duffy" and "0.93 for Robinson"

The output is shown as:

Enter in the Candidate's name (Space between) then enter their votes
Johnson
100
Miller
233
Duffy
800
Robinson
12
Ashtony
150
Total number of votes is: 1295
Candidate # of votesPercent of vote
Johnson, 100, 7.72201
Miller, 233, 17.9923
Duffy, 800, 61.7761
Robinson, 12, 0.926641
Ashtony, 150, 11.583
The Winner of the Election is Duffy
Last edited on
And the problem is??
I need the percentage for Duffy and Robinson to be 61.78 & 0.93
Hello Horror,

A "float" will work, but "double" is the preferred floating point type.

ne555 has shown you what to read up on which is what you need.

You may like the output this bit of code produces.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Begin the output.
std::cout << std::fixed << std::showpoint << std::setprecision(2);

std::cout << "\n Candidate" << "  " << "# of votes" << "  " << "Percent of vote" << std::endl;  // <--- Added a space in the second "".

for (counter = 0; counter < 5; counter++)
{

	std::cout << ' ' << std::left << std::setw(8) << candidate[counter]
		<< "    " << std::setw(4) << votes[counter]  // <--- Added two spaces in the "  " and removed the comma.
		<< "   " << std::setw(7) << ' '  // <--- Added one space in the "  " and removed the comma.
		<< std::right << std::setw(5) << percent[counter] << std::endl;
}

std::cout << "\n The Winner of the Election is " << candidate[maxIndex] << std::endl;

The "std::showpoint" tells the "cout" stream to print ".00" should it arise.

If there is any part inside the for loop that you do not understand let me know.

Hope that helps,

Andy
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
Topic archived. No new replies allowed.