0

How do I read lines from a text file and store them into an array. For example I have a text file with 45 different lines.

My attempt:

int main()
{
    int a[45];
    ifstream myfile("enroll_assg.txt");

    if(!myfile){
        cout << "Error opening file" << endl;
        return -1;
    }

    if(myfile.is_open()){
        while(getline(myfile, a, '\n') == 1){

        }
    }

}

I am supposed to make a hash table with this. I get a error in the while loop at the getline(). It is saying "no instance of overloaded functions".

I have 45 lines and each line has white space. Each line looks like:

9650376 George Jones CS 4.5 ... ...

Student.cpp:

template <typename Comparable>
Student<Comparable>::Student()
{
    // Add your code
    this->fname = fname;
    this->lname = lname;
    this->gpa = gpa;
    this->department = department;
    this->id = id;
    this->bucketId = bucketId;
}

The student.cpp also has the getters and setter for the variables.

  • `int a[45];` allocates space for 45 integer variables. How is that going to hold lines of text? Required reading: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Retired Ninja Nov 29 '21 at 05:19
  • @RetiredNinja I changed it and am now using if(my.is_open()){ instead. How can I do this i am new to this and lost at the moment. – codingisfun543 Nov 29 '21 at 05:36
  • Solving one problem at a time in order of appearance. The first problem was: *int a[45]; allocates space for 45 integer variables. How is that going to hold lines of text?* Have you solved it? – n. m. could be an AI Nov 29 '21 at 05:49
  • Curious where you got the idea that `std::getline` returns a 1 on success. – Retired Ninja Nov 29 '21 at 06:03

1 Answers1

0

A better alternative would be to use std::vector as shown below. The advantage of using a vector over built in array is that since a vector is a dynamically sized container, we don't need to know beforehand how many students/entries will be there in the input file. We can add that information dynamically.

The below shown program uses struct to represent a given Student and it also used a std::vector. You can use this program as a reference(starting point). It reads student information from the input text file and store that information in a vector of Student.

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
//this class represents a Student
class Student
{
    public:
        std::string firstName, lastName, courseName ;
        unsigned long id = 0;
        float marks = 0;
    
};

int main()
{
   std::ifstream inputFile("input.txt");
   std::string line;
   std::vector<Student> myVec;//create a vector of Student objects
   if(inputFile)
   {
       while(std::getline(inputFile, line))
       {
           Student studentObject;
           std::istringstream ss(line);
           //read the id 
           ss >> studentObject.id;
           
           //read the firstname 
           ss >> studentObject.firstName;
           
           //read the lastname 
           ss >> studentObject.lastName;
           
           //read the courseName
           ss >> studentObject.courseName;
           
           //read the marks 
           ss >> studentObject.marks; 
           
           if(ss)//check if input succeded
           {
               myVec.emplace_back(studentObject);//add the studentObject into the vector
           }
       }
   }
   else 
   {
       std::cout<<"File cannot be opened"<<std::endl;
   }
   
   //lets print out the elements of the vecotr to confirm that all the students were correctly read 
   for(const Student &elem: myVec)
   {
       std::cout << elem.id << ": "<<elem.firstName<<" "<<elem.lastName<<" "<<elem.courseName<<" "<<elem.marks <<std::endl;
   }
    return 0;
}

The output of the above program can be seen here.

Jason
  • 36,170
  • 5
  • 26
  • 60
  • Do I need a struct Student when i already have a student.cpp class? The student.cpp class has a student() which assigns fname, lname, gpa, etc and also has getters and setters methods. – codingisfun543 Nov 29 '21 at 08:11
  • @codingisfun543 since you already have a student.cpp, you don't need to create a separate struct. But since in your question you did not mention anything about student.cpp we had no idea whether you have it or not. So i created my own struct. You can use your own student.cpp (class/struct) – Jason Nov 29 '21 at 08:16
  • so I changed it from std::vector myVec to std::vector myVec, and I changed it from student studentObject to string studentObject. Is that all fine? I am getting error on all the "ss >> ...". – codingisfun543 Nov 29 '21 at 08:25
  • and sorry for not explaining properly I didn't want to give a bunch of unnecessary info. – codingisfun543 Nov 29 '21 at 08:26
  • @codingisfun543 you must use `std::vector`. This is because we need a vector of `Student` objects and not a vector of `int` as you have. In your student.cpp do you have your own `struct` or `class`? You can use the struct that i gave above. – Jason Nov 29 '21 at 08:29
  • I edited the question and added part of the student.cpp class. – codingisfun543 Nov 29 '21 at 08:36
  • When I use i get a error saying "argument list for class template 'Student' is missing" – codingisfun543 Nov 29 '21 at 08:38
  • @codingisfun543 i checked your edited question and i think you're making your program unnecessarily complicated. There is no need to use a template. Just use the `struct Student` that i created. – Jason Nov 29 '21 at 08:39
  • I cannot use your struct because I am working off something. SO it is required that I use the student.cpp class. – codingisfun543 Nov 30 '21 at 21:04
  • @codingisfun543 I have changed the `struct` to `class` in my code. Check it out. FYI, `struct` and `class` are the same thing, the only difference between them is the default access mode. – Jason Dec 01 '21 at 05:00