2 Hours Until Assignment Is Due...Help

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.
Last edited on
Try and change this
while (stopPrompt == 'Y' || stopPrompt == 'y')
to this
while ((stopPrompt == 'Y') || (stopPrompt == 'y'))
If the original while is working for you, its a miracle because I can't get any of my whiles to work like that :(

I also believe you have two too many brackets in your main function (I think, I'm a noob).
1 above the first while.
1 below return 0.

Otherwise, I'm much below what your doing here, so that's all I can try to give you. I don't understand much of what you've coded here..
wow, I didn't think there was anything below this skill level. You make me feel better about this disaster of code I have created.

My suggestion, which I wish I would have started earlier. Is to work through every exercise in your textbook. I get more out of that than a weeks worth of lectures. Oh, and YouTube makes for a hell of a summary lesson sometimes.

1 hour 40 minutes and counting, I am sure it is in the getline area...but I cannot figure out how to change it, or what to change it to. My professor has given up the last 2 weeks since he is leaving and we are left out to dry. He is passing out tests that he hasn't even looked at, or could finish himself. I rocked a 43 out of 100 on my lab final, as did 80% of the rest of the class. We hadn't even covered half the information on the test. I was carrying a B+ before that 30% of my grade meteor hit me.

This is the last assignment, the final is tomorrow and I have been working on this for 3 days. According to him, if I can write this program I can pass the final. Things aren't looking hopeful...

At least I am not the only one, most of the class is in the same boat. We haven't been able to contact the one guy that got this running so maybe we will get partial credit for effort...

You will notice that this "oh so important" assignment is due at 10pm the night before the 8am final exam, which counts as 30% of your grade as well, just like lab. That leaves a lot of time to help us correct our mistakes! Well, that's just how he rolls.
Last edited on
Im pretty sure those parameters shouldnt be in main lol

either int main(){}

or int main( int argc, char** argv ){}

EDIT 1: When calling functions you have to fill in the parameters. Your sumVotes function should be taking 2 parameters, and int winner should be taking 3.

EDIT 2: You dont have to do this but you should get used to using the correct loops. The first while loop should be an 'if' loop. The second while loop should be for(; index < 6; index++)
and remove index++ from the bottom of that loop.

EDIT 3: LOL what are they teaching you? Im 14 and self and taught and this is laughable( no offense, im not that good but idk how errors like these could be made in college courses ).

More findings
- in your prototype sumVotes take 2 arguments, but your defintion takes 3. I dont think you need the third one in the definition, just add int percentVote[5]; to the top of the body of the function.

Ill look for more and keep editing this post
Last edited on
int main(string canName[5], int& totalVotes, int numOfVotes[5]) //should these parameters be here?

No. It should just be int main(). Put the rest of it in main.

1
2
int sumVotes(); //is this proper call for 
int winner();   //is this proper call for function? 


No. I wonder how this compiles. Anyway, you have to pass the variables that your declarations call for to the functions. This will prove to be slightly challenging due to passing an array. You could always make the string, int&, and array global.
Last edited on
ok, thanks for your advice. Don't worry about editing further, I see now I am screwed.

I will plan to re take this class...The point was to learn something, not just pass.

Here's the correct code, which I cannot take credit for.

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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;


char canNames[5][20];
float votes[5];
float totalVotes = 0;
int winnerSpot;  
float winnerVotes;
float votePercent[5];

void promptUser()    
{
int i;
for (i=0;i<5;i++)
{
cout << "Enter The Last Name of Candidate With No Spaces" << endl;
cin >> canNames[i];  
cout << "Enter The Number Of Votes Received " << endl;
cin >> votes[i];  
totalVotes += votes[i];  
}
}

void winner()
{
int i;
winnerSpot = 0;
winnerVotes = votes[0];

for (i=1;i<5;i++)
{
if ( winnerVotes < votes[i] )
{
winnerSpot = i;
winnerVotes = votes[i];
}
}
}

void sumVotes()
{
int i;
for (i=0; i<5 ; i++)
{
votePercent[i] = ((100/totalVotes) * votes[i]);
}
}

void show_output()
{
cout << setprecision(2) << fixed << showpoint << endl;
int i;
cout << "Candidate" << "  " << "Votes Received" << "   " << "Percentage Votes." << endl;
for ( i = 0; i < 5; i++ )
{
cout << canNames[i] << "\t\t" << votes[i] << "\t\t" << votePercent[i] << endl;
cout << endl;
}
cout << "Winner of the election is: " << canNames[winnerSpot]<<endl;
}

int main()
{
promptUser();
winner();
sumVotes();
show_output();
    system("pause");
    
    
    return 0;
}
I suggest reading some c++ books on the side too. My first book was Ivor Hortons beginning c++ but i dont really reccomend it :/
Last edited on
Those are not for beginners. For beginners there are others. For me I started with C++ Primer.
Topic archived. No new replies allowed.