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

So the first program creates biblio.txt and the second program is supposed to read from biblio.txt and fill in the missing data? What does biblio.txt look like? You may want to #include your class in your second program too. That would probably mean splitting that first file from one file to two files, having just the class in one of the files and the first main function in another, then having that second main function like it is, but #include the file with the class.

It looks to me like you have each book attribute on a different line. So it looks like, if you type in "Twain" and the goal is to come back with something like:

Mark Twain Adventures of Huck Finn Random House 12345 3/4/1865

all you are going to get back is "Twain" because all of these different words are in different elements of the vector.

If your intent is to have the entire long string above be one string in your bookVector, that's not the way your second program handles it. Each word will be a separate string.

I'm not sure how your first program writes it to the file.

I have modified the first program I believe this now writes all info on one book to its own string, then seperates it from the next with a newline. Am I right?

#include <string>
#include <fstream>
#include <vector>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <iterator> 
#include <vector>  

using namespace std;

class BookInfo
{
private:
   string author;
   string title;
   string publisher;
   string isbn;
   string 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 getInfo()
   {
      std::string temp;
      cout << "   Please enter author: ";
      getline(cin, temp);
      author = temp;
      bookVector.push_back (author);
      temp.clear();
      cout << "   Please enter title: ";
      getline(cin, temp);
      title=temp;
      bookVector.push_back (title);
      temp.clear();
      cout << "   Please enter publisher: ";
      getline(cin, temp);
      publisher = temp;
      bookVector.push_back (publisher);
      temp.clear();
      cout << "   Please enter ISBN in format XXXXX: ";
      getline(cin, temp);
      isbn = temp;
      bookVector.push_back (isbn);
      temp.clear();
	  cout << "   Please enter the date in format mm/dd/yyyy: ";
      getline(cin, temp);
      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.getInfo();
                  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);
}

Sorry if these qwuestions seem stupid, but I've never used any of this stuff before.

Create 4 files:

file #1: BookInfo.h
#include <string>
using namespace std;

class BookInfo
{
private:
   string author;
   string title;
   string publisher;
   string isbn;
   string date;
   
public:
   BookInfo();
   ~BookInfo();

   //accessors and mutators
   void setAuthor(string);
   void setTitle(string);
   void setPublisher(string);
   void setIsbn(string);
   void setDate(string);
   string getAuthor();
   string getTitle();
   string getPublisher();
   string getIsbn();
   string getDate();

   void printBook();
   void getInfo();
   void writeBookToFille();
   void readBookFromFile();
};

File #2: BookInfo.cpp
#include <iostream>
#include <fstream>

#include "BookInfo.h"

void BookInfo::getInfo()
{
      string temp;
      cout << "   Please enter author: ";
      getline(cin, temp);
      author = temp;
  
      cout << "   Please enter title: ";
      getline(cin, temp);
      title = temp;
            
      cout << "   Please enter publisher: ";
      getline(cin, temp);
      publisher = temp;
      
      //etc 
}   

//define other functions in BookInfo.h here in BookInfo.cpp

file #3: createBookFile.cpp
#include <iostream>
#include "BookInfo.h"

using namespace std;

int main()
{
    BookInfo book
    bool addAnotherBook = true;
    char ans;
    while(addAnotherBook)
    {
        cout << "\nWould you like to enter book info? (Y/N)\n";
         cin >> ans;
         if (ans=='y' || ans =='Y')
         {  
             book.getInfo();
             book.writeToFile();
          }
          else 
          {
              cout << "\n\n   Thank you for using the program!" << endl;
              addAnotherBook = false;
          }     
    }
    return 0;
}

file #4: mainProgram.cpp
#include <iostream>
#include <vector>
#include "BookInfo.h"
using namespace std;

int main()
{
   vector<BookInfo> books;
   
   //read information from file into books

   //declare iterators to search vector with
   vector<BookInfo>::iterator start = books.begin();
    vector<BookInfo>::iterator stop = books.end();

   string author;
   string title;
   string publisher;
   string isbn;
   string date;
   
   //get search field
   cout << "select what you want to search by by entering number of the type:
   cout << "1) Author" << '\n';
   cout << "2) Title" << '\n';
   //etc;
   int choice;
   
   //search vector by field
   switch(choice)
   {
       case 1:
           cout << "enter authors name" << '\n';
           cin >> author;
           for( ; start != stop; ++start)
           {
               if(start->author == author)
                  start->printBook();
           }
           break;
       case 2:
       //etc
    }  

   getch();     
   return 0;
}

As you can see this is just a general outline showing one protocol that could be used based on your posted code. You should write one file at a time, compiling as you go because there's lots of stuff to complete and who knows how many mistakes I've made along the way. File 3 is one program and File 4 is another, both of which use Files 1 and 2.

Wow. Thanks so much! I'll fool around with that for a while and see if I can figure it out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.