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

}

Also, how do i make verticle columbs for the display?

Solutions

Expert Solution

// C++ program to read data of candidates from file and perform operations

#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;

int curr_size = 0;// variable to store the current number of candidates in the array

// function declaration

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];

       curr_size = 0;

       readFile(candidates);

       Scores(candidates);

       cout<<"Candidates : "<<endl;

       List(candidates);

       Votes(candidates);

       cout<<"\nCandidates sorted by votes in descending order : "<<endl;

       List(candidates);

       cout<<"\nCandidate with highest Score : "<<endl;

       displayCandidate(First(candidates));

       cout<<"\nCandidate with lowest Score : "<<endl;

       displayCandidate(Last(candidates));

       return 0;

}

void readFile(Candidate candidates[]) {

       string line;

       string fname, lname, votes;

       ifstream infile;

       size_t index;

       infile.open("elections.txt");

       if(infile.is_open())

       {

             // read till the end of file

             while (!infile.eof()) {

                    getline(infile,line);

                    if(!infile.eof()) // if its not the last line

                           line = line.substr(0,line.length()-1);

                    if(line.length() > 0)

                    {

                           index = line.find(','); //find the index of first comma

                           if(index != string::npos) // if not present ignore the record

                           {

                                 fname = line.substr(0,index); // get the firstname field

                                 if(fname.find(' ') != string::npos) // check for space

                                 {

                                        fname = fname.substr(0,fname.find(' ')) + fname.substr(fname.find(' ')+1); // extract the firstname

                                 }

                                 if(line.find(',',index+1) != string::npos) // find the index for next comma

                                 {

                                        lname = line.substr(index+1,line.find(',',index+1)-index-1); // get the last name field

                                        index = line.find(',',index+1);

                                        if(lname.find(' ') != string::npos) // check for space

                                        {

                                               lname = lname.substr(0,lname.find(' ')) + lname.substr(lname.find(' ')+1); // extract the lastname

                                        }

                                        votes = line.substr(index+1); // get the number of votes field

                                        if(votes.find(' ') != string::npos) // check for comma

                                               votes = votes.substr(0,votes.find(' ')) + votes.substr(votes.find(' ')+1); //extract the votes

                                        if(votes.length() > 0)

                                        {

                                               // get the fieldnames of the candidate

                                               candidates[curr_size].Fname = fname.substr(fname.find('=')+1);

                                               candidates[curr_size].Lname = lname.substr(lname.find('=')+1);

                                               candidates[curr_size].votes = stoi(votes.substr(votes.find('=')+1)); // stoi converts string to inetger (requires C++ 11)

                                               candidates[curr_size].Score = 0;

                                               curr_size++;

                                        }

                                 }

                           }

                    }

             }

             infile.close();

       }else

             cout<<"Unable to open file : elections.txt"<<endl;

}

// function to print all the candidates in a tabular format

void List(Candidate candidates[]) {

       cout<<left<<setw(15)<<"Firstname"<<left<<setw(15)<<"Lastname"<<left<<setw(10)<<"Votes"<<left<<"Score"<<endl;

       for(int i=0;i<curr_size;i++)

       {

             displayCandidate(candidates[i]);

       }

}

// function to sort the candidates array in descending order by votes

void Votes(Candidate candidates[]) {

       int max;

       for(int i=0;i<curr_size-1;i++)

       {

             max = i;

             for(int j=i+1;j<curr_size;j++)

                    if(candidates[j].votes > candidates[max].votes)

                           max = j;

             if(max != i)

             {

                    Candidate temp = candidates[i];

                    candidates[i] = candidates[max];

                    candidates[max] = temp;

             }

       }

}

// function to display the details of a candidate

void displayCandidate(Candidate candidates) {

       cout<<left<<setw(15)<<candidates.Fname<<left<<setw(15)<<candidates.Lname<<left<<setw(10)<<candidates.votes<<left<<fixed<<setprecision(2)<<candidates.Score<<endl;

}

// function to return the candidate with highest score

Candidate First(Candidate candidates[]) {

       int max = 0;

       for(int i=1;i<curr_size;i++)

       {

             if(candidates[i].Score > candidates[max].Score)

                    max = i;

       }

       return candidates[max];

}

// function to return candidate with lowest score

Candidate Last(Candidate candidates[]) {

       int min = 0;

       for(int i=1;i<curr_size;i++)

       {

             if(candidates[i].Score < candidates[min].Score)

                           min = i;

       }

       return candidates[min];

}

// function to calculate the scores of the candidate

void Scores(Candidate candidates[]) {

       double total = 0;

       for(int i=0;i<curr_size;i++)

       {

             total += candidates[i].votes;

       }

       if(total > 0)

       {      for(int i=0;i<curr_size;i++)

             {

                           candidates[i].Score = ((double)(candidates[i].votes*100))/total;

             }

       }

}

//end of program

Output:

Input file:

Output:


Related Solutions

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...
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. ***DO NOT REPLY ANUNAGA.*** HOW TO DO?...
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...
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. HOW TO DO? ****IMPORTANT**** PLEASE READ CAREFULLY...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT