I have to get the total votes and percent of each vote for this program.I cant figure it out!

I have attached input,but this should be output.

Johnson 5000 6.2
Miller 4000 7.75
Duffey 6000 5.16
Robinson 2500 12.4
Ashtony 1800 17.22
Adkins 3500 8.85
Walls 5500 5.63
Bills 2700 11.48
Total Votes: 31000

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

using namespace std; 
void getData(int votes[8],char names[8][20]); 
void printData (int votes[8],char names[8][20],int totalVotes,float per); 
int totalVotes(int votes[8]);
float percent(int totalVotes[8]);

int main() 
{ 
    int votes[8]; 
    char names[8][20];
	int totVotes;
	float per;

	totVotes=totalVotes(votes);
	per=percent(votes);

    getData(votes,names); 
   printData(votes,names,totVotes,per); 

    return 0; 
} 

void getData(int votes[8],char names[8][20]) 
{ 
    int col=0; 
    int row=0; 
    ifstream inputFile; 

    //opens files 
    inputFile.open("input.txt"); 

    for (row = 0; row < 8; row++) 
    { 
        inputFile>>names[row]; 
        inputFile>>votes[row]; 
    } 
    inputFile.close(); 
    cout << "Results processed and saved in output.txt" << endl; 
} 

int totalVotes(int votes[8])

{
	int col=1;
	int sum=0;
	int row=0;
    for(col=0;col<1;col++)
	{
		sum=0;
	for(row=0;row<8;row++)
	sum=sum+votes[col];
	}

return sum;
}


float percent(int totalVotes[8])
{
	int row=0;
	int sum=0;
	{
for (row = 0; row < 8; row++) 
	{
	sum=sum+totalVotes[row];
	}

return sum/8.0;
}


void printData (int votes[8],char names[8][20],int totVotes,float per ) 
{ 
    ofstream outputFile; 
    outputFile.open("output.txt"); 

    int row;
	
    for (row = 0; row < 8; row++) 
    { 
        outputFile<<names[row] << " "; 
        outputFile<<votes[row] <<" ";
		outputFile<<per[row]<<endl;
    }    
    
    outputFile<<"Total Votes:"<<totVotes<<endl;

    outputFile.close(); 
}

Recommended Answers

All 8 Replies

Ooops for got input.

Just a helpful tip while you're on this forum:

In the future, please name your thread something relevant to your topic. "Need C++ help quick", in addition to seeming a little rude, tells us absolutely nothing about what you need help with. I mean, come on... it's a little obvious. You're starting a post in the C/C++ forum, so I wouldn't expect you'd need help rebuilding a carburetor or something....

In the future, please name your thread something relevant to your topic. "Need C++ help quick",

Sorry was kinda in a rush that's why it was so short and blunt..as you can see I even forgot to attach the file.

I didnt mean to sound rude it wasnt my intention,and I apologize, but...

I mean, come on... it's a little obvious. You're starting a post in the C/C++ forum, so I wouldn't expect you'd need help rebuilding a carburetor or something....

To me that semed a little rude to someone new to the board, just asking for a little help.

never rush through coding, it only leads to errors.
Think first, code last.

I have to get the total votes and percent of each vote for this program.I cant figure it out!

There are a couple of syntax errors.

float percent(int totalVotes[8])
{
	int row=0;
	int sum=0;
	//{ // unnecessary mismatched {
for (row = 0; row < 8; row++)
	{
	sum=sum+totalVotes[row];
	}

return sum/8.0;
}

void printData (int votes[8],char names[8][20],int totVotes,float per )
{
    ofstream outputFile;
    outputFile.open("output.txt");

    int row;
    for (row = 0; row < 8; row++)
    {
        outputFile<<names[row] << " ";
        outputFile<<votes[row] <<endl;
		//outputFile<<per[row]<<endl; // per is not an array
    }

    outputFile<<"Total Votes:"<<totVotes<<endl;

    outputFile.close();
}

But let's look at this.

int main()
{
    int votes[8];
    char names[8][20];
	int totVotes;
	float per;

	totVotes=totalVotes(votes);
	per=percent(votes);

    getData(votes,names);
   printData(votes,names,totVotes,per);

    return 0;
}

Calculate the results, then get the data?

for the calculation of % dont divide by 8, as this will distribute the % equally among the 8 candidates....

It should be along the lines of:

Total votes = add the other votes (you dont need to pass it as a parameter)
Each vote % = (the vote result / the total votes) * 100 (cast the fraction to int if you dont want decimals)

To me that semed a little rude to someone new to the board, just asking for a little help.

Not meaning to sound rude at all. I'm just stating the obvious there, and in fact showing you what we're all about. We're here to help, so you don't need to ask for it! :D I'm just making a silly example as to why you don't need to say you need C++ help when you're posting in the C++ forum, really.

I figured it out last about 4am est. Thanks everyone for the help
Sorry for the confusion alc6379.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.