User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,860 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,955 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 704 | Replies: 7
Reply
Join Date: Nov 2007
Location: Anderson,AL
Posts: 19
Reputation: abarnett is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
abarnett abarnett is offline Offline
Newbie Poster

Candidate Program

  #1  
Nov 6th, 2007
Need ideas for this program:

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 pgoram should then output each candidate's name, the number of votes received, and the percentage of the total voates recieved by the candidate. The program should also output the winner of the election.

Ok So my thought is that I need 3 arrays: candidatenames[5]; votes received[5]; and percentOfTotalVotes[5]; but I'm not sure if that's right.

I haven't actually wrote code for it yet, just trying to get an idea of where to start. Please and thank you
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,070
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 419
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Candidate Program

  #2  
Nov 6th, 2007
>but I'm not sure if that's right.
Who cares? Try it and see if it works. This program is small enough that you can comfortably experiment.

>I haven't actually wrote code for it yet, just trying to get an idea of where to start.
You have an idea of where to start. It strikes me that your problem is confidence, and the best way to build confidence is to jump right in. Do something, anything, even if it turns out to be wrong. As it is you'll spend all of your time second guessing yourself and then you'll come to us at the last minute with the usual "my project is due in an hour and I haven't done anything!".
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Feb 2006
Location: UK
Posts: 468
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Rep Power: 5
Solved Threads: 42
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Candidate Program

  #3  
Nov 6th, 2007
Originally Posted by Narue View Post
It strikes me that your problem is confidence, and the best way to build confidence is to jump right in. Do something, anything, even if it turns out to be wrong. As it is you'll spend all of your time second guessing yourself and then you'll come to us at the last minute with the usual "my project is due in an hour and I haven't done anything!".

That appears to be almost every Computer Science student in the world these days - How fewer posts would this place get if students had 'more confidence'?
Last edited by Bench : Nov 6th, 2007 at 12:06 pm.
¿umop apisdn upside down?
Reply With Quote  
Join Date: Nov 2007
Location: Anderson,AL
Posts: 19
Reputation: abarnett is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
abarnett abarnett is offline Offline
Newbie Poster

Re: Candidate Program

  #4  
Nov 6th, 2007
alright I've got a start but there's got to be a shorter way of doing this. I admit that I don't really know much about arrays. However this program works, except The % of total Votes is wrong and then I'm not sure how to get it to output the winner. Help now please!

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	string name1, name2, name3, name4, name5;
	int votes1, votes2, votes3, votes4, votes5, totalvotes;
	int percent1, percent2, percent3, percent4, percent5;

	cout << "Please Enter Last Name of 5 Candidates: " << endl;
	cin >> name1;
	cout << endl;
	cin >> name2;
	cout << endl;
	cin >> name3;
	cout << endl;
	cin >> name4;
	cout << endl;
	cin >> name5;
	cout << endl;

	cout <<" Please Enter the Votes Received by each Candidate: " << endl;
	cin >> votes1;
	cout << endl;
	cin >> votes2;
	cout << endl;
	cin >> votes3;
	cout << endl;
	cin >> votes4;
	cout << endl;
	cin >> votes5;
	cout << endl;

	totalvotes = votes1 + votes2 + votes3 + votes4 + votes5;
	percent1 = votes1 / totalvotes;
	percent2 = votes2 / totalvotes;
	percent3 = votes3 / totalvotes;
	percent4 = votes4 / totalvotes;
	percent5 = votes5 / totalvotes;



	cout << "Candidate" << "    " << " Votes Received " << "        " << " % of Total Votes" << endl;
	cout << name1 << setw(12) << votes1 << setw(15) <<	fixed << showpoint << setprecision(2) << percent1<< setw(15)  << endl;
	cout << left << name2 << votes2  << setw(15) <<	fixed << showpoint << setprecision(2)  << percent2  << setw(15)  << endl;
	cout << left << name3 << votes3   << setw(15)<<	fixed << showpoint << setprecision(2)  << percent3 << setw(15)  << endl;
	cout << left << name4  << votes4  << setw(15)<<	fixed << showpoint << setprecision(2)  << percent4 << setw(15)  << endl;
	cout << left << name5 << setw(15) << votes5  <<	fixed << showpoint << setprecision(2) << percent5 << setw(15)   << endl;
	cout << "Total" << setw(15) << totalvotes << endl;
	return 0;
}


