In: Computer Science
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO?
void readFile(Candidate candidates[]) – reads the elections.txt file, fills the candidates[] array. Hint: use substr() and find() functions. Set Score to 0.
void List(Candidate candidates[]) – prints the array of Candidate
structs. One candidate per one line, include all fields. Use setw()
to display nice looking list.
void displayCandidate(Candidate candidates[]) – prints the complete
information about the candidate
.
Candidate First(Candidate candidates[]) – returns single struct
element: candidate with highest score
Candidate Last(Candidate candidates[]) – returns single struct
element: candidate with lowest score
void Votes(Candidate candidates[]) – function sorts the
candidates[] array by number of votes, the order in candidates[]
array is replaced
void Scores(Candidate candidates[]) – calculates the percentage
score for each candidate. Use the following formula:
??????=(CandidateVotes)/(sum of votes)*100%
Correct line for the reference: F=John,L=Smith,V=3342
The line errors that your program needs to detect, are as follows:
incorrect token / separator, example in line 5: F=Steven,L=JohnV=4429 --- (comma missing) – lines with this error need to be ignored
space in token, example in line 3: F=Hillary,X=Clinton, V=1622 --- lines with this error need to be read, error fixed, data included in your dataset
empty line, example in line 6 – empty lines need to be ignored
Example Textfile
F=Michael,L=John,V=3342
F=Danny,L=Red,V=2003
F=Hillary,L=Clinton, V=1588
F=Albert,L=Lee,V=5332
F=Steven,L=JohnV=4429
*IMPORTANT* How would I do the readFile function? It says to check if the commas are present, and that the program will correct the line if there is white spaces. How do i use the find() function? Please be DETAILED in explanations of each part of code. Beginner Coder. *IMPORTANT*
Code Skeleton We HAVE to follow. How Would i go about using this skeleton? YOU CANNOT CHANGE FUNCTIONS OF VARIABLES, BUT YOU MAY ADD TO IT. THE CODE MUST HAVE WHAT IS LISTED IN THE SKELETON CODE:
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <fstream>
#include <string>
using namespace std;
struct Candidate {
string Fname;
string Lname;
int votes;
double Score;
};
const int MAX_SIZE = 100;
void readFile(Candidate[]);
void List(Candidate[]);
void Votes(Candidate[]);
void displayCandidate(Candidate);
Candidate First(Candidate[]);
Candidate Last(Candidate[]);
void Scores(Candidate[]);
int main() {
}
void readFile(Candidate candidates[]) {
string line;
ifstream infile;
infile.open("elections.txt");
while (!infile.eof()) {
getline(infile,line);
// your code here
}
infile.close();
}
void List(Candidate candidates[]) {
}
void Votes(Candidate candidates[]) {
}
void displayCandidate(Candidate candidates) {
}
Candidate First(Candidate candidates[]) {
}
Candidate Last(Candidate candidates[]) {
}
void Scores(Candidate candidates[]) {
}
Also, how do i make verticle columbs for the display?
// C++ program to read data of candidates from file and perform operations
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <fstream>
#include <string>
using namespace std;
struct Candidate {
string Fname;
string Lname;
int votes;
double Score;
};
const int MAX_SIZE = 100;
int curr_size = 0;// variable to store the current number of candidates in the array
// function declaration
void readFile(Candidate[]);
void List(Candidate[]);
void Votes(Candidate[]);
void displayCandidate(Candidate);
Candidate First(Candidate[]);
Candidate Last(Candidate[]);
void Scores(Candidate[]);
int main() {
Candidate candidates[MAX_SIZE];
curr_size = 0;
readFile(candidates);
Scores(candidates);
cout<<"Candidates : "<<endl;
List(candidates);
Votes(candidates);
cout<<"\nCandidates sorted by votes in descending order : "<<endl;
List(candidates);
cout<<"\nCandidate with highest Score : "<<endl;
displayCandidate(First(candidates));
cout<<"\nCandidate with lowest Score : "<<endl;
displayCandidate(Last(candidates));
return 0;
}
void readFile(Candidate candidates[]) {
string line;
string fname, lname, votes;
ifstream infile;
size_t index;
infile.open("elections.txt");
if(infile.is_open())
{
// read till the end of file
while (!infile.eof()) {
getline(infile,line);
if(!infile.eof()) // if its not the last line
line = line.substr(0,line.length()-1);
if(line.length() > 0)
{
index = line.find(','); //find the index of first comma
if(index != string::npos) // if not present ignore the record
{
fname = line.substr(0,index); // get the firstname field
if(fname.find(' ') != string::npos) // check for space
{
fname = fname.substr(0,fname.find(' ')) + fname.substr(fname.find(' ')+1); // extract the firstname
}
if(line.find(',',index+1) != string::npos) // find the index for next comma
{
lname = line.substr(index+1,line.find(',',index+1)-index-1); // get the last name field
index = line.find(',',index+1);
if(lname.find(' ') != string::npos) // check for space
{
lname = lname.substr(0,lname.find(' ')) + lname.substr(lname.find(' ')+1); // extract the lastname
}
votes = line.substr(index+1); // get the number of votes field
if(votes.find(' ') != string::npos) // check for comma
votes = votes.substr(0,votes.find(' ')) + votes.substr(votes.find(' ')+1); //extract the votes
if(votes.length() > 0)
{
// get the fieldnames of the candidate
candidates[curr_size].Fname = fname.substr(fname.find('=')+1);
candidates[curr_size].Lname = lname.substr(lname.find('=')+1);
candidates[curr_size].votes = stoi(votes.substr(votes.find('=')+1)); // stoi converts string to inetger (requires C++ 11)
candidates[curr_size].Score = 0;
curr_size++;
}
}
}
}
}
infile.close();
}else
cout<<"Unable to open file : elections.txt"<<endl;
}
// function to print all the candidates in a tabular format
void List(Candidate candidates[]) {
cout<<left<<setw(15)<<"Firstname"<<left<<setw(15)<<"Lastname"<<left<<setw(10)<<"Votes"<<left<<"Score"<<endl;
for(int i=0;i<curr_size;i++)
{
displayCandidate(candidates[i]);
}
}
// function to sort the candidates array in descending order by votes
void Votes(Candidate candidates[]) {
int max;
for(int i=0;i<curr_size-1;i++)
{
max = i;
for(int j=i+1;j<curr_size;j++)
if(candidates[j].votes > candidates[max].votes)
max = j;
if(max != i)
{
Candidate temp = candidates[i];
candidates[i] = candidates[max];
candidates[max] = temp;
}
}
}
// function to display the details of a candidate
void displayCandidate(Candidate candidates) {
cout<<left<<setw(15)<<candidates.Fname<<left<<setw(15)<<candidates.Lname<<left<<setw(10)<<candidates.votes<<left<<fixed<<setprecision(2)<<candidates.Score<<endl;
}
// function to return the candidate with highest score
Candidate First(Candidate candidates[]) {
int max = 0;
for(int i=1;i<curr_size;i++)
{
if(candidates[i].Score > candidates[max].Score)
max = i;
}
return candidates[max];
}
// function to return candidate with lowest score
Candidate Last(Candidate candidates[]) {
int min = 0;
for(int i=1;i<curr_size;i++)
{
if(candidates[i].Score < candidates[min].Score)
min = i;
}
return candidates[min];
}
// function to calculate the scores of the candidate
void Scores(Candidate candidates[]) {
double total = 0;
for(int i=0;i<curr_size;i++)
{
total += candidates[i].votes;
}
if(total > 0)
{ for(int i=0;i<curr_size;i++)
{
candidates[i].Score = ((double)(candidates[i].votes*100))/total;
}
}
}
//end of program
Output:
Input file:
Output: