Question

INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? PLEASE READ CAREFULLY. MAX_SIZE...

INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? PLEASE READ CAREFULLY.

MAX_SIZE is size of array

When iterating over the candidates’ list, do not iterate over the entire array, but just over the records where data is filled in

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?:

#include <iostream>

#include <fstream>

#include <string>

#include <stdlib.h>

#include <iomanip>

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[]) {

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

line.find(F=); line.find(, L=); line.find(,V=); int firstPos int nextPos int thirdPos //Checks that Line format conditi

--

answer is given in Bold format

if you have any doubt, please mention it

love to help

all the best

please upvote

Add a comment
Know the answer?
Add Answer to:
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? PLEASE READ CAREFULLY. MAX_SIZE...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT