I am trying to read a file with a list of titles and authors, and I need to be able to ignore the newline character that separates each line in the file.
For example, my .txt file might have a list like this:
The Selfish Gene
Richard Dawkins
A Brave New World
Aldous Huxley
The Sun Also Rises
Ernest Hemingway
I have to use parallel arrays to store this info, and then be able to format the data like so:
The Selfish Gene (Richard Dawkins)
I was trying to use getline
to read the data, but when I go to format the title and author, I get this:
The Selfish Gene
(Richard Dawkins
)
How do I ignore the newline character when I read in the list from the file?
This is what I have so far:
int loadData(string pathname)
{
string bookTitle[100];
string bookAuthor[100];
ifstream inFile;
int count = -1; //count number of books
int i; //for variable
inFile.open(pathname.c_str());
{
for (i = 0; i < 100; i++)
{
if(inFile)
{
getline(inFile, bookTitle[i]);
getline(inFile, bookAuthor[i]);
count++;
}
}
inFile.close();
return count;
}
EDIT:
This is my output function:
void showall(int count)
{
int j; //access array up until the amount of books
for(j = 0; j < count; j++)
{
cout << bookTitle[j] << " (" << bookAuthor[j] << ")";
cout << endl;
}
}
Am I doing something wrong here?