In: Computer Science
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. ***DO NOT REPLY ANUNAGA.***
HOW TO DO?
*****IMPORTANT******* PLEASE READ CAREFULLY. WE HAVE TO DO WHAT THIS ASSIGNMENT DOES OR WE WILL MARKED OFF POINTS. IT DOES NOT HELP WHEN YOU CHANGE THE SKELETON TO YOU'RE PREFERENCE. YOU CAN ADD TO IT, BUT NOT CHANGE WHAT IS ALREADY THERE. THIS IS FOR A BASIC C++ LEVEL CLASS SO WE HAVE TO STICK TO BASIC C++ CODE. HOWEVER IT COULD BE WRONG IN TERMS OF WORKING CONDITIONS SO PLEASE HELP FIX THESE. *IMPORTANT*
Input format for each line:
F=firstname,L=lastname,V=votes.
Read only valid lines into the array
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? IT IS A SKELETON, WHICH MEANS MORE VARIABLES AND SUCH CAN BE ADDED:
#include
#include
#include
#include
#include
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