>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!".
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
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.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
>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:
#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;
}
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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Please do not resurrect threads that are years old. By doing so you run the risk of confusing current posters. If you have any questions please ask. You are welcome to start your own threads. Have a look at forum rules .
Thread Locked.
__avd
Posting Genius (adatapost)
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241