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