setprecision problem

Have a coding error i cant find the issue.
currently passing by 96% - 4% off being a issue on how the final piece is coming out
i think the problem is down in the setprecision. i set it to 4, yet when the final stats come out its oddly printed as below

1
2
3
4
5
6
7
Candidate     Votes Received    % of Total Votes
Richmond             5000        2.18341
Johns               60000           26.2
Hopkins             25000          10.92
Smith               80000          34.93
McCoy               59000          25.76
Total 229000


i need the john line to read 26.20 to pass without changing anything out.
what am i doing wrong
 
Johns               60000           26.2


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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void getPercent(int votes, int totalVotes[], double percent[]);
void printResult(int total, string lastName[], int vt[], double percent[]);
int winInd(int vt[]);

const int CNDTS = 5;

int main()

{
string lastName[CNDTS];
int votes[CNDTS] = { 0 };
int totalVotes = 0;
double percent[CNDTS] = { 0.0 };

for (int i = 0; i < CNDTS; i++)
   {
   cout << "Candidate Votes Received % of Total Votes" << endl;
   cout << "candidate " << (i+ 1) << "votes received: ";
		cin >> lastName[i] >> votes[i];

		totalVotes += votes[i];
	}


getPercent(totalVotes, votes, percent);
printResult(totalVotes, lastName, votes, percent);

system("pause");
		return 0;
}


	/* function to print the result*/

void printResult(int total, string lastName[], int votesTotal[], double percent[])
{
    cout << "Candidate     Votes Received    % of Total Votes\n";
    for (int i = 0; i < CNDTS; i++)
    {

	   cout << left << setw(10) << lastName[i] /*creates the output grid*/
		  << right << setw(15) << votesTotal[i]
		  << right << setw(15) << percent[i] << setprecision(4)
		  << "\n";
    }

    cout << "Total " << total << "\n"; // It obtains the total from the main function (line 24 on main)
    cout << "The Winner of the Election is " << lastName[winInd(votesTotal)];

}

int winInd(int votesTotal[])
{
    int largest = 0;
    int position = 0;
    for (int i = 0; i < CNDTS; i++)
    {
	   // If it finds the largest value, then record the larfest variable
	   // and record its position by recording the index.
	   if (votesTotal[i] > largest) /*will find the largest sum*/ 
	   {
		  largest = votesTotal[i];
		  position = i;
	   }

    }
    return position; // Return the index of the largest value.
}


	/*function to calculate the percentages*/
	void getPercent(int votes, int totalVotes[], double percent[])
	{
		for (int i = 0; i < CNDTS; i++)
		{ /*Forumla to find the percent*/
			percent[i] = static_cast<double>(totalVotes[i]) / static_cast<double>(votes) * 100;
		}

	}


You need to set the precision before you print the value, not after. Also you will probably want to use fixed and showpoint.

And remember that setprecision, showpoint, and fixed only need to be called once since they are "sticky".

If you're using fixed then you shouldn't need showpoint.
Candidate     Votes Received    % of Total Votes
Richmond             5000        2.18341
Johns               60000           26.2
Hopkins             25000          10.92
Smith               80000          34.93
McCoy               59000          25.76
Total 229000


Summing the % of Total I get 99.99341% what is not 4% off as you say.
I think the OP is saying he's only getting 96% grade for the assignment, because 4% is being knocked off for how the final results are being displayed.

Cheddar99 (101)
I think the OP is saying he's only getting 96% grade for the assignment, because 4% is being knocked off for how the final results are being displayed.


Correct.
Currently moving around the set precision trying to find a place to put it without breaking the code, but cant find a spot at this very moment
jlb (4173)
You need to set the precision before you print the value, not after. Also you will probably want to use fixed and showpoint.

And remember that setprecision, showpoint, and fixed only need to be called once since they are "sticky".


Actually i didn't need to change the precision, i used the show point after the set precision like you said and it fixed the 4% error. Thank you

Final code that works
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void getPercent(int votes, int totalVotes[], double percent[]);
void printResult(int total, string lastName[], int vt[], double percent[]);
int winInd(int vt[]);

const int CNDTS = 5;

int main()

{
string lastName[CNDTS];
int votes[CNDTS] = { 0 };
int totalVotes = 0;
double percent[CNDTS] = { 0.0 };

for (int i = 0; i < CNDTS; i++)
   {
   cout << "Candidate Votes Received % of Total Votes" << endl;
   cout << "candidate " << (i+ 1) << "votes received: ";
		cin >> lastName[i] >> votes[i];

		totalVotes += votes[i];
	}


getPercent(totalVotes, votes, percent);
printResult(totalVotes, lastName, votes, percent);

system("pause");
		return 0;
}


	//function to print the result

void printResult(int total, string lastName[], int votesTotal[], double percent[])
{
    cout << "Candidate     Votes Received    % of Total Votes\n";
    for (int i = 0; i < CNDTS; i++)
    {

	   cout << left << setw(10) << lastName[i] //creates the output grid
		  << right << setw(15) << votesTotal[i]
		  << right << setw(15) << percent[i] << setprecision(4) << showpoint
		  << "\n";
    }

    cout << "Total " << total << "\n"; // It obtains the total from the main function (line 24 on main)
    cout << "The Winner of the Election is " << lastName[winInd(votesTotal)];

}

int winInd(int votesTotal[])
{
    int largest = 0;
    int position = 0;
    for (int i = 0; i < CNDTS; i++)
    {
	   // If it finds the largest value, then record the larfest variable
	   // and record its position by recording the index.
	   if (votesTotal[i] > largest) //will find the largest sum
	   {
		  largest = votesTotal[i];
		  position = i;
	   }

    }
    return position; // Return the index of the largest value.
}





	//function to calculate the percentages
	void getPercent(int votes, int totalVotes[], double percent[])
	{
		for (int i = 0; i < CNDTS; i++)
		{ //Forumla to find the percent
			percent[i] = static_cast<double>(totalVotes[i]) / static_cast<double>(votes) * 100;
		}

	}
OK, in that case I would do without the 4% as 96% is much more than it is worth with this funny display.
Consider the 80/20-rule, it will take you 20% of the effort to achieve 80% of the task, but it will take you 80% to finish the remaining 20%.
Topic archived. No new replies allowed.