Question

In: Computer Science

Checking Input File for correct line format. So for my C++ assignment, my professor says we...

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

Solutions

Expert Solution

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) ;
}


Related Solutions

Checking Input File for correct line format. So for my C++ assignment, my professor says we...
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...
Validating Input file, So for my C++ assignment, my professor says we need to validate the...
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...
My professor says that through social media we should be able to make contact and speak...
My professor says that through social media we should be able to make contact and speak with anybody on the planet. He says everybody knows somebody that knows somebody that knows somebody. He said that we should be able to reach out to a person by knowing less than 5 people in the chain. How would I go about meeting a person like Lebron James? What would the process look like? I am trying to see if he is correct...
Hi, so for my homework assignment, we are meant to change this function so that instead...
Hi, so for my homework assignment, we are meant to change this function so that instead of making a list from the array going up in an ascending order, making it be listed in descending order, meaning going from largest number to smallest number, the code I have now works for the ascending order, but how would I change this for it to go descending? Any help would be amazing, and please with a tutorial, thanks so much. def heapify(arr,...
this is my matlab code for class, my professor commented "why is the eps an input...
this is my matlab code for class, my professor commented "why is the eps an input when it is set inside the function and not specified as a variable? how do i fix? function[] = () %Declare Global Variables global KS; global KC; KC = 0; KS = 0; End = 0; while (End == 0) choice = questdlg('Choose a function', ... 'Fuction Menu', ... 'A','B','B'); switch choice; case 'A' Program = 'Start'; while strcmp(Program,'Start'); Choice = menu('Enter the Trigonometric...
In python can you fix the error in the line where it says message = input("Input...
In python can you fix the error in the line where it says message = input("Input a lowercase sentence: ") this is the error message I get after running the program and inputing a lowercase sentence Input a lowercase sentence:hello hi MM§MTraceback (most recent call last): MM§M File "client.py", line 14, in <module> MM§M message = input("Input a lowercase sentence:") MM§M File "<string>", line 1 MM§M hello hi MM§M ^ MM§MSyntaxError: unexpected EOF while parsing from socket import * #...
Exercise 2 — Matrix file The file format for the matrix will have the first line...
Exercise 2 — Matrix file The file format for the matrix will have the first line contain 2 integers, which are the number of rows and columns. After this line, repeatedly count from 0 to 9, filling in the matrix size conditions as specified above. For example: 3 4 0 1 2 3 4 5 6 7 8 9 0 1 Randomly create different sized arrays with random numbers. There should be a space between each number. You will be...
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. 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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT