Hello again! In a battle against my own insanity, I decided to do some extra stuff for my final program in my C++ class. Probably got a bit carried away. Here's the code:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
usingnamespace 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;
}
}
system("cls");
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 (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();
}
elseif (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;
}
here's the problem(s):
first, the outfile isn't creating a "save" file. I have no idea where its going. Is it possible to use outfile only, without infile? the only reason infile is in there right now is for troubleshooting reasons.
second, is there some way to code a function that will return the largest value of the array? Reason I ask, is because the program asks for the five candidates and votes, and after the display of the five candidates and so forth, there is supposed to be "The winner of the election is *name*", and I'm not entirely sure how that could return the name based off the entered values.
Did you check where the program executable is? That is where my files have been saved when I create them. As for checking for the greatest value, I do not think there is such a function. You will probably have to check manually.
char fileName[20];
ifstream infile;
ofstream outfile;
cout << "Please enter a name for your file (including path and extension): ";
cin >> fileName;
outfile.open(fileName);
and it works! Aww yea.
Still having trouble with returning the largest value on the array though.
To do it yourself, the algorithm goes something like this:
1 2 3 4 5
// pseudo-code
highest_val_so_far = value-of-first-element;
for_each( element 2..N in list )
if( element is greater than highest_val_so_far )
highest_val_so_far = element;
And the way I'd do it is (if you're allowed to use STL algorithms):