I am trying to write a bibliography program that puts the book's info into a vector, then writes it to a file. Can someone tell me why my "biblio" file is blank when I'm done with this program?
Code:
#include <string>
#include <fstream>
#include <vector>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <iterator>
using namespace std;
class BookInfo
{
private:
char *author;
char *title;
char *publisher;
char *isbn;
char *date;
public:
BookInfo(){}
~BookInfo(){}
vector <std::string> bookInfo;
void printBook()
{
cout << "\n Author : " << author;
cout << "\n Title : " << title;
cout << "\n Publisher : " << publisher;
cout << "\n ISBN : " << isbn;
cout << "\n Date : " << date;
}
void setAuthor()
{
char temp[50];
cout << " Please enter author: ";
cin.getline(temp, 99);
int size = strlen(temp);
author = new char[size];
strncpy(author, temp, strlen(temp));
author[size] = '\0';
bookInfo.push_back (author);
}
void setTitle()
{
char temp[50];
cout << " Please enter title: ";
cin.getline(temp, 99);
int size = strlen(temp);
title = new char[size];
strncpy(title, temp, strlen(temp));
title[size] = '\0';
bookInfo.push_back (title);
}
void setPublisher()
{
char temp[50];
cout << " Please enter publisher: ";
cin.getline(temp, 99);
int size = strlen(temp);
publisher = new char[size];
strncpy(publisher, temp, strlen(temp));
publisher[size] = '\0';
bookInfo.push_back (publisher);
}
void setIsbn()
{
char temp[6];
cout << " Please enter ISBN in format XXXXX: ";
cin.getline(temp, 99);
int size = strlen(temp);
isbn = new char[size];
strncpy(isbn, temp, strlen(temp));
isbn[size] = '\0';
bookInfo.push_back (isbn);
}
void setDate()
{
char temp[11];
cout << " Please enter the date in format mm/dd/yyyy: ";
cin.getline(temp, 99);
int size = strlen(temp);
date = new char[size];
strncpy(date, temp, strlen(temp));
date[size] = '\0';
bookInfo.push_back (date);
write (bookInfo);
}
void write( vector<string>& bookfile)
{
std::fstream biblio ("biblio.txt", std::ios_base::out);
for (int y=0; y < bookInfo.size() ; y++ )
{
biblio << bookInfo[y] << ' ';
}
}
};
void Clear_Screen(void);
void Flush(void);
int main()
{
std::vector<std::string> bookInfo;
char ans='\n';
do
{
cout << "\n\nWould you like to enter book info? (Y or N): ";
cin >> ans;
Clear_Screen();
cin.get();
BookInfo book;
book.setAuthor();
book.setTitle();
book.setPublisher();
book.setIsbn();
book.setDate();
Clear_Screen();
book.printBook();
}
while ( ans=='y' || ans=='Y');
cout << "\n\n Thank you for using the program!" << endl;
Flush();
cout << "\n\n Press any key to terminate . . ." << endl;
getchar();
return 0;
}
void Clear_Screen(void)
{
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
void Flush(void)
{
int ch;
do
{
ch = getchar();
}
while (ch != EOF && ch != '\n');
clearerr(stdin);
}