How to approach this problem?

Write your question here.

1
2
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 :)
Any idea anyone?
Beginner:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <iostream>
using namespace 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;
}


Advanced:
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
27
28
29
30
31
32
#include <string>
#include <iostream>
#include <vector>
using namespace std;

class candidate {
	string name;
	int votes;
	
public:
	friend istream &operator >> (istream &iss, candidate &c) {
		iss >> c.name >> c.votes;
		return iss;
	}
	
	friend ostream &operator << (ostream &oss, candidate &c) {
		oss << c.name << " " << c.votes;
		return oss;
	}
	
	int n_votes() const { return votes; }
};


int main() {
	vector<candidate> runners(5);	
	
	for (auto &can: runners)
		cin >> can;
		
	return 0;
}
Last edited on
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 :)
Have you atleast learned about vectors?
Last edited on
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
Ok answer is updated with version for beginners which uses arrays and for-loops
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void lastnameoffivecandiadtes (ifstream &in, ofstream &out,string candidates[], int size, int vote[])
{
	//while file is open
	while (in)
	{
	 for(int i=0;i<size;i++)
	 {
		 in>>candidates[i]>>vote[i];
	 
	 
	 
	 
	 }
	
	
	
	
	}


}
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];
		}
	}
}
Last edited on
Alright I got that. Just out of curiousity, I have been using while statements on a lot of my previous assignments. Can I not use while if possible :(
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
Alright smac89. I have come up with this following code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
Anyone willing to help?
Topic archived. No new replies allowed.