In: Computer Science
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? ****IMPORTANT**** PLEASE READ CAREFULLY ****IMPORTANT****
***GOALS***
HOW TO CHECK FOR COMMAS, TILL THE END OF FILE. IT WILL CHECK THE LINE FOR THE APPRORIATE FORMAT IN THE TEXT FILE. IF THERE IS MISSING A COMMA, IT WILL IGNORE, IF THERE IS A WHITE SPACE, IT WILL CORRECT AND READ LINE, IF IT IS MISSING 1 OF THE 3 INFORMATION, IT WILL IGNORE.
Display candidates’ names using displayList()
function
Execute the getWinner(Candidate[]) function to get the candidate
with the highest number of votes. Display his name along with
number of votes.
Execute the getLast(Candidate[]) function to get the candidate
with the lowest number of votes. Display his name along with number
of votes.
Calculate pScore for each candidate
Sort candidates by votes
Display sorted list using displayList() function
For three records with the highest pScore, use the roundScore
function to round the pScore
Display list again using displayList() function
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 WHAT IS ALREADY THERE ON THE SKELETON, YOU MAY ADD EXTRA INFORMATION THOUGH:
#include <iostream>
#include <iomanip>
#include <string>
#include <stdlib.H>
#include <fstream>
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[]) {
}
Solution
code
void readFile(Candidate candidates[])
{
//Variables for taking the file, storing the lines into strings,
and counting the amt of lines found.
string line;
ifstream infile;
int counter=0;
//Open file of cadidate data.
infile.open("elections.txt");
//Goes threw data in while loop.
while (!infile.eof())
{
getline(infile,line);//sets line in file to string variable.
//Finds proper format conditons in saved line of
data.
int firstPos = line.find("F=");
int nextPos = line.find(",L=");
int thirdPos = line.find(",V=");
//Checks that line format conditions are true then
stores it in struct array if true.
if (firstPos==0 && nextPos>0 &&
thirdPos>0)
{
//Sets the first name out of line.
candidates[counter].first =
line.substr(2,line.find(",L=")-2);
//Cuts out used part of line.
line = line.substr(line.find(",L=")+3);
//Sets the last name from next part of line.
candidates[counter].last = line.substr(0, line.find(",V="));
//Cuts out used part of line up to votes.
line = line.substr(line.find(",V=")+3);
//Converts rest of string to double number.
double numVotes = atof(line.c_str());
//Stores double number into votes.
candidates[counter].votes = numVotes;
//Sets pScore as zero to start.
candidates[counter].pScore = 0;
//Increment counter.
counter++;
}
//If line of data isnt formatted properly, continue to next.
else
continue;
}
infile.close();
}
Screenshot
--
answer is given in Bold format
if you have any doubt, please mention it
love to help
if you need complete code also, let me know
all the best
please upvote