ok, So I try not to bother with class questions, but I am in a pinch.
This is the assignment...
Due Date: Thursday, July 28, 2011 at 10:00 PM Central (Blackboard)
This assignment introduces you to C++ arrays, strings and void functions:
USM student government is getting ready to conduct its annual elections and needs your help. The organizers ask you to write a program that will allow them to enter the last names of the five contesting candidates, and the number of votes received by each candidate. The program should then outputs the name of each candidate, number of votes, and the percentage of the total votes received by the candidate. Then your program should declare the winner based on the results.
Use at least three functions. 1) A void function that simply prompts the user for the five names of the candidates (you may call it promptUser); 2) a value-returning function to sum the votes (you may call it sumVotes); and 3) another value-returning function to determine the winner (call it Winner). Finally, you must use at least two arrays. A string array to store the names of the candidates, and an integer array to store the votes.
A simple look-and-feel for the sample output is provided below.
Candidate Votes Received % of Total Votes
==================================================
Gang 5000 25.91
Bazor 4000 20.73
Mandel 6000 31.09
Wills 2500 12.95
Taquino 1800 9.33
Total 19300
*And the winner of the election is Mandel! Congratulations!!
Here's 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#include <iostream>
#include <string>
using namespace std;
//prototype functions
void promptUser(); //prompts the user for the five names of the candidates
int sumVotes(int numOfVotes[5],int percentVote[5]); //totals all the votes
int winner(string canName[5],int numOfVotes[5],int percentVote[5]); //finds and declares winner
int main(string canName[5], int& totalVotes, int numOfVotes[5]) //should these parameters be here?
{
//define variables
char stopPrompt;
int index;
//starts the program
cout <<"Would you like to start the program? Press Y or N: ";
cin >> stopPrompt;
{
while (stopPrompt == 'Y' || stopPrompt == 'y')
{
//starts the actual work
while (index <= 5)
{
index = 0;
promptUser(); //is this proper call for void function?
//THIS IS WHERE IT CRASHES
getline (cin,canName[5]); //is this syntax correct?
cout <<"Please enter the number of votes received:";
cin >> numOfVotes[index];
index++;
}
//formula to calculate total votes
totalVotes = numOfVotes[0]+numOfVotes[1]+numOfVotes[2]+numOfVotes[3]+numOfVotes[4];
int& totalVotes = totalVotes; //is this the right order?
int sumVotes(); //is this proper call for function?
int winner(); //is this proper call for function?
}
cout << "Now quitting..." << endl;
system("pause");
return 0;
}
}
//-------------------------------------------------------------------------------------
// first user defined function
void promptUser() //instructions specifically requested only the input of user names
{
cout <<"Please enter the names of the candidates on seperate lines." <<endl;
}
//-------------------------------------------------------------------------------------
//second user defined function
int sumVotes(int& totalVotes, int numOfVotes[5],int percentVote[5])
{
percentVote[0] = (totalVotes % numOfVotes[0]);
percentVote[1] = (totalVotes % numOfVotes[1]);
percentVote[2] = (totalVotes % numOfVotes[2]);
percentVote[3] = (totalVotes % numOfVotes[3]);
percentVote[4] = (totalVotes % numOfVotes[4]);
return percentVote[5]; //is this the right way to return this?
}
//-------------------------------------------------------------------------------------
//third user defined function
int winner(string canName[5],int numOfVotes[5],int percentVote[5])
{
string winner;
int maxIndex;
int index;
//outputs the stored data
cout <<" Candidate" << "Votes Received" << "% of total votes"<<endl;
cout <<"===================================================="<<endl;
cout <<"===================================================="<<endl;
cout << canName[0] << numOfVotes[0] << percentVote[1] <<endl;
cout << canName[1] << numOfVotes[1] << percentVote[2] <<endl;
cout << canName[2] << numOfVotes[2] << percentVote[3] <<endl;
cout << canName[3] << numOfVotes[3] << percentVote[4] <<endl;
cout << canName[4] << numOfVotes[4] << percentVote[5] <<endl;
//compares the votes
maxIndex = 0;
for (index=1; index <= 5; index++)
if(numOfVotes[maxIndex] < numOfVotes[index])
maxIndex = index;
winner = numOfVotes[maxIndex];
//declares the winner
cout <<"The winner of the election is "<<winner<<".";
}
|
I know there's multiple problems, this is my first programming class. Could someone help me clean this up? I really did try to finish it with only my own research, but time is running out!
Thanks.