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 votes: 200
Give name: Adam
Give votes: 750
Give name: Susan
Give votes: 50

John 200 20.0%
Adam 750 75.0%
Susan 50 5.0%

Total votes 1000
Hints - Follow incremental development with the following suggested increments:
1. Write program to read three names and three votes and just display name and votes

John 200

Adam 750
Susan 50
2. If working, compute total and add to display
John 200 Adam 750
Susan 50

Total votes 1000.
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
Votes: 50
Candidate 2: lol guy
Votes: 10
Candidate 3: cool guy
Votes 40

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#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;} 


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.