Question

In: Computer Science

INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? void readFile(Candidate candidates[]) –...

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:

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

}

TO ANUNAGA: I don't know what you want, this is the code we HAVE TO go by and code by and figure out. I can't give you anymore information. Please, either help me solve this or just don't.

Solutions

Expert Solution

#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() {
        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;
                }
        }
}
**************************************************

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.


Related Solutions

INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. void readFile(Candidate candidates[]) –...
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...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? *IMPORTANT* PLEASE READ CAREFULLY....
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. 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. 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* void readFile(Candidate candidates[]) – reads...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. alot of people give...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. alot of people give me either partial answers, or incorrect skeleton. PLEASE YOU CAN'T CHANGE WHAT IS THERE, YOU CAN ONLY ADD. 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...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. alot of people give...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. alot of people give me either partial answers, or incorrect skeleton. PLEASE YOU CAN'T CHANGE WHAT IS THERE, YOU CAN ONLY ADD. 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...
C++ How do you make a 2d array with a user input. For example, the row...
C++ How do you make a 2d array with a user input. For example, the row and columns of the 2d array will be the same so the program can create a square matrix.
How to read the given structure from a random CSV file separated by commas(which contains no...
How to read the given structure from a random CSV file separated by commas(which contains no headers only the values of the contents of the structure) and then insert in a binary search tree using one of the structure contents as a key i.e. datetime and handle duplicates in binary search tree by implementing link_list.Please develop a C code for this. struct data{ char biker_id[200]; char distance_bike_travelled[200]; char datetime[200]; char count_tripr[200]; }
Checking Input File for correct line format. So for my C++ assignment, my professor says we...
Checking Input File for correct line format. So for my C++ assignment, my professor says we need to validate the Input File, if there are white lines and spaces, in which it will ignore them, separated by a comma, and that all data items are there. This will be used in a program where i will store these into an Array, and display them in First name, Last name, and Number of Votes. This is a sample of the txt...
Checking Input File for correct line format. So for my C++ assignment, my professor says we...
Checking Input File for correct line format. So for my C++ assignment, my professor says we need to validate the Input File, if there are white lines and spaces, in which it will ignore them, separated by a comma, and that all data items are there. This will be used in a program where i will store these into an Array, and display them in First name, Last name, and Number of Votes vertical columned categories. This is a sample...
Construct an array of 1000 random integers within range [0, 100] An input file input.txt is...
Construct an array of 1000 random integers within range [0, 100] An input file input.txt is provide. Each line of input.txt is a query integer that you need to check how many of that number is in your random integer array. For each query integer, fork a new child process to do the counting. The output is for each input query, output the count and child process id. For example: $> query: 13    count: 5    pid: 13342 $> query: 22   ...
Write a function called fillList that takes three parameters, an integer array, input file, and size....
Write a function called fillList that takes three parameters, an integer array, input file, and size. The function should fill the integer array with randomly generated values between two numbers lowLim and highLim read from the input file. in C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT