### I need some serious help with this

Write a program that will ask the user for results of an election. There are 3 candidates that get votes. The program should ask, for each candidate: the name (without spaces) and the number of votes.
Then the program should display the results in tabular format:
name left justified, number of votes right justified, and percentage of votes right justified with 1 decimal,
for each of the three candidates.
At the end, display total votes.
You can assume names are no longer than 8, and the number of votes in less than two thousand total.

No input validation needed.

Example (you should match the format)
Give name: John
Give name: Susan

John 200 20.0%
Susan 50 5.0%

Hints - Follow incremental development with the following suggested increments:

John 200

Susan 50
2. If working, compute total and add to display
Susan 50

Add another column which is the percentage (you may first display just the ratio before converting to fractions)
4. Format the output
closed account (9y8C5Di1)
#include <iostream>
using namespace std;

int main(){

unsigned x = 0,vote[3];
string name[3];

while(x<3){
cout<<"Vote for who?: ";
cin>>name[x];
cout<<"Vote how much?: ";
cin>>vote[x];
cout<<"Thank you for voting for "<<name[x]<<'.'<<endl;
++x;
}

cout<<"Evaluation in progress..."<<endl<<endl;

for(x=0;x<3;++x){

cout<<name[x]<<", "<<vote[x]<<", "<<((float)vote[x]/(((float)vote[0]+(float)vote[1]+(float)vote[2])))*100<<"%"<<endl;

}
return 0;}
thank you very much!
Dude, you need to do cin.getline. Candidates have a first and last name...
closed account (9y8C5Di1)
(The name without spaces), he noted.
Candidate 1: cool guy
Candidate 2: lol guy
Candidate 3: cool guy

cool guy 50 50%
lol guy 10 10%
cool guy 40 40%

cool guy repeats....
closed account (9y8C5Di1)
Yeah, i know.
You should use getline() or a input that scans for only a '\n' if you want spaces in your string,(cin only scans either for the first space or a '\n').
Since he specifically stated that the program werent to take line input,
then there was no need for it, so i excluded it.
Last edited on
@sebelius - if you're going to be posting code to help people, you really should be using code tags (the <> formatting button).
closed account (9y8C5Di1)
Not everyone uses an editor that supports code highlightning.
But why is that important when specifically writing to help someone?
I agree though, in fact you should exploit that feauture regardless of who you are writing to.
@sebelius

 Not everyone uses an editor that supports code highlightning.

AbstractionAnon is talking about code formatting on this webpage, So your code looks like this:

 ``1234567891011121314151617181920212223242526`` ``````#include using namespace std; int main(){ unsigned x = 0,vote[3]; string name[3]; while(x<3){ cout<<"Vote for who?: "; cin>>name[x]; cout<<"Vote how much?: "; cin>>vote[x]; cout<<"Thank you for voting for "<

Notice the line numbers and how it's easier to read.

 `Select your code & press the <> button on the right, under the formatting menu.`

You can also make use of the format options, like quoting, program output, Bold, Italics etc.
closed account (9y8C5Di1)
Oh, thank you, i hadnt noticed that one.
I appreciate all the help. Thank you everyone for your assistance.
Topic archived. No new replies allowed.