zach fisherman
zach fisherman

Reputation: 1

issues with getline overload function

I am having issues with cin.getline(). When I put it into the below program, it is giving an error:

no instance of overload function (ect) matches the argument list

I am also having an issue with my variable nx. The compiler says that it cannot be a constant, but I'm not sure how it is.

Here is my code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

//create a class for one book
class Book
{
    //member variables
public:
    string title;
    string author;
    int year;

    //function to print the book details
    void print()
    {
        cout << "Title: " << title << endl;
        cout << "Authot: " << author << endl;
        cout << "Year: " << year << endl;
    }
};

int main()
{
    std::string filename, temp;

    //prompt the user to enter the file name
    cout << "Enter filename: ";
    cin >> filename;

    //open the file
    fstream file;
    file.open(filename.c_str());

    int nx;
    //read the first line of the file to know the number of books
    file >> nx;
    //move to the next line of the file
    std::cin.getline(file, temp);

    //allocate an array of n Book
    
    Book books[nx];
   
    //read the data for n books from the file
    for (int i = 0; i < nx; i++)
    {
        //read file and initialize the books array
        std::cin.getline(file, books[i].title);
        std::cin.getline(file, books[i].author);
        file >> books[i].year;
        //move to the next line of the file
        getline(file, temp);
    }

    //Display the output
    cout << "Books found: " << nx << endl;
    for (int i = 0; i < nx; i++)
    {
        cout << "\nBook " << i + 1 << ":" << endl;
        cout << "Title: " << books[i].title << endl;
        cout << "Author: " << books[i].author << endl;
        cout << "Year: " << books[i].year << endl;
    }
}

Upvotes: 0

Views: 321

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595467

The 2-parameter std::cin.getline() method does not take a std::fstream or a std::string as parameters. It takes a char* and a std::streamsize instead, as it is meant to fill a char[] buffer. To fill a std::string from a stream, you need to use the standalone std::getline() function instead, eg:

std::getline(file, temp);
...
std::getline(file, books[i].title);
...

As for the "constant" error relating to your nx variable, the problem is on this line:

Book books[nx];

You are trying to declare a fixed-sized array using a size that is not known until runtime. You can't do that in standard C++, an array's size must be known at compile-time instead. Otherwise, you have to use new[] or std::vector to allocate the array dynamically at runtime instead, eg:

Book* books = new Book[nx];
...
delete[] books;

Or:

#include <vector>

std::vector<Book> books(nx);

Demo


On a side note:

When you want to discard content from an istream up to the next '\n' character, you can use the stream's ignore() method instead of std::getline(), that way you are not wasting memory unnecessarily for an unused std::string, eg:

#include <limits>

//move to the next line of the file
//std::getline(file, temp);
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

Also, you have defined a print() method in Book, but you are not actually using it. Your final display loop can do this instead:

//Display the output
cout << "Books found: " << nx << endl;
for (int i = 0; i < nx; i++)
{
    cout << "\nBook " << i + 1 << ":" << endl;
    /*
    cout << "Title: " << books[i].title << endl;
    cout << "Author: " << books[i].author << endl;
    cout << "Year: " << books[i].year << endl;
    */
    books[i].print();
}

Upvotes: 1

Related Questions