0

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;
    }
 
}
user438383
  • 5,716
  • 8
  • 28
  • 43
  • If you know the number of characters in the first line, you can seek. Otherwise, you need to read and discard all data up to and including the first line terminator. – William Pursell Oct 03 '21 at 13:19
  • 1
    Have a look at [std::istream::ignore()](https://en.cppreference.com/w/cpp/io/basic_istream/ignore). The sample code shows exactly what you could use as well (to skip the first line): `ifs.ignore(std::numeric_limits::max(), '\n');`. – Scheff's Cat Oct 03 '21 at 13:23
  • Btw. please, don't use `while(! ifs.eof()){` - it's broken code: [SO: Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/q/5605125/7478597). – Scheff's Cat Oct 03 '21 at 13:25
  • It's hard to sort the file but easy to sort your array in memory with [std::sort()](https://en.cppreference.com/w/cpp/algorithm/sort). Thus, if you really need to sort the file then: 1. read the complete file into memory, 2. sort the contents in memory, 3. write the contents back to the file. Thereby, you even can easily overwrite the file without changing its name. (In this case, it might be worth to read the first line into a `std::string` instead of skipping it, and to rewrite it into the new file. Otherwise, it will be lost after file has re-written.) – Scheff's Cat Oct 03 '21 at 13:27
  • @Scheff'sCat thanks for the info, but what can i use as an alternative for the while(! ifs.eof())? thanks again – peanut butter Oct 03 '21 at 13:34
  • The blue text parts of my comments (at least, they are blue in my browser) are links. Follow the link [SO: Why is iostream::eof inside a loop condition (i.e. while (!stream.eof())) considered wrong?](https://stackoverflow.com/q/5605125/7478597), and you will find more than one answer. (I've to add nothing else.) ;-) – Scheff's Cat Oct 03 '21 at 13:37

1 Answers1

1

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.

Ross Aylen
  • 66
  • 5
  • wow this is a life saver! However, I am afraid I cannot use the sort function because we really need to use some sorting algorithm. But anyway, this is such a big help for me. A new knowledge indeed since we haven't use vectors yet. – peanut butter Oct 04 '21 at 02:46
  • No worries mate, glad I could help! – Ross Aylen Oct 04 '21 at 12:33