I am trying to write two programs to work in conjunction. The first is a bibliography program that writes book info to a file; That's ok. Here's what I did:
#include <string>
#include <fstream>
#include <vector>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <iterator>
#include <vector> // vector class-template definition#include <algorithm> // copy algorithm
using namespace std;
class BookInfo
{
private:
char *author;
char *title;
char *publisher;
char *isbn;
char *date;
public:
BookInfo(){}
~BookInfo(){}
vector <std::string> bookVector;
void printBook()
{
cout << "\n Author : " << bookVector[0];
cout << "\n Title : " << bookVector[1];
cout << "\n Publisher : " << bookVector[2];
cout << "\n ISBN : " << bookVector[3];
cout << "\n Date : " << bookVector[4];
cout << endl;
}
void setAuthor()
{
char temp[50];
cout << " Please enter author: ";
cin.getline(temp, 50);
author = temp;
bookVector.push_back (author);
}
void setTitle()
{
char temp[50];
cout << " Please enter title: ";
cin.getline(temp, 50);
title=temp;
bookVector.push_back (title);
}
void setPublisher()
{
char temp[50];
cout << " Please enter publisher: ";
cin.getline(temp, 50);
publisher = temp;
bookVector.push_back (publisher);
}
void setIsbn()
{
char temp [6];
cout << " Please enter ISBN in format XXXXX: ";
cin.getline(temp, 6);
isbn = temp;
bookVector.push_back (isbn);
}
void setDate()
{
char temp[11];
cout << " Please enter the date in format mm/dd/yyyy: ";
cin.getline(temp, 11);
date = temp;
bookVector.push_back (date);
}
void write()
{
std::fstream biblio ("biblio.txt", std::ios_base::out|std::ios_base::app);
for (int y=0; y < bookVector.size() ; y++ )
{
biblio << bookVector[y] << ' ';
}
biblio << "\n";
}
};
void Clear_Screen(void);
void Flush(void);
int main()
{
cout << "Welcome to the Bibliography Program\n\n";
while (true)
{
char ans;
cout << "\nWould you like to enter book info? (Y/N)\n";
cin >> ans;
if (ans=='y' || ans =='Y')
{
Clear_Screen();
cin.get();
BookInfo book;
book.setAuthor();
book.setTitle();
book.setPublisher();
book.setIsbn();
book.setDate();
book.write();
Clear_Screen();
book.printBook();
Flush();
}
else if (ans =='n' || ans =='N')
{
cout << "\n\n Thank you for using the program!" << endl;
Flush();
cout << "\n\n Press any key to terminate . . ." << endl;
getchar();
return 0;
}
else {
cout << "Invalid entry! Please re-try;\n";
ans ='y';
}
}
}
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);
}
Now I could use some help with the second part. I'm supposed to "Write a second program that prompts the user for partial information about a book and then supplies the rest from the bibliography file."I tried putting it into a vector and searching through that, but I can't get it to stop at the "\n". What am I doing wrong? and how can I get the info before the pointer?
#include <string>
#include <fstream>
#include <vector>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
int main()
{
int count;
string identifier;
vector<string>::iterator p;
cout << "Enter partial info:\n";
cin >> identifier;
std::vector<std::string> bookVector;
std::string line;
std::fstream books ("biblio.txt", std::ios_base::in);
while (books >> line)
{
bookVector.push_back (line);
}
p = find(bookVector.begin(), bookVector.end(), identifier);
do
{
cout << *p++;
}
while(*p != "\n");
cout << endl;
system ("pause");
}