In: Computer Science
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 of the txt file. My question is, What do i use to be able to take only the names and number and place them into an array. Please explain what each line is doing, even it is not completely obvious. I am a complete beginner.
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
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
This is the sample of the getline code we are suppose to use.
void readFile(Candidate candidates[]) {
string line;
ifstream infile;
infile.open("elections.txt");
while (!infile.eof()) {
getline(infile,line);
// your code here
}
infile.close();
}
/************************************
c++ program
**************************************/
#include <iostream>
#include <fstream>
#include <string.h>
#include <vector>
using namespace std;
//class for Candidate
class Candidate {
private:
string firstName;
string lastName;
int numberOfVotes;
public:
Candidate(){
}
Candidate(string fName,string lName,int votes){
firstName = fName;
lastName = lName;
numberOfVotes = votes;
}
string getFirstName(){
return firstName;
}
string getLastName(){
return lastName;
}
int getNumberOfVotes(){
return numberOfVotes;
}
};
void readFile(Candidate candidates[]) {
string line;
ifstream infile;
infile.open("elections.txt");
int candidateCounter = 0;
while (!infile.eof()) {
getline(infile,line);
if(line.empty()){
continue;
}
//parse each line with respect to comma
char lineChars[line.length()];
int i;
//copy current linr string into char array
for(i = 0; i < sizeof(lineChars); i++){
lineChars[i] = line[i];
}
lineChars[i+1]= '\0'; //terminate the char array with end of
string
char* lineToken = strtok(lineChars, ","); //start tokennize char
array lineChars with comma
vector<string> lineVectors; //here we will put each token for
this line which was separayed by comma
while(lineToken!=NULL) //continue as long as final token fr this
line is not found
{
lineVectors.push_back(lineToken); //add the token
// cout<<lineToken<<endl;
lineToken = strtok(NULL,",");
}
vector<string> dataVectors;
for(int k = 0 ; k < lineVectors.size(); k++){
//for each lineVector get char array
string data = lineVectors[k];
char record[data.length()];
int j;
for(j = 0; j < sizeof(record); j++){
if(data[j]==' '){
j++;
}
record[j] = data[j];
}
record[j+1]='\0'; //charb array is formed from current string in
linevector
//parse the char array with respect to "="
char* recordToken = strtok(record, "=");
int counter = 0;
while(recordToken!=NULL){
counter++;
//cout<<recordToken<<endl;
if(counter == 2){
dataVectors.push_back(recordToken); //put the right side tken of of
=, into dataVector
}
recordToken = strtok(NULL,"=");
}
}//end of processing of each string in curent line vector
if(dataVectors.size()==3){ //if data vector has 3 items in it
(first name, last name, numbr of votes)
int votes = stoi(dataVectors[2]); //convert vote from string to
int
candidates[candidateCounter] =
Candidate(dataVectors[0],dataVectors[1],votes);//add it to
candidates array
candidateCounter++; //increase counter index
}
}
infile.close();
}
int main()
{
int numOfCandidates = 10;
Candidate candidates[numOfCandidates] = {};//create array of 10
candidates
readFile(candidates); //candidates array will be populated
for(int i = 0; i < 10 ;i ++){
Candidate c = candidates[i];
if(c.getFirstName().empty()){
continue;
}
//print data
cout<< c.getFirstName()<<" ;
"<<c.getLastName()<<" ; "<<
c.getNumberOfVotes()<<endl;
}
}
========================================
output
=========================================
Michael ; John ; 6522
Danny ; Red ; 2003
Hillary ; Clinton ; 1588
Albert ; Lee ; 5332
Roger ; Kane ; 3562
=========================
input file used
=========================
F=Michael,L=John,V=6522
F=Danny,L=Red,V=2003
F=Hillary,L=Clinton, V=1588
F=Albert,L=Lee,V=5332
F=Steven,L=JohnV=4429
F=Roger,L=Kane,V=3562