Question

In: Computer Science

C++ Write a program that reads candidate names and numbers of votes in from a file....

C++

Write a program that reads candidate names and numbers of votes in from a file. You may assume that each candidate has a single word first name and a single word last name (although you do not have to make this assumption). Your program should read the candidates and the number of votes received into one or more dynamically allocated arrays.

In order to allocate the arrays you will need to know the number of records in the file. In order to determine the number of records in the file you may include the number as metadata in the first line of the file or you may read through the file to count the lines and then reset the file pointer back to the beginning of the file to read in the data. Do not hard code the number of candidates. Do not prompt the user to enter the number of candidates. You must either count the number of candidates in the file or encode the number as metadata as the first record in the file.

The program should output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. You may, but are not required to, use the example file provided.

Solutions

Expert Solution

C++ Program:

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

using namespace std;

const int no0fcandidate = 100;

int main()
{
int cnt;
string cNames[no0fcandidate];
string cLNames[no0fcandidate];
int votes[no0fcandidate];
float voteP[no0fcandidate];
int totalVotes = 0;
float maxVotesPercentage;
string winner;

//Opening file for reading
fstream fin("votes.txt", ios::in);

//Reading number of contestants
fin >> cnt;

for (int i = 0; i < cnt; i++)
{
fin >> cNames[i] >> cLNames[i] >> votes[i];
}

//Calculating total number of votes
for (int i = 0; i < cnt; i++)
{
totalVotes = totalVotes + votes[i];
}

//Finding average
for (int i = 0; i < cnt; i++)
{
voteP[i] = ((float) votes[i] / totalVotes) * 100;
}

//Percentage
maxVotesPercentage = voteP[0];

//Winner
winner = cNames[0];

//Calculating winner
for (int i = 0; i < cnt; i++)
{
//Comparing
if (voteP[i] > maxVotesPercentage)
{
maxVotesPercentage = voteP[i];

winner = cNames[i];
}
}

//Printing result
cout << endl << left << setw(15) << " " << left << setw(25) << "Candidate " << left << setw(20) << "Votes Received" << left << setw(15) << "% of Total Votes" << endl << endl;
cout << fixed << setprecision(2);

for (int i = 0; i < cnt; i++)
{
cout << left << setw(6) << " " << left << setw(20) << cNames[i] << left << setw(20) << cLNames[i] << left << setw(20) << votes[i] << left << setw(15) << voteP[i] << endl;
}

cout << endl << left << setw(6) << " " << left << setw(20) << "Total " << left << setw(20) << totalVotes << endl;

cout << endl << "The Winner of the Election is " << winner << endl;
return 0;
}

_________________________________________________________________________________________

Sample Run:


Related Solutions

4. Write a program that reads all numbers from a file and determines the highest and...
4. Write a program that reads all numbers from a file and determines the highest and lowest numbers. You must NOT use arrays to solve this problem! Write functions where appropriate. Programming language should be C
Write a C ++ program which opens a file and reads several numbers, utilizing the fscanf()...
Write a C ++ program which opens a file and reads several numbers, utilizing the fscanf() function. Can you add few comments with explanations what is going on?
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
Write a program in c that reads the content from the file and stores each line...
Write a program in c that reads the content from the file and stores each line in an int array in heap(using dynamic memory allocation). For example, let the file has elements following (we do not know the size of files, it could be above 100,000 and contents of the file and make sure to convert file elements to int): 10067 26789 6789 3467
Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using Notepad) into a one-dimensional array and then analyzes the numbers as described below. Your program must use loops to read the numbers into the array and to analyze the contents of the array. The program’s main function should do the following:  Read eight floating-point numbers from the file named numbers.txt into a onedimensional array, displaying each number on the screen.  Pass the...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write a C++ program that reads numbers from the user until the user enters a Sentinel....
Write a C++ program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input. Do the following: Output the sum of all even numbers Output the sum of all odd numbers Output the count of all even numbers Output the count of all odd numbers You must use loops and numbers to do this. You must not use any arrays or vectors for this program.
C Programming Write a program in C that reads in a file, stores its contents as...
C Programming Write a program in C that reads in a file, stores its contents as a character array/pointer (char*) into an unsigned character array/pointer (unsigned char* message). Note: the input file can have one line or multiple lines and vary in length
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT