?
Solved

Whats wrong with my candidate code?

Posted on 2008-10-28
5
Medium Priority
?
2,266 Views
Last Modified: 2013-12-14
Good Evening,
Here is my problem:

Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should output each candidate's name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is:

Candidate          Votes Received           % of Total Votes

Johnson           5000                              25.91
Miller                 4000                              20.73
Duffy                6000                              31.09
Robinson         2500                               12.95
Ashtony          1800                                9.33
Total                19300

The Winner of the Election is Duffy.


Any further input you guys could provide would be great. I'm very new to C++ so my code is a little weak, but its worth a start. Thank you for any input you can provide

// Program Name:		Vote Counter
// Program Purpose:		User inputs votes which are later calculated, tallied and displayed.
// Created By:			Tom Jackson
// Date Created:		28 October 2008
 
#include <iostream>
#include <cctype>
#include <iomanip>
 
using namespace std;
 
int main()
{
 
string people[5];
int votes[5];
double percent[5];
int counter2 = 0;
int sum = 0;
int counter;
 
	for (counter = 0; counter < 5; counter++)
	{
		counter2++;
		cout << "Enter last name of candidate #" << counter2 << ": ";
		cin >> people[counter];
		cout << "Enter amount of votes received for candidate #" << counter2 << ": ";
		cin >> votes[counter];
	}
 
	for (counter = 0; counter < 5; counter++)
	{
		sum = sum + votes[counter];
	}
 
	for (counter = 0; counter < 5; counter++)
	{
		percent[counter] = (votes[counter] / sum) * 100;
	}
	cout << "Candidate" << setw(7) << "Votes Received" << setw(7) << "% of Total Votes" << endl;
	
	for (counter = 0; counter < 5; counter++)
	{
		cout << people[counter] << setw(5) << votes[counter] << setw(5) << percent[counter] << endl;
	}
	cout << "Total" << setw(15) << sum << endl;
	cout << "The Winner of the Election is: << " << endl;
 
system("pause");
return 0;
 
}

Open in new window

0
Comment
Question by:tomsterkw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:sh0e
ID: 22828213
cout << "The Winner of the Election is: << " << endl;

You didn't finish writing the code for the winner, and the quote is in the wrong place.  What are you supposed to do if it's a tie?

The logic should be:
max = -1
winner = 0
for each candidate: if # votes of candidate bigger than max: max = # votes, winner = candidate
print candidate
0
 

Author Comment

by:tomsterkw
ID: 22828250
I'll have to finish writing this portion first. What would be a simple for loop to compare the percentages to find out who is the winner?
(This winner would be the candidate with the highest % of total votes)

I'm not too worried about the tie situation for this example but I would assume it could go to either of them.
0
 
LVL 16

Accepted Solution

by:
sh0e earned 100 total points
ID: 22828462
I wouldn't use percentages, simply because the percentages have a potential margin of error.  I'd just count number of votes.
But if you want to use percentages it will look something like this:
double max = -1;
int winner;
for each candidate {
  if max < percent[counter] {
    max=percent[counter]
    winner = counter
  }
}
print people[winner];
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 100 total points
ID: 22830443
To retrieve the winner you may change the last loop to

                idxWinner      = 0;

      for (counter = 0; counter < 5; counter++)
      {
                     if (percent[counter] > percent[idxWinner])
                     {
                           idxWinner = counter ;
                      }
           cout << people[counter] << setw(5) << votes[counter] << setw(5) << percent[counter] << endl;
      }
     
                cout << "The Winner of the Election is: << people[idxWinner] << endl;
0
 

Author Closing Comment

by:tomsterkw
ID: 31511037
Thank you very much for your detailed explaninations. I was able to utilize both comments and solve the issue with outputting the winner of the election.

Thanks for your help!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question