Apr 29, 2013 at 6:28pm UTC
I have a project in my beginner C++ class. we have to design a voter form that brings the info in from a txt file finds the winner of the vote and also finds the percentage of the votes. I have built this and cannot figure out why it is not working. can any one help?
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
string lname [5]= { " "};
double votes[5];
float winingvotes;
float percentagevotes[5];
float total =0;
void readData(ifstream& in, string lname[], double votes[]);
int main()
{
ifstream in;
ifstream out;
in.open("week8.txt");
string lname[5] = {" "};
double votes[5], percentage[5] = {0.0};
readData (in, lname, votes);
}
void readData(ifstream& in, string lname[], double votes[])
{
int i = 0;
while (!in.eof())
{
in >> lname[0] >> votes[0];
cout << lname[i] << " " << votes[i] << endl;
}
}
void percentage()
{
int i;
for (i=0; i<5; i++)
{
percentagevotes[i] = ((100/votes) * votes[i]);
}
}
void calcwinner()
{
int i =0;
winnerpos = 0;
winnervotes = votes[0]
for ( i=1; i<5; i++)
{
if (winnervotes< votes[i] )
{
winnerpos =i;
winnervotes =votes[i];
}
}
}
void showoutput()
{
system("cls");
int i;
cout<< "canidate" << " " << "votes recieved" << " "<< "percentage of votes" << endl;
for (i=0; i<5; i++)
{ cout<< lname[i] << "\t\t" <<votes[i] << "\t\t" << percentagevotes[i] << endl;
cout<< endl;
}
cout<< "winner of the elections is: " << names[winnervotes];
cout<<"\n\n\n";
system ("pause");
}
}
out.open("week8output.txt");
void printdata ()
out<< "canidate" << " " << "votes recieved" << " "<< "percentage of votes" << endl;
for (i=0; i<5; i++)
{ cout<< lname[i] << "\t\t" <<votes[i] << "\t\t" << percentagevotes[i] << endl;
out<< endl;
}
out<< "winner of the elections is: " << names[winnervotes];
out<<"\n\n\n";
}
}
in.close();
out.close();
}
infile information
Johnson 5000
Miller 4000
Duffy 6000
Robinson 2500
Ashtony 1800
Apr 29, 2013 at 10:15pm UTC
You need to wrap your code in code tags, and properly format it before posting.
http://www.cplusplus.com/articles/z13hAqkS/
I just glanced over your code but it looks like this
out.open("week8output.txt" );
isn't inside a funtion. It also looks like the brace right above itsn't needed.
when I run your code I get a bunch of errors like this.
error C2065: 'winnervotes' : undeclared identifier
Which you should be able to handle
Last edited on Apr 29, 2013 at 10:17pm UTC