Home > Net >  How to skip reading the first line of file?
How to skip reading the first line of file?

Time:10-04

How can I ignore the first line of the text file and start at the second line when I called it in the code? I was wondering how. Also, how can I sort the file according to first name, last name and grade? I just have the first name sorted but not the last name and grade accordingly. If you have any idea, I hope you can share it with me. Thanks for the help! Here's my code:

#include <iostream>
#include <fstream>
using namespace std;
  
struct studentRecord{
    string lastname;
    string firstname;
    string grade;
};
 
int main(){

    ifstream  ifs("student-file.txt");
    string lastname, firstname, grade, key;
    
    studentRecord records[20];
    if(ifs.fail()) {
        cout << "Error opening student records file" <<endl;
        exit(1);
     }
    int i = 0;
    while(! ifs.eof()){
        ifs >> lastname >> firstname >> grade;

        records[i].lastname = lastname;
        records[i].firstname = firstname;
        records[i].grade = grade;
        i  ;
    }  
 
    for (int a = 1, b = 0; a < 20; a  ) {
        key = records[a].firstname ;
        b = a-1;
                
        while (b >= 0 && records[b].firstname > key) {
            records[b 1].firstname = records[b].firstname;
            b--;
        }
        records[b 1].firstname = key;
    }

    for (int k = 0; k < 20; k  ) {
        cout << "\n\t" << records[k].firstname << "\t"<< records[k].lastname << "\t" << records[k].grade;
    }
 
}

CodePudding user response:

When I saw this post it reminded me of a similar task completed at uni. I have rewritten your code to perform the same task but using classes instead of structs. I have also included a way to sort the vector by using the function here.

I have included the "ignore first line" method @Scheff's Cat mentioned.

Here it is:

#include <iostream>
#include <fstream>
#include <sstream>
#include <limits>
#include <string>
#include <vector>

using namespace std;

class studentrecord{
    string firstname, lastname, grade;        
    public:
    studentrecord(string firstname, string lastname, string grade){
        this -> firstname = firstname;
        this -> lastname = lastname;
        this -> grade = grade;
    }

    friend ostream& operator<<(ostream& os, const studentrecord& studentrecord) {
        os << "\n\t" << studentrecord.firstname << "\t" << studentrecord.lastname << "\t" << studentrecord.grade;
        return os;
    }
};

void displayRecords(vector <studentrecord*> records){
    for(int i = 0; i < records.size(); i  ){
        cout << *records[i];
    }
}

int main(){
    //read in file
    ifstream infile;
    infile.open("student-file.txt");
    infile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    if (!infile.is_open()){
            cout << "Error opening student records file" <<endl;
            exit(1);
    }
    vector <studentrecord*> records;
    string firstname, lastname, grade;
    while (infile >> firstname >> lastname >> grade;) {
        records.push_back(new studentrecord(firstname, lastname, grade));
    }

    displayRecords(records);

    return 0;
}

To sort the vector so that it prints in order of either first name, last name or grade I used the following functions:

bool sortfirstname(studentrecord* A, studentrecord* B) {
    return (A->getfirstname() < B->getfirstname());
}

bool sortlastname(studentrecord* A, studentrecord* B) {
    return (A->getlastname() < B->getlastname());
}

bool sortgrade(studentrecord* A, studentrecord* B) {
    return (A->getgrade() < B->getgrade());
}

sort(records.begin(), records.end(), (sortfirstname));
sort(records.begin(), records.end(), sortlastname);
sort(records.begin(), records.end(), sortgrade);

If you wanted to sort by first name you would call the sort(records.begin(), records.end(), (sortfirstname)); function and then the displayrecords() function.

The advantage of using classes stored in vectors is that you don't have to state the size of the vector containing the details about students since you can keep adding information to the end of the vector using the vector.push_back() function. It also makes sorting the data contained easier.

If anything isn't clear, let me know and I can give you a hand.

  • Related