In: Computer Science
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.
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: