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.
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:
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.
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
//function prototype
void inputCandidates();
int findWinner(int votes[]);
void printResults(string candidates[], int votes[]);
double calculatePercentage(int votes[], int vote);
constint 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
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.
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.
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.
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace 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);
constint 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
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"
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace 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);
constint 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.