In: Computer Science
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY.
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* 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[]) {
}
#include <iostream>
#include <iomanip>
#include <cstdlib>
#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() {
Candidate candidates[MAX_SIZE];
readFile(candidates);
Scores(candidates);
Votes(candidates);
cout << endl;
List(candidates);
}
// trim a string, it removes any spaces at end and start of the string
string trim(string& str) {
str.erase(0, str.find_first_not_of(' ')); //prefixing spaces
str.erase(str.find_last_not_of(' ')+1); //surfixing spaces
return str;
}
// this method counts the occurrences of a character in string.
int countCharacter(string s, char c) {
int count = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == c) {
count++;
}
}
return count;
}
// this method parses the token which is in format:
// F=<firstName>
// L=<lastName>
// V=<Votes>
void parseToken(string s, Candidate &c) {
if(s[0] == 'F') {
c.Fname = s.substr(2);
} else if(s[0] == 'L') {
c.Lname = s.substr(2);
} else if(s[0] == 'V') {
c.votes = atoi(s.substr(2).c_str());
}
}
// this method converts a comma separated string to tokens
// it returns all tokens in form of a vector
void tokens(string s, Candidate &c) {
// while string has a comma
while(s.find(",") != string::npos) {
// find index of comma in string
int index = s.find(",");
// break token till comma
string token = s.substr(0, index);
// remove the token from original string
s = s.substr(index + 1);
// remove space from start of token, and push to vector of tokens
parseToken(trim(token), c);
}
// if last token is now not empty, push that too on vector
if(!s.empty()) {
parseToken(trim(s), c);
}
}
void readFile(Candidate candidates[]) {
string line;
ifstream infile;
infile.open("elections.txt");
if(infile.fail()) {
cout << "unable to open file elections.txt" << endl;
return;
}
int i = 0;
while (!infile.eof()) {
// read a line from the file
getline(infile,line);
// Here comes the main logic to parse the line
int commaCount = countCharacter(line, ',');
if(commaCount != 2) {
// Ignore line,
cout << "Incorrect token separator: " << line << endl;
} else if(line.empty()) {
// ignore empty line.
} else {
// We now have a line which does contain correct commas
// and is not empty line.
tokens(line, candidates[i++]);
}
}
infile.close();
}
void List(Candidate candidates[]) {
for(int i=0; i<MAX_SIZE; i++) {
if(!candidates[i].Fname.empty()) {
displayCandidate(candidates[i]);
}
}
}
void Votes(Candidate candidates[]) {
int size = MAX_SIZE;
for(int i=0; i<MAX_SIZE; i++) {
if(candidates[i].Fname.empty()) {
size = i;
break;
}
}
for(int i=0; i<size-1; i++) {
int minIndex = i;
for(int j=i+1; j<size; j++) {
if(candidates[j].Score < candidates[minIndex].Score) {
minIndex = j;
}
}
Candidate tmp = candidates[i];
candidates[i] = candidates[minIndex];
candidates[minIndex] = tmp;
}
}
void displayCandidate(Candidate candidate) {
cout << candidate.Fname << " " << candidate.Lname << " " << candidate.Score << endl;
}
Candidate First(Candidate candidates[]) {
double max = 0;
Candidate result;
for(int i=0; i<MAX_SIZE; i++) {
if(!candidates[i].Fname.empty() && candidates[i].Score > max) {
max = candidates[i].Score;
result = candidates[i];
}
}
return result;
}
Candidate Last(Candidate candidates[]) {
double min = 100;
Candidate result;
for(int i=0; i<MAX_SIZE; i++) {
if(!candidates[i].Fname.empty() && candidates[i].Score < min) {
min = candidates[i].Score;
result = candidates[i];
}
}
return result;
}
void Scores(Candidate candidates[]) {
int total = 0;
for(int i=0; i<MAX_SIZE; i++) {
if(!candidates[i].Fname.empty()) {
total += candidates[i].votes;
}
}
for(int i=0; i<MAX_SIZE; i++) {
if(!candidates[i].Fname.empty()) {
candidates[i].Score = candidates[i].votes * 100.0/total;
}
}
}
************************************************** Answering the input logic here.. please ask other functions separately if required. Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.