In: Computer Science
Validating Input file,
So for my C++ assignment, my professor says we need to validate the Input File, Which means, if the line is missing a comma, one of the 3 information, or the string is a white line, it will ignore it. If the line has a white line, the program will correct it and will read in the line.
I will store these into an Array, and display them in First name, Last name, and Number of Votes vertical columned categories.
Apparently I skipped this part of the lecture and book, since he won't tell me exactly and keeps telling me to read the book. But the book shows me how to make arrays, take a file and put it into an array. He said using substr and find() or something like that.
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 HAVE to USE and FOLLOW.
void readFile(Candidate candidates[]) {
string line;
ifstream infile;
infile.open("elections.txt");
while (!infile.eof()) {
getline(infile,line);
// your code here
}
infile.close();
}
I DO NOT NEED A WHOLE CODE, JUST THE FUNCTION EXPLAINED WHAT THE FUNCTION AND THE CODE IS DOING BY LINE.
####################################### elections.txt ####################################### 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 ####################################### main.cpp ####################################### #include <iostream> #include <fstream> #include <vector> #include <sstream> #include <cstring> using namespace std; // 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 converts a comma separated string to tokens // it returns all tokens in form of a vector vector<string> tokens(string s) { vector<string> result; // 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 result.push_back(trim(token)); } // if last token is now not empty, push that too on vector if(!s.empty()) { result.push_back(trim(s)); } return result; } // this method parses the token which is in format: // F=<firstName> // L=<lastName> // V=<Votes> void parseToken(string s) { if(s[0] == 'F') { cout << "FirstName: " << s.substr(2) << endl; } else if(s[0] == 'L') { cout << "LastName: " << s.substr(2) << endl; } else if(s[0] == 'V') { cout << "Votes: " << s.substr(2) << endl; } } // method to read the file and print the votes void readFile() { // variable to read the line string line; // File variable to read input file ifstream infile; // open the file to read infile.open("elections.txt"); // while input file has lines 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. for(string t: tokens(line)) { parseToken(t); } } cout << endl; } // close the file we opened infile.close(); } int main() { readFile(); }
************************************************** 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.