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

}

Solutions

Expert Solution

/************************************

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



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. This is a sample of the txt...
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...
C++ Assignment: For this assignment, we will model a checking account and savings account, arranging these...
C++ Assignment: For this assignment, we will model a checking account and savings account, arranging these two types of accounts within a simple class hierarchy beneath a base class. By creating a class hierarchy, we will avoid unnecessary code duplication across the derived classes and create an intuitive relationship between them. We will expand on this assignment in a future assignment. Create a class named Account, which will contain data and methods that would naturally be shared by any derived...
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,...
The input file Each line of the input file will contain a sentence with words separated...
The input file Each line of the input file will contain a sentence with words separated by one space. Read a line from the listed below  and use a StringTokenizer to extract the words from the line. The input file . Mary had a little lamb whose fl33ce was white as sn0w And everywhere that @Mary went the 1amb was sure to go. Read the above that contains a paragraph of words. Put all the words in an array, put the...
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...
Assignment in C programming class: read the text file line by line, and place each word,...
Assignment in C programming class: read the text file line by line, and place each word, in order, in an array of char strings. As you copy each word into the array, you will keep a running count of the number of words in the text file and convert the letters of each word to lower case. Assume tgat you will use no more than 1000 words in the text, and no more than the first 10 characters in a...
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 * #...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. ***DO NOT REPLY ANUNAGA.*** HOW TO DO?...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. ***DO NOT REPLY ANUNAGA.*** 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. YOU CAN ADD TO IT, BUT NOT CHANGE WHAT IS ALREADY THERE. THIS IS FOR A BASIC C++ LEVEL CLASS SO WE HAVE TO STICK TO BASIC C++ CODE. HOWEVER IT COULD BE WRONG...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT