Question

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 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 to use find(), so please explain how to use find().*

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

find() : It will find the position of a given character for the given string.

substr(): It will extract string from start index to end index.

By using find, we will find comma indexes in the line. Then we will extract it and then we will print data.

readFile function will read the line by line and validating line, if it is ok, then It will assign into a structure array.

Code:

#include <iostream>

#include <fstream>

#include <sstream>

#include <string>

using namespace std;

#define MAX_SIZE 20

struct Candidate {

string fname;

string lname;

int votes;

};

string ltrim(string str){

int i=0;

while(str[i]==' ')

i++;

str = str.substr(i);

return str;

}

int readFile(Candidate candidates[]) {

string line;

ifstream infile;

infile.open("elections.txt");

string fname, lname, votes;

int n = 0;

while (!infile.eof()) {

getline(infile, line);

if(line!=""){

int comma1 = line.find(",");

if(comma1!=string::npos){

int comma2 = line.find(",", comma1+1);

if(comma2!=string::npos){

fname = ltrim(line.substr(0, comma1));

lname = ltrim(line.substr(comma1+1, comma2-comma1-1));

votes = ltrim(line.substr(comma2+1));

candidates[n].fname = fname.substr(2);

candidates[n].lname = lname.substr(2);

candidates[n].votes = stoi(votes.substr(2));

n++;

}

}

}

}

infile.close();

return n;

}

int main(){

Candidate candidates[MAX_SIZE];

int n = readFile(candidates);

cout<<"FName\tLName\tVotes"<<endl;

cout<<"-----------------------------"<<endl;

for(int i=0; i<n; i++){

cout<<candidates[i].fname<<"\t"<<candidates[i].lname<<"\t"<<candidates[i].votes<<endl;

}

}

Add a comment
Know the answer?
Add Answer to:
Validating Input file, So for my C++ assignment, my professor says we need to validate the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? PLEASE READ CAREFULLY. MAX_SIZE...

    INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? PLEASE READ CAREFULLY. MAX_SIZE is size of array When iterating over the candidates’ list, do not iterate over the entire array, but just over the records where data is filled in 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,...

  • My code doesn't output correctly using a .txt file. How do I clean this up so...

    My code doesn't output correctly using a .txt file. How do I clean this up so it posts correctly? ----------------------------------------------- CODE ---------------------------------------------- #include <iostream> #include <string> #include <fstream> #include <iomanip> #include <fstream> using namespace std; struct Customer {    int accountNumber;    string customerFullName;    string customerEmail;    double accountBalance; }; void sortDesc(Customer* customerArray, int size); void print(Customer customerArray[], int size); void print(Customer customerArray[], int size) {    cout << fixed << setprecision(2);    for (int i = 0; i...

  • Hi! 1. I need some help with sorting string in a text file. My goal is...

    Hi! 1. I need some help with sorting string in a text file. My goal is to 1 shift left strings for string.length time. I was able to do that successfully. However, I am also trying to sort, using insertion sort , the resulting shifts. I store all shifts in a vector (or is it better to use an array?!) , and try to sort them that way, but my output is just the shifted strings but not sorted. Can...

  • Please read this comment: This is the only file given by my professor and so can...

    Please read this comment: This is the only file given by my professor and so can you please help me to write a python program. You can use any test file and post the output with the code. The output might be different then above but please help me to get started with the PYTHON code. Thank you in advance You are going to write a Python program that represents a command line version of a hexadecimal editor. This program...

  • Write a simple telephone directory program in C++ that looks up phone numbers in a file...

    Write a simple telephone directory program in C++ that looks up phone numbers in a file containing a list of names and phone numbers. The user should be prompted to enter a first name and last name, and the program then outputs the corresponding number, or indicates that the name isn't in the directory. After each lookup, the program should ask the user whether they want to look up another number, and then either repeat the process or exit the...

  • I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt...

    I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt Project#3 is an extension of the concepts and tasks of Lab#3. You will again read the dictionary file and resize the array as needed to store the words. Project#3 will require you to update a frequency counter of word lengths every time a word is read from the dictionary into the wordList. When your program is finished this histogram array will contain the following:...

  • Input hello, I need help completing my assignment for java,Use the following test data for input...

    Input hello, I need help completing my assignment for java,Use the following test data for input and fill in missing code, and please screenshot output. 1, John Adam, 3, 93, 91, 100, Letter 2, Raymond Woo, 3, 65, 68, 63, Letter 3, Rick Smith, 3, 50, 58, 53, Letter 4, Ray Bartlett, 3, 62, 64, 69, Letter 5, Mary Russell, 3, 93, 90, 98, Letter 6, Andy Wong, 3, 89,88,84, Letter 7, Jay Russell, 3, 71,73,78, Letter 8, Jimmie Wong,...

  • For the following task, I have written code in C and need help in determining the...

    For the following task, I have written code in C and need help in determining the cause(s) of a segmentation fault which occurs when run. **It prints the message on line 47 "printf("Reading the input file and writing data to output file simultaneously..."); then results in a segmentation fault (core dumped) I am using mobaXterm v11.0 (GNU nano 2.0.9) CSV (comma-separated values) is a popular file format to store tabular kind of data. Each record is in a separate line...

  • i have two issues that i need help. 1- that in case three and two only...

    i have two issues that i need help. 1- that in case three and two only one line is being read from the file 2- my case 4 is not working correctly as it should as the output is only " Enter 1 for Trump, 2 for Warren:" #include <iostream> #include <cctype> // For the letter checking functions #include <fstream> // For file input #include <iomanip> // For setw #include <ctime> #include <cstdlib> // For exit and abs #include <errno.h>...

  • NEED HELP BEING SOLVED IN C++! USE ORIGINAL CODE TO ADD TO AS WELL! #include<iostream> using...

    NEED HELP BEING SOLVED IN C++! USE ORIGINAL CODE TO ADD TO AS WELL! #include<iostream> using namespace std; int main(){    for (int i = 0; i < 7; i++) { int x,y; char opr; cin>>x>>y>>opr;    switch(opr) { case '+': cout<<x<<" + "<<y<<" = "<<x+y; break; case '-': cout<<x<<" - "<<y<<" = "<<x-y; break; case '*': cout<<x<<" * "<<y<<" = "<<x*y; break; case '/': if(y == 0) cout<<x<<" / "<<y<<" = "<<"ERROR"; else cout<<x<<" / "<<y<<" = "<<x/y<<"R"<<x%y; break;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT