Help involving arrays and strings

Pages: 12
Hello.

I was wondering if perhaps some assistance could be given to me. The assignment is as follows:

Write a program that allows the user to enter the last names of five canadiates in a local election and the number of votes received by each candidate. The program should then output each candidate's name, the number of votes received, and the percentage of the total votes received by each candidate.

When I submitted it to my instructor, I was informed that there are only meant to be user-defined functions in the main function. I was hoping someone could guide me on how to do that without destroying my code, which seems like it is working properly.

Here it is:

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <string>

using namespace std;

int findWinner(int votes[]);
void printResults(string candidates[], int votes[]);
double calculatePercentage(int votes[], int vote);

const int NUMBER_OF_CANDIDATES = 5;

int main()
{

	string candidates[NUMBER_OF_CANDIDATES];
	int votes[NUMBER_OF_CANDIDATES];

	cout << "Please input the 5 candidates followed by the votes they recieved i.e. Smith 5000: ";
	for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) 
	{
		cin >> candidates[i] >> votes[i];
	}
	printResults(candidates, votes);
	cout << "And the winner is: " << candidates[findWinner(votes)] << endl;
	return 0;
}

double calculatePercentage(int votes[], int vote)
{
	int sumOfVotes = 0;

	for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) 
	{
		sumOfVotes += votes[i];
	}

	double percentage = static_cast<double>(vote) / sumOfVotes;

	return percentage * 100;
}


void printResults(string candidates[], int votes[])
{
	cout << "Name:" << setw(15) << "Votes:" << setw(15) << "Percentage:" << endl;
	for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) 
	{
		cout << candidates[i] << setw(15) << votes[i] << setw(15);
		int percentage = calculatePercentage(votes, votes[i]);
		cout << percentage << "%" << endl;

	}
}

int findWinner(int votes[])
{
	int winner = 0;
	for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) 
	{
		if (votes[i] > winner)
			winner = i;
	}
	return winner;
}


Thank you. I greatly appreciate your help.
The simpliest would be to create a new function, move the content of main() to it, and call it from main().
The thing is, is that I am worried that if I mess with it I will mess up the code's working. But, if you are saying it just make a new function and it won't create any logical errors, thanks.
I was informed that there are only meant to be user-defined functions in the main function.

Personally, I see nothing wrong with your main. It's clear and to the point.
However, that's not going to satisfy your instructor.

coder777 wrote:
create a new function, move the content of main() to it

IMO, moving all of main into another function doesn't really accomplish anything and just creates an unnecessary function.

I think the point your instructor is trying to get across is to divide your program into discrete functions. You're really doing two things in your program. Input and output. So I would have a function for each of those actions.

I would move lines 18-22 to a function called input_candidates (candidates, votes).
I would leave line 23 as is and move line 24 to after line 52.

Line 60: Your if condition is wrong. You're comparing the number of votes in the array to the array index of the winner. What you want is:
 
  if (votes[i] > votes[winner])


Last edited on
I was informed that there are only meant to be user-defined functions in the main function.

Personally, I see nothing wrong with your main. It's clear and to the point.
However, that's not going to satisfy your instructor.

coder777 wrote:
create a new function, move the content of main() to it

IMO, moving all of main into another function doesn't really accomplish anything and just creates an unnecessary function.

I think the point your instructor is trying to get across is to divide your program into discrete functions. You're really doing two things in your program. Input and output. So I would have a function for each of those actions.

I would move lines 18-22 to a function called input_candidates (candidates, votes).
I would leave line 23 as is and move line 24 to after line 52.

Line 60: Your if condition is wrong. You're comparing the number of votes in the array to the array index of the winner. What you want is:

if (votes[i] > votes[winner])


Hello. Thanks for the input. I take it the new user-function from lines 18-22 will be a void function? Furthermore, thanks for correcting my condition.

Also, I am sorry if I seem to be asking silly question. I was doing fine until we got to user-defined functions, and am now completely lost. So all help is greatly appreciated.
A void function is perfectly fine for lines 18-22 since there is no need to return anything.

New code gave me a really bad logical error. Please help!
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

//function prototype
void inputCandidates();
int findWinner(int votes[]);
void printResults(string candidates[], int votes[]);
double calculatePercentage(int votes[], int vote);

const int Number_Of_Candidates = 5;

int main()
{

	string candidates[Number_Of_Candidates];
	int votes[Number_Of_Candidates];


	printResults(candidates, votes);
	

	system("pause");
	return 0;
}

//inputCandidates takes input of candidates names
//and the number of votes they have received.
void inputCandidates()
{
	string candidates[Number_Of_Candidates];
	int votes[Number_Of_Candidates];

	cout << "Please input the 5 candidates followed by the votes they recieved i.e. Smith 5000: ";
	for (int i = 0; i < Number_Of_Candidates; i++)
	{
		cin >> candidates[i] >> votes[i];
	}
}
//end of inputCandidate

//calculatePercentage returns the number of votes in the array as percentages
//for respective candidates
double calculatePercentage(int votes[], int vote)
{
	int sumOfVotes = 0;

	for (int i = 0; i < Number_Of_Candidates; i++) 
	{
		sumOfVotes += votes[i];
	}

	double percentage = static_cast<double>(vote) / sumOfVotes;

	return percentage * 100;
}
//end of calculatePercentage

//print results of the array in desired format, and then displays the winning candidate.
void printResults(string candidates[], int votes[])
{
	cout << "Name:" << setw(15) << "Votes:" << setw(15) << "Percentage:" << endl;
	for (int i = 0; i < Number_Of_Candidates; i++) 
	{
		cout << candidates[i] << setw(15) << votes[i] << setw(15);
		int percentage = calculatePercentage(votes, votes[i]);
		cout << percentage << "%" << endl;

	}
	cout << "And the winner is: " << candidates[findWinner(votes)] << endl;
}
//end of printResults

//analyzes the array of candidates and corresponding vote values, and
//then determines winner by which candidate had the highest vote value
int findWinner(int votes[])
{
	int winner = 0;
	for (int i = 0; i < Number_Of_Candidates; i++) 
	{
		if (votes[i] > votes[winner])
			winner = i;
	}
	return winner;
}
//end of findWinner
Please any one. I have been working on this all night, and still can't get the thing to work properly.
You need to call inputCandidates(...) before printResults(...).

inputCandidates(...) requires the same parameters as printResults(...). I.e. remove the local variables on line 33/34 and use the parameter instead.
So what you are saying is that I want to put

1
2
3
void inputCandidates(string&, int&);
...
void inputCandidates(string& candidates, int& votes)


Is that what you are saying? IDK...Again, these sections have me completely lost, so this is all ancient latin to me.

I also wish to point out that I am an Mech. E major, so I realize that many of questions are probably a little silly, but I legitimately have no clue what is going on. This is completely out of my comfort zone.

Anyway thanks for your input. I just want to make sure that I understand it correctly.
So what you are saying is that I want to put ...

No. The arguments to inputCandidates should be the same as printResults.
You want to pass both arrays.

Don't worry about your questions being silly. They're not.
They're run of the mill beginners questions.
We see them all the time.
We were all beginners once.




But those are the same arguements...Geez...Now I am even more confused!

Look:

1
2
void inputCandidates(string&, int&);
void printResults(string candidates[], int votes[]);


I have no idea what you mean, AbstractionAnon. I am sorry, but I really don't.

Please, if someone could provide me an example, because I cannot visualize what any one is trying to convey to me.

I am sorry, but I now more lost than ever!
No, those are not the same arguments.

The & and the [] have very different meanings.
Line 1 passes a single string by reference and a single int by reference.
Line 2 passes an array of strings and an array of ints.

If inputCandidates were going to input only a single candidate name and vote count, then line 1 would be appropriate.

However, your inputCandidates function has a for loop which inputs all the candidates and votes. Therefore, you want to pass the both arrays. So we want the function signature matching line 2. This is no different than printResults which also references both arrays.
Last edited on
So you mean:

1
2
void inputCandidates(string&  candidates[], int& votes[]);
void printResults(string& candidates[], int& votes[]);


Is that what you mean?
No need for the &. How you had it for printResults is just fine for inputCandidates.
Still getting the logical error.

My code:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

//function prototype
void inputCandidates(string candidates[], int votes[]);
int findWinner(string candidates[], int votes[]);
void printResults(string candidates[], int votes[]);
double calculatePercentage(int votes[], int vote);

const int Number_Of_Candidates = 5;

int main()
{

	string candidates[Number_Of_Candidates];
	int votes[Number_Of_Candidates];


	printResults(candidates, votes);


	system("pause");
	return 0;
}

//inputCandidates takes input of candidates names
//and the number of votes they have received.
void inputCandidates(string candidates[], int votes[])
{
	

	cout << "Please input the 5 candidates followed by the votes they recieved i.e. Smith 5000: ";
	for (int i = 0; i < Number_Of_Candidates; i++)
	{
		cin >> candidates[i] >> votes[i];
	}
}
//end of inputCandidate

//calculatePercentage returns the number of votes in the array as percentages
//for respective candidates
double calculatePercentage(int votes[], int vote)
{
	int sumOfVotes = 0;

	for (int i = 0; i < Number_Of_Candidates; i++)
	{
		sumOfVotes += votes[i];
	}

	double percentage = static_cast<double>(vote) / sumOfVotes;

	return percentage * 100;
}
//end of calculatePercentage

//print results of the array in desired format, and then displays the winning candidate.
void printResults(string candidates[], int votes[])
{
	cout << "Name:" << setw(15) << "Votes:" << setw(15) << "Percentage:" << endl;
	for (int i = 0; i < Number_Of_Candidates; i++)
	{
		cout << candidates[i] << setw(15) << votes[i] << setw(15);
		int percentage = calculatePercentage(votes, votes[i]);
		cout << percentage << "%" << endl;

	}
	cout << "And the winner is: " << candidates[findWinner(votes)] << endl;
}
//end of printResults

//analyzes the array of candidates and corresponding vote values, and
//then determines winner by which candidate had the highest vote value
int findWinner(int votes[])
{
	int winner = 0;
	for (int i = 0; i < Number_Of_Candidates; i++)
	{
		if (votes[i] > votes[winner])
			winner = i;
	}
	return winner;
}
//end of findWinner 


What is going on...
Last edited on
Line 21: You're not calling inputCandidates().
You mean like this in the main function?

1
2
	inputCandidates(candidates, votes);
	printResults(candidates, votes);
Last edited on
Yes.
Thing is then I get a two build errors. As follows:

"Warning 1 warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data e:\compsci_1\hw_assignments\candidate analysis\candidate analysis\candidate analysis.cpp 73 1 Candidate analysis"

and

"Error 2 error C2660: 'findWinner' : function does not take 1 arguments e:\compsci_1\hw_assignments\candidate analysis\candidate analysis\candidate analysis.cpp 77 1 Candidate analysis"

Here is the code:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

//function prototype
void inputCandidates(string candidates[], int votes[]);
int findWinner(string candidates[], int votes[]);
void printResults(string candidates[], int votes[]);
double calculatePercentage(int votes[], int vote);

const int Number_Of_Candidates = 5;

int main()
{

	string candidates[Number_Of_Candidates];
	int votes[Number_Of_Candidates];

	inputCandidates(candidates, votes);
	printResults(candidates, votes);


	system("pause");
	return 0;
}

//inputCandidates takes input of candidates names
//and the number of votes they have received.
void inputCandidates(string candidates[], int votes[])
{


	cout << "Please input the 5 candidates followed by the votes they recieved i.e. Smith 5000: ";
	for (int i = 0; i < Number_Of_Candidates; i++)
	{
		cin >> candidates[i] >> votes[i];
	}
}
//end of inputCandidate

//calculatePercentage returns the number of votes in the array as percentages
//for respective candidates
double calculatePercentage(int votes[], int vote)
{
	int sumOfVotes = 0;

	for (int i = 0; i < Number_Of_Candidates; i++)
	{
		sumOfVotes += votes[i];
	}

	double percentage = static_cast<double>(vote) / sumOfVotes;

	return percentage * 100;
}
//end of calculatePercentage

//print results of the array in desired format, and then displays the winning candidate.
void printResults(string candidates[], int votes[])
{
	cout << "Name:" << setw(15) << "Votes:" << setw(15) << "Percentage:" << endl;
	for (int i = 0; i < Number_Of_Candidates; i++)
	{
		cout << candidates[i] << setw(15) << votes[i] << setw(15);
		int percentage = calculatePercentage(votes, votes[i]);
		cout << percentage << "%" << endl;

	}
	cout << "And the winner is: " << candidates[findWinner(votes)] << endl;
}
//end of printResults

//analyzes the array of candidates and corresponding vote values, and
//then determines winner by which candidate had the highest vote value
int findWinner(int votes[])
{
	int winner = 0;
	for (int i = 0; i < Number_Of_Candidates; i++)
	{
		if (votes[i] > votes[winner])
			winner = i;
	}
	return winner;
}
//end of findWinner  


Thank you, AbstractionAnon. I apologize for any trouble I am causing.
Pages: 12