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;
}
}