Writea program that allows the user to enter the last names of gfive canidates. Number of votes received by each candidate. The program should output each candidates name, number of votes received and the percentage of the total votes received by the canidtate.
The sample output would be
Candidate Votes percent of total votes
Johson 5000 25.92
miller 4000 20.73
Duffy 6000 31.09
robsinson 2500 12.95
ashtony 1800 9.33
Total 19300
In conclusion, I'm looking forward for a hint of how to start this program. I just cant determine how to do this. I'm thinking of using a parallel array of candidate name and candidate vote; however I get stuck on the part wondering how percent of total votes will be displayed on screen in this format. I would need the total amount of votes first to determind the percentage :( So, as always :D how would I approach this problem :)
#include <string>
#include <iostream>
usingnamespace std;
int main() {
string candidates[5];
int votes[5], total = 0;
for (int t = 0; t < 5; ++t) {
cin >> candidates[t] >> votes[t];
total += votes[t];
}
// ... rest is up to you
return 0;
}
Thanks smac! Did I forget to mention I'm just a beginner :D I have not learned about classes nor pointers. I'm currently working from the chaper of arrays :)
No smack :) so far in my book I have learned about string, arrays, user defined functions and other basics. Not up to vectors at all nor pointers. This assignment was posted within the array section if my textbook. Also, this ws how the output is suppose to look like. It's not an Input file :) so I got an idea maybe getting the entire data feom the user and then printing it out in that format to the output files might be wrong, but looking for your opinion too
I was hoping for you not to tell me the answer :D :D or give me a similar code :( but that's all fine. I wanted to give it a try myself before looking at that. Anyway smac, the data will be entered in the input file.
I have made my code like this. I'm stuck on the while part? I know using while file is open is not a correct approach, but what can I add in there. I was thinking in>>candidates>>vote
As I see no reason to use a while loop, I replaced the while with an if statement which looks like:
1 2 3 4 5 6 7 8 9 10
void lastnameoffivecandiadtes (ifstream &in, ofstream &out, string candidates[], int size, int vote[])
{
//if file is open
if (in.is_open())
{
for(int i = 0; i < size; i++) {
in >> candidates[i] >> vote[i];
}
}
}
This really depends on the programmer, you use what you need to use. I can only give advice so you have to post the relevant code in order for me to help
void lastnameoffivecandiadtes (ifstream &in, ofstream &out,string candidates[], int size, int vote[])
{
candidates[size];
vote[size];
while (in)
{
for(int i=0;i<size;i++)
{
in>>candidates[i]>>vote[i];
out<<candidates[i]<<" " <<vote[i];
out<<endl;
}
}
}
I'm going to use the while method since i'm accustomed to that. If its recommended for me to change it then please tell me. Meanwhile, when i'm running the program, this is my input
lewis 45
ray 23
alba 23
rayd 25
stewar 23
and I get an output of this, which is getting an extra dose of identical information. I'm really awful in using while functions when making files, so please give me an explanation of what my code is doing.
lewis 45
ray 23
alba 23
rayd 25
stewar 23
lewis 45
ray 23
alba 23
rayd 25
stewar 23