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.
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
The main idea is that you read the input line by line and process each line accordingly
Some information is missing like
what to do with test cases like
F=abc def,L=efg hij,V=1234
I have given a basic idea and you can modify accordingly
while (cin.getline(inputLine, 256, '\n'))
{
bool validString = true;
int n = strlen(inputLine);
if (n == 0)
continue;
{
cout << "The first Name stored in the array is: " ;
cout << it << endl;
}
// visualisation for the input string
// cout << n << endl ;
// for(int i = 0 ; i < n ; i++){
// cout << inputLine[i] ;
// }
// cout << endl ;
int i = 0;
string fname, lname;
int votes = 0;
while (i < n && isspace(inputLine[i]))
i++;
if (inputLine[i] == 'F')
{
i++ ;
while (i < n && isspace(inputLine[i]))
i++;
if (inputLine[i] != '=')
{
validString = false;
}
}
else
{
validString = false;
}
i++;
while (i < n && isspace(inputLine[i]))
i++;
int sp = i;
while (i < n && isalpha(inputLine[i]) &&
inputLine[i] != ',')
{
i++;
}
for (int j = sp; j < i; j++)
{
fname += inputLine[j];
}
// cout << fname << endl ;
if (validString)
{
firstName.push_back(fname);
}
}
for (auto it : firstName)
{
cout << "The first Name stored in the array is: " ;
cout << it << endl;
}
This is only for the first name
You can easily extend this for last name and votes.
Remeber to use stoi to store the number of votes
as integers
Another advanced Idea is to store the position of commas in a
vector and then if their number is greater than 2 you return
invalid string
else copy the input string into 3 different segments
for(int i = 0 ; i < positionOfCommaInString[0] ; i++){
fname += inputLine[i] ;
}
for (int i = positionOfCommaInString[0] ; i <
positionOfCommaInString[1] ; i++)
{
lname += inputLine[i];
}
for (int i = positionOfCommaInString[1] ; i < n ; i++)
{
votes += inputLine[i];
}
and don't put the character into string if isspace(inputLine[i])
return true
Construction for postitonOfCommaInString
int n = strlen(inputLine) ;
for(int i = 0 ; i < n ; i++){
if(inputLine[i] == ',') positionOfCommaInString.push_back(i)
;
}