Question

In: Computer Science

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

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

}

ANUNAGA: YOU CAN ADD TO THE FUNCTION, BUT YOU CAN;T CHANGE IT. THE PROGRAMS NEEDS TO READ UNTIL END OF FILE, AND THEN READ THE LINE UNTIL OF THE LINE. IT CHECKS FOR COMMAS. YOU'RE NOT SUPPOSE TO KNOW EXACTLY HOW MANY.

Solutions

Expert Solution

Code:

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
#include <iomanip>

using namespace std;

struct Candidate {
   string Fname;
   string Lname;
   int votes;
   double Score = 0;
};

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 candidateCnt = 0;

int main() {
   Candidate candidates[MAX_SIZE];
   readFile(candidates);
   List(candidates);
   Votes(candidates);
   Scores(candidates);
   displayCandidate(First(candidates));
   displayCandidate(Last(candidates));
   return 0;
}

void readFile(Candidate candidates[]) {

   string line;

   ifstream infile;

   infile.open("elections.txt");


   while (!infile.eof()) {

       getline(infile, line);

       bool bExcludeLine = false; //flag to exclude the line
       int findResult = 0;
       findResult = line.find('=', findResult); //getting the first =
       int loopCount = 0; //To determine which word
       Candidate aCadidate;
       //The idea here is that findResult will point to the = in the string
       //Inside the while loop there is another find to get the next = in the string
       //Get the substring between those two =
       //Check for the , delimitor in that substring
       //If , is not present then set the bExcludeLine = true and break
       //If , is present then find out the substring Fname,Lname etc.
       //Add to the Candidate array only if the bExcludeLine = false
       while (findResult != string::npos)
       {
           int offset = 0;
           offset = line.find('=', (findResult + 1));
           bool bIsLastWord = false;
           if (offset == string::npos)
           {
               offset = line.length();
               bIsLastWord = true;
           }
           int commapos = 0;
           string aString = line.substr((findResult + 1), (offset - findResult));
           if (!bIsLastWord && (commapos = aString.find(',')) == string::npos)
           {
               bExcludeLine = true;
               break;
           }
           if (bIsLastWord)
           {
               stringstream ss(aString);
               int votes;
               ss >> votes; //Converting to integer
               aCadidate.votes = votes;
               break;
           }
           if (0 == loopCount) //To determine which is the string
           {
               aCadidate.Fname = aString.substr(0, commapos);
           }
           else if (1 == loopCount)
           {
               aCadidate.Lname = aString.substr(0, commapos);
           }
           findResult = offset;
           ++loopCount;
       }

       if(!bExcludeLine)
           candidates[candidateCnt++] = aCadidate;

   }

   infile.close();

}

void List(Candidate candidates[]) {
   for (int i = 0; i < candidateCnt; i++)
   {
       cout << std::setw(15) << candidates[i].Fname <<
           std::setw(15) << candidates[i].Lname <<
           std::setw(15) << candidates[i].votes <<
           std::setw(15) << candidates[i].Score;
       cout << endl;
   }
}

void Votes(Candidate candidates[]) {
   //Used Bubble Sort
   for (int i = 0; i < candidateCnt; i++)
   {
       for (int j = 0; j < candidateCnt - i - 1; j++)
       {
           if (candidates[j].votes > candidates[j + 1].votes)
           {
               Candidate tempCandidate = candidates[j];
               candidates[j] = candidates[j + 1];
               candidates[j + 1] = tempCandidate;
           }
       }
   }
}

void displayCandidate(Candidate candidates) {
   cout << std::setw(15) << candidates.Fname <<
       std::setw(15) << candidates.Lname <<
       std::setw(15) << candidates.votes <<
       std::setw(15) << candidates.Score;
   cout << endl;
}

Candidate First(Candidate candidates[]) {
   int maxScoreIndex = 0;
   double maxScore = candidates[0].Score;
   for (int i = 0; i < candidateCnt; i++)
   {
       if (candidates[i].Score > maxScore)
       {
           maxScore = candidates[i].Score;
           maxScoreIndex = i;
       }
   }
   return candidates[maxScoreIndex];
}

Candidate Last(Candidate candidates[]) {
   int minScoreIndex = 0;
   double minScore = candidates[0].Score;
   for (int i = 0; i < candidateCnt; i++)
   {
       if (candidates[i].Score < minScore)
       {
           minScore = candidates[i].Score;
           minScoreIndex = i;
       }
   }
   return candidates[minScoreIndex];
}

void Scores(Candidate candidates[]) {
   double sumofVotes = 0.00;
   for (int i = 0; i < candidateCnt; i++)
   {
       sumofVotes += candidates[i].votes;
   }

   for (int i = 0; i < candidateCnt; i++)
   {
       candidates[i].Score = (candidates[i].votes / sumofVotes) * 100;
   }
}

OUTPUT:


Related Solutions

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...
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? 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...
****NEED CODED IN C++, READ THE INSTRUCTIONS CAREFULLY AND PAY ATTENTION TO THE INPUT FILE, IT...
****NEED CODED IN C++, READ THE INSTRUCTIONS CAREFULLY AND PAY ATTENTION TO THE INPUT FILE, IT IS REQUIRED FOR USE IN THE PROBLEM**** You are to generate a list of customers to serve based on the customer’s priority, i.e. create a priority queue/list for a local company. The company has been receiving request and the request are recorded in a file, in the order the request was made. The company processes each user based on their priority, the highest priority...
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]; }
c++. please read this task carefully. First, try given examples of input to your code, if...
c++. please read this task carefully. First, try given examples of input to your code, if it works, then send it here. Create structure Applicants with following fields: struct applicants{ int id; string name; string surname; int subject1,subject2,subject3,selectedSubject; string specialCase; int total; }; SpecialCase (if applicant has "Awardee of Olympiads", then this field is "true", otherwise "false"). Do not forget that all "Awardees of Olympiads" will gain grants automatically. If the total points are same your algorithm have to choose...
c++ Please read the instructions carefully. You must put the proper items in the proper file...
c++ Please read the instructions carefully. You must put the proper items in the proper file (eitehr SSandwich.h or SSandwich.cpp. Do NOT include any main function in your submission. You are encouraged to write your own main function to test what you are submitting. You are submit two files via BlackBoard: A file named SSandwich.h, a header file defining the class SSandwich. No inline methods are permitted. Any enum classes should be defined here also. Your header file should have...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT