voter program

Apr 27, 2013 at 11:59pm
In my class we were suposed to write a program from the book that inputed the names and votes and the program is suposed to spit out who won the vote. I did this then while i was reading the assignment from the instructor I noticed he wanted the information inputed from a TXT file instead of manually inputing it. I cannot seem to get it to work can anybody help me with this?

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

using namespace std;


#define N 5

char names[N][20];
float votes[N];
float totalVotes = 0;
int winnerPos;
float winnerVotes;
float percentageVotes[N];
float total = 0;
ofstream out;
ifstream in;



void get_input()
{
int i;
for (i=0;i<N;i++)
in.open("week8input.txt");

char char1 = char();
//int i = 0;

system("cls");
cout << endl;

cout << fixed << setprecision(2);


while(!in.eof())
{
in >> names[i] >> votes[i];
cout << setw(10) << left<< setw(15) << left << names[i] << setw(15) << left << votes[i];
cout << endl;
++i;
}

}

void calculate_winner()
{
int i;
winnerPos = 0;
winnerVotes = votes[0];

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

void calculate_Percentage_Of_Every_Candidate()
{
int i;
for (i=0; i<N ; i++)
{
percentageVotes[i] = ((100/totalVotes) * votes[i]);
}
}

void show_output()

{
system("cls");
int i;

cout << "Candidate" << " " << "Votes Received" << " " << "Percentage Votes." << endl;
for ( i = 0; i < N; i++ )
{
cout << names[i] << "\t\t" << votes[i] << "\t\t" << percentageVotes[i] << endl;
cout << endl;
cout << "Totoal Votes: "<< (totalVotes+ votes[i])<< endl;
}
cout << "Winner of the election: " << names[winnerPos];
cout<< "\n\n\n";
//system("pause");
}


int main()
{
get_input();
calculate_winner();
calculate_Percentage_Of_Every_Candidate();
show_output();
return 0;
system("pause");




out.open("week8out.txt");
while (out)
{
out << names << "\t\t" << votes << "\t\t" << percentageVotes << endl;
out << endl;
out << "Winner of the election: " << names[winnerPos];
system("pause");

}
while(out.eof())
out.close();
}




here is the txt file

Johnson 5000
Miller 4000
Duffy 6000
Robinson 2500
Ashtony 1800
Apr 28, 2013 at 2:15am
As your code as shown now, it is unreadable because the indentation was not preserved. If you put [code] before your code and [/code] after it with reasonable indentation, it can read. Or select your code and press on the <> button in Format: list. You are more likely to get help if you do that.
Last edited on Apr 28, 2013 at 3:11am
Apr 28, 2013 at 4:25am
I am not sure what you mean by this.
Topic archived. No new replies allowed.