•
•
•
•
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
![]() |
•
•
Join Date: Nov 2007
Location: Anderson,AL
Posts: 19
Reputation:
Rep Power: 1
Solved Threads: 0
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
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
>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!".
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.
•
•
•
•
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? •
•
Join Date: Nov 2007
Location: Anderson,AL
Posts: 19
Reputation:
Rep Power: 1
Solved Threads: 0
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!
and also I can't get it to line up correctly!!
#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.
•
•
Join Date: Jul 2005
Posts: 1,104
Reputation:
Rep Power: 9
Solved Threads: 144
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.
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.
•
•
Join Date: Nov 2007
Location: Anderson,AL
Posts: 19
Reputation:
Rep Power: 1
Solved Threads: 0
#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..
>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:
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.
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:
cplusplus Syntax (Toggle Plain Text)
#include <iostream> #include <map> #include <string> using namespace std; int main() { // Key: name, value: # of votes map<string, int> candidate; // Temporaries for input string name; int votes; cout<<"Enter the name and votes of the 5 candidates: "; for ( int i = 0; i < 5; i++ ) { cin>> name >> votes; candidate[name] = votes; } map<string, int>::const_iterator it = candidate.begin(); map<string, int>::const_iterator end = candidate.end(); while ( it != end ) { cout<< it->first <<" has "<< it->second <<" votes\n"; ++it; } return 0; }

>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.
•
•
Join Date: Nov 2007
Location: Anderson,AL
Posts: 19
Reputation:
Rep Power: 1
Solved Threads: 0
•
•
•
•
>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...
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Playing .Wav/MIDI files in a Visual Basic Program (Visual Basic 4 / 5 / 6)
- What's the HARDEST program you've written? (Computer Science and Software Design)
- Cool little Program to disable startup programs (Windows NT / 2000 / XP / 2003)
- Program is shutting down right after program is executed (C++)
- 3d Program (Game Development)
Other Threads in the C++ Forum
- Previous Thread: overloading operator >>
- Next Thread: C++ Address book



Linear Mode