Having a serious problem with my Homework.

My homework states: "Write a program that allows the user to enter the last names of five canadiates in a local election and the number of votes received by each candidate. The program should then output each candidate's name, the number of votes received, and the percentage of the total votes received by each candidate."

I need a lot of help here but...I pretty much have the gist of what I need to do. I just don't know how to do figure out the percent of a canadidates votes.
Here is what I've mustered up 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

#include <iostream>
#include<Iomanip>
#include<string>

int main()
{

int votes[5];
int candidate[5];
int sum;
int counter;
int index;
int largestscale;

cout << "Enter in the Candidate's name and then the five numbers for the votes" << endl;

sum =0;
for (counter =0; counter < 5; counter++)
{   //input of names and number of votes
	cin >> candidate[counter];
	cin >> votes[counter];
	sum = sum + votes[counter]; //finds the sum

}

	//Finds the largest number of votes in the array
	maxIndex = 0;
	for (index = 1; index < 10; index++)
		if (votes[maxIndex] < votes[index])
			maxIndex = index;
	largestscale = sales[maxIndex]
//Begin of output.
for (counter =0; counter < 5; counter++)
{
	cout << "Candidate:::::::" << candidate[counter];
	cout << "Amount of votes::" << votes[counter];
cout << "The total number of votes is" << sum << endl;
}
cout <<"Largest amount of votes are::" << largestscale << endl;
	}
Found the following errors;

1. using namespace std; missing from headers.
2. maxIndex line 28 undeclared identifier. Put int in front.
3. sales line 32 undeclared identifier. Decide what you are doing here.
4. no semi-colon at the end of line 32.
5. curly braces after for loop line 29 and closing brace after line 32.
6 Just noticed... return 0; before closing brace in int main and also
add my favourite
cin.ignore().get();
before return 0; just to keep the console window from closing
at the end of the program.

... then you've got a host of other problems. Look at strings arrays and cin input etc.
Last edited on
ill give you the pseudo code for % of candidates vote

1
2
3
//total sum = sum(5-n)
//n is from 1 to 4
//sum(5-n)/total sum = % of canadidates votes 
Why not
candidate result / total * 100 = percentage.
Okay thanks for the help so far!! It really helped. I cleaned it up a bit and put decided to use parrallel arrays im pretty sure that's what needs to be done so the name of the candidate, his/her votes, and percentage of the vote all comes up in output. It complies now so its free of syntax error but my logic is off. When i try to find the percent instead of dividing by sum then multiplying by 100 and assigning it to an place in the array percent. It just multiplies all the votes by 100.... :| What am I doing wrong here?

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()
{

	//Parallel Array
int votes[50];
string candidate[50];
double percent[50];

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

cout << "Enter in the Candidate's name *Hit SPACE* Then enter the votes" << endl;
counter = 0;

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

}
		

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

}
cout << "Total number of votes is" << sum << endl;
for (x = 0; x < 5; x++)
{
percent[x] = (votes[x] / sum) * 100; //<---I'm guessing my main problem is somewhere here?
}

	//Finds the largest number of votes in the array
	int maxIndex = 0;
	for (index = 0; index < 5; index++)
	{
		if (votes[maxIndex] < votes[index])
			maxIndex = index;
	largestscale = votes[maxIndex];
	}

//Begin of 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 is" << candidate[largestscale] << endl;

cin.ignore().get(); 
return 0;
	}
integer division.

since both votes[x] and sum are integers, the result will be an integer. For example 1 / 2 is zero (not 0.5). And zero * 100 is zero.

To avoid this loss, the easiest way would be to multiply by 100.0 first:

percent[x] = votes[x] * 100.0 / sum;

Note the multiplication by 100.0 (not 100). This will make the calculations be floating point. Another option would be to cast either votes[x] or sum to a double.
Thanks for the help, everyone :)
Topic archived. No new replies allowed.