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 :)

>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!".

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'? ;)

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!!

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.

#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:

#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.

>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...

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.

This article has been dead for over six months. Start a new discussion instead.