How to create ballot function

For my latest project, I am supposed to create a program that tallies up votes for both the position of "headmaster", and then for each of 10 amendments. I was able to get through the headmaster part, but the amendment part is giving me trouble. For the headmaster, all I had to do was input vote counts and candidate names, but for the amendment side, I am supposed to count up "yes" and "no" votes. The part that I cant figure out is how to "combine" both results. For headmaster, it was either candidate 1 or 2. For amendments, I tried to model it the same way, but the output is messed up.
Here is my code so far: any tips/corrections are greatly appreciated.

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
string candidates[5];
int votes[5];
int g, f, u;
double sum = 0;

cout << "Enter the first candidate. \n";

for ( g = 0 ; g < 5; g++ )
{
cin >> candidates[g];

switch (g)
{
case 0:
cout << "Enter the second candidate: \n";
break;
case 1:
cout << "Enter the third candidate: \n";
break;
case 2:
cout << "Enter the fourth candidate: \n";
break;
case 3:
cout << "Enter the fifth candidate: \n";
break;
}
}

cout << "\nEnter votes for " << candidates[0] << ":\n";

for ( f = 0 ; f < 5; f++ )
{

cin >> votes[f];

switch (f)
{
case 0:
cout << "\nEnter votes for " << candidates[1] << ":\n";
break;
case 1:
cout << "\nEnter votes for " << candidates[2] << ":\n";
break;
case 2:
cout << "\nEnter votes for " << candidates[3] << ":\n";
break;
case 3:
cout << "\nEnter votes for " << candidates[4] << ":\n";
break;
case 4:
cout << "Enter yes votes for amendment 1 (out of 4): \n";
break;
case 5:
cout << "Enter yes votes for amendment 2 (out of 4): \n";
break;
case 6:
cout << "Enter yes votes for amendment 3 (out of 4): \n";
break;
case 7:
cout << "Enter yes votes for amendment 4 (out of 4): \n";
break;
case 8:
cout << "Enter yes votes for amendment 5 (out of 4): \n";
break;
case 9:
cout << "Enter yes votes for amendment 6 (out of 4): \n";
break;
case 10:
cout << "Enter yes votes for amendment 7 (out of 4): \n";
break;
case 11:
cout << "Enter yes votes for amendment 8 (out of 4): \n";
break;
case 12:
cout << "Enter yes votes for amendment 9 (out of 4): \n";
break;
case 13:
cout << "Enter yes votes for amendment 10 (out of 4): \n";
break;
}
}

for (u = 0; u < f; u++)
{
sum = sum + votes[u];
}
bool y, n;
string boolValue;

cout << "Would you like a copy of the results in a .txt file? (y/n) ";
cin >> boolValue;

if (boolValue == "y")
{ y = true;

char fileName[20];
ifstream infile;
ofstream outfile;

outfile.open(fileName);

cout << "Please enter a name for your file to write the good ballots to (including path and extension): ";
cin >> fileName;

outfile << "\nCandidate Votes Received % of Total Votes\n";
outfile << left << setw(20) << candidates[0] << setw(20) << left << votes[0] << fixed << setprecision(2) << (votes[0]/sum)*100 << "\n";
outfile << left << setw(20) << candidates[1] << setw(20) << left << votes[1] << fixed << setprecision(2) << (votes[1]/sum)*100 << "\n";
outfile << left << setw(20) << candidates[2] << setw(20) << left << votes[2] << fixed << setprecision(2) << (votes[2]/sum)*100 << "\n";
outfile << left << setw(20) << candidates[3] << setw(20) << left << votes[3] << fixed << setprecision(2) << (votes[3]/sum)*100 << "\n";
outfile << left << setw(20) << candidates[4] << setw(20) << left << votes[4] << fixed << setprecision(2) << (votes[4]/sum)*100 << "\n";

infile.close();
outfile.close();
}
else if (boolValue == "n")
{ n = false;
cout << "\nCandidate Votes Received % of Total Votes\n";
cout << left << setw(20) << candidates[0] << setw(20) << left << votes[0] << fixed << setprecision(2) << (votes[0]/sum)*100 << "\n";
cout << left << setw(20) << candidates[1] << setw(20) << left << votes[1] << fixed << setprecision(2) << (votes[1]/sum)*100 << "\n";
cout << left << setw(20) << candidates[2] << setw(20) << left << votes[2] << fixed << setprecision(2) << (votes[2]/sum)*100 << "\n";
cout << left << setw(20) << candidates[3] << setw(20) << left << votes[3] << fixed << setprecision(2) << (votes[3]/sum)*100 << "\n";
cout << left << setw(20) << candidates[4] << setw(20) << left << votes[4] << fixed << setprecision(2) << (votes[4]/sum)*100 << "\n";
}

return 0;
}
P.S. I am in sort of a time crunch, and am not fluent in C++, so glaring errors may abound.
Line 45: You're trying to enter votes for amendments inside your loop for candidates. You're never going to get to branches higher than 4.

Line 48,51,54,57: You're displaying the wrong candidate's name. If g==0, you're displaying candidate[1]. You should be displaying candidate[0]. Hint: You can use g as a subscript and don't need a switch statement.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Topic archived. No new replies allowed.