Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Below are the instructions that were given. Any help would be appreciated. Write

ID: 3642031 • Letter: B

Question

Below are the instructions that were given. Any help would be appreciated.

Write a program that reads file results.txt to determine the winner of an election.
There are 5 candidates.
The output is shown above

The first line in the text file is a "Header".

The text file format is

Name,     Votes

THERE ARE NO FUNCTIONS IN THIS PROGRAM.

Program details:

1. Declare 5 element string array for names.

   Declare 5 element integer array for Votes

Declare 5 element array for totalVotes

Declare 5 element float array for percentage

Other variables as needed.

2. open the file (You should always terminate the program with a non-zero return code if the file does not open successfully)

3. index=0;

getline(filename, name[index],',');      //pre-read

While not eof

{

filename>> votes[index];

accumulate total

increment index

getline(filename, name[index],',';    //post-read

}

For loop 0 to 5

Calculate percentage

Determine the maximum

Print each line

After the loop print the winner

This is whats in the txt file

Explanation / Answer

I hope this code helps

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int main()
{
    ifstream myReadFile;
    myReadFile.open("C:\Data\data.txt");
    string Names[5];
    int Votes[5];
    float percentages[5];

    if(!myReadFile)
    {
        // file couldn't be opened
        cerr << "Error: file could not be opened" << endl;
        return 1;
    }
    else
    {
        int index = -1, maxIndex = 0;
        string output;
        int TotalVotes = 0;
        while ( !myReadFile.eof() )
        {
            getline(myReadFile, output);

            if(index != -1)
            {
                string name = output.substr(0,output.find(','));
                int nSeparatorIndex = output.find(',');
                int numlength = output.length() - nSeparatorIndex;
                string votes = output.substr(nSeparatorIndex+1,numlength);
                int numVotes;
                istringstream(votes) >> numVotes;
                TotalVotes = TotalVotes + numVotes;
                Names[index] = name;
                Votes[index] = numVotes;
            }
            index++;
        }
        myReadFile.close();
        for(int i=0;i< 5;i++)
        {
            if(i==0)
                maxIndex = 0;
            else
            {
                if(Votes[i] > Votes[maxIndex])
                    maxIndex = i;
            }
            percentages[i] = ((float)Votes[i]/TotalVotes) * 100;
        }
        cout<< "Candidates Votes Received % of Votes"<<endl;
        for(int i=0;i< 5;i++)
        {
            cout<<Names[i]<< " " << Votes[i] << " " << percentages[i]<<endl;
        }
        cout<<"Total Votes "<<TotalVotes<<endl;
        cout<<endl;
        cout<<"The winner of the election is "<<Names[maxIndex];
    }
    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote