Trouble getting percentage of number.

I am trying to finish the last bits of this homework assignment. My code works for the most part but one line is causing me trouble
Everything is working and outputting in an acceptable way but line 36 isnt storing anything in votePercent. My output shows all 0.00 for each line. Any help you could give would be great!

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
 1 #include <iostream>
2 #include <iomanip>
3 #include <cstring>
4 #include <string>
5 
6 using namespace std;
7 
8 int main()
9 {
10	int numberOfVotes[5];
11	string candidateName[5];
12	double votePercent;
13	int votesTotal = 0;
14	
15	
16	for (int i = 0; i < 5; i++)
17	{
18		cout << "Enter name of candidate: ";
19		cin >> candidateName[i];
20		cout << endl;
21
22		cout << "Enter " << candidateName[i] << "'s number of votes: ";
23		cin >> numberOfVotes[i];
24		cout << endl;
25		votesTotal = votesTotal + numberOfVotes[i];
26
27	}
28	
29	cout << fixed << showpoint << setprecision(2);
30	cout << setw(6) << "Candidate Name" << setw(15) << "Votes Recieved" << setw(15) << "Percentage of Vote";
31	cout << endl << endl;
32
33	for (int i = 0; i < 5; i++)
34	{
35		cout << setw(6) << candidateName[i] << setw(15) << numberOfVotes[i] << setw(15);
36		votePercent = numberOfVotes[i] / votesTotal * 100;
37		cout << votePercent << endl;
38	}
39	cout << votesTotal << endl;
40
41	system("pause");
42	return 0;
43}
Last edited on
To get the percentage to show I had to convert votesTotal to a double.
http://www.cplusplus.com/doc/oldtutorial/typecasting/

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

 using namespace std;

 int main()
 {
	 int numberOfVotes[5];
	 string candidateName[5];
	 double votePercent;
	 int votesTotal = 0;


	 for (int i = 0; i < 5; i++)
	 {
		 cout << "Enter name of candidate: ";
		 cin >> candidateName[i];
		 cout << endl;

		 cout << "Enter " << candidateName[i] << "'s number of votes: ";
		 cin >> numberOfVotes[i];
		 cout << endl;
		 votesTotal = votesTotal + numberOfVotes[i];

	 }

	 cout << fixed << showpoint << setprecision(2);
	 cout << setw(6) << "Candidate Name" << setw(15) << "Votes Recieved" << setw(15) << "Percentage of Vote";
	 cout << endl << endl;

	 for (int i = 0; i < 5; i++)
	 {
		 cout << setw(6) << candidateName[i] << setw(15) << numberOfVotes[i] << setw(15);
		 votePercent = (numberOfVotes[i] / (double)votesTotal) * 100;
		 cout << votePercent << endl;
	 }
	 cout << votesTotal << endl;

	 system("pause");
	 return 0;
 }
That was it! Thank you!
One question for you if you can answer it though... What is the difference between static_cast<double>(votesTotal) , and the way you did it with just (double)votesTotal???
I am not sure. An explanation would be nice from someone who knows.
What is the difference between static_cast<double>(votesTotal) , and the way you did it with just (double)votesTotal???

In this case, it would do the same thing as (double)votesTotal.

However, in general, there's a couple factors, some technical, some not.
static_cast<type>(var) is the modern C++ way of doing it. [/b]
(type)var is the old C way of doing it.

The advantages of C++'s static_cast are that it is more type-safe, and can improve readability by making meaning unambiguous. static_casts only allow type-to-type conversion, and not pointer-to-type conversion or vice-versa. C-style casts don't guarantee this, so it's easier to introduce a bug. Especially in C++, parentheses are a commonly overloaded operator. Having the "cast" keyword immediately makes it clear to the programmer that the cast is intentional. C++ generally tries to be more type-safe than C, so in practice you should need less casts than the old C days. Lots of casting points to some flaw in design. A cast every now and then to make division work is fine, though.

It also exists to explicitly differentiate between casts like reinterpret_cast and dynamic_cast, which are also useful, but offer different behavior than static_cast.
Last edited on
Topic archived. No new replies allowed.