and also I can't get it to line up correctly!!
Last edited by abarnett : Nov 6th, 2007 at 8:31 pm.
Reply With Quote  
Join Date: Jul 2005
Posts: 1,104
Reputation: Lerner is a jewel in the rough Lerner is a jewel in the rough Lerner is a jewel in the rough Lerner is a jewel in the rough 
Rep Power: 9
Solved Threads: 144
Lerner Lerner is offline Offline
Veteran Poster

Re: Candidate Program

  #5  
Nov 7th, 2007
The shorter way would be to use arrays, and the concept of parrallel arrays in particular, meaning the information stored as elements with the same index in each of the parallel arrays pertains to the same candidate, so candidate[x] has totalVotes[x] and percent[x], etc. (Actually the concept of an array of objects of user defined type would be even shorter, but that's probably not in the cards at all at this point. You'll get to it later, though).

You're using integer variables and therefore integer math (meaning 10/3 = 3, not 3.33333333) to calculate percent so the percentages won't add up to 100. In fact, the total percentages will probably only add up to 95% given the truncation of decimals in integer math. Probably better to use decimal point percentages or figure out a way to round up/down if you really want integer percentages. (There are several ways to do the rounding). A winner can be readily determined by looping through an array of votes to see which element in the vote array is largest and the index associated with that element will then allow you to print the name of the candidate who won.

Start by declaring an array of string and being able to obtain and display the names of the candidates using the index of the array elements using loops. When you can do that, then declare several other arrays to hold the votes, etc. Then ask for the names and votes and calculate the percentages all within the same loop storing the appropriate information in the appropriate array. Then proceed as described above. Do not try to write it all once! Do it step by step so you know each step works before going to the next step.
Reply With Quote  
Join Date: Nov 2007
Location: Anderson,AL
Posts: 19
Reputation: abarnett is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
abarnett abarnett is offline Offline
Newbie Poster

Re: Candidate Program

  #6  
Nov 7th, 2007
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{

	string candidate[5];
	double votes[5];
	double percent[5];
	string name; // to store candidates name

	cout << "Please enter last name of 5 candidates:";
	cin >> candidate[name];
	cout << endl;

return0;
}

Ok I'm trying But i'm getting an error: "B:\CIS 251 work\Program Files\chap 9 num 7\chap 9 num 7.cpp(16) : error C2677: binary '[' : no global operator defined which takes type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no accepta
"

will still keep working on it though..
Reply With Quote  
Join Date: Sep 2004
Posts: 6,070
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 419
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Candidate Program

  #7  
Nov 8th, 2007
>cin >> candidate[name];
candidate is an array, but name is a string. Arrays can only be indexed by integral values. To get that kind of mapping you need something like...a map:
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. // Key: name, value: # of votes
  10. map<string, int> candidate;
  11.  
  12. // Temporaries for input
  13. string name;
  14. int votes;
  15.  
  16. cout<<"Enter the name and votes of the 5 candidates: ";
  17.  
  18. for ( int i = 0; i < 5; i++ ) {
  19. cin>> name >> votes;
  20. candidate[name] = votes;
  21. }
  22.  
  23. map<string, int>::const_iterator it = candidate.begin();
  24. map<string, int>::const_iterator end = candidate.end();
  25.  
  26. while ( it != end ) {
  27. cout<< it->first <<" has "<< it->second <<" votes\n";
  28. ++it;
  29. }
  30.  
  31. return 0;
  32. }
You also have the benefit of the candidates being sorted by name automagically.

>return0;
That's probably a typo. You need some kind of token separation between return and 0. I prefer a space, but you can also wrap the 0 in parentheses.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Nov 2007
Location: Anderson,AL
Posts: 19
Reputation: abarnett is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
abarnett abarnett is offline Offline
Newbie Poster

Re: Candidate Program

  #8  
Nov 8th, 2007
Originally Posted by Narue View Post
>cin >> candidate[name];

>return0;
That's probably a typo. You need some kind of token separation between return and 0. I prefer a space, but you can also wrap the 0 in parentheses.


yea that was a typo- thanks for the help. But I've got to get it to output the percentage and the winner now. But will have to work on that later...
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 1:19 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC