Hi,

I have to write an airline ticket program for my class. My instructor gave us an example of a similar program so I thought I would build and run it to help me understand it better but he seems to have given us code for a program that doesn't even build right. I can usually fix build errors by reading the build report but I've not been able to understand just what it wants from me. Can anyone help me out so I can move on to my actual assignment? Here's the code my instructor provided that I can't get to build.

/**
 *  BookRecord.h
*/

#ifndef _BOOK_RECORD_H_
#define _BOOK_RECORD_H_

#include <iostream>
#include <string>

using namespace std;

class BookRecord
{
public:
  BookRecord();
  BookRecord(const BookRecord &bookRecord);
  ~BookRecord();

  void setIsbn(string isbn);
  string getIsbn() const;

  void setAuthor(string author);
  string getAuthor() const;

  void setTitle(string title);
  string getTitle() const;

  void setYear(string year);
  string getYear() const;

  void setPublisher(string publisher);
  string getPublisher() const;

private:
  string isbn;
  string author;
  string title;
  string year;
  string publisher;
};

#endif
/**
 *  BookRecord.cpp
 */

#include "BookRecord.h"

BookRecord::BookRecord()
{
   // Currently does nothing ...
}

// Copy constructor
BookRecord::BookRecord(const BookRecord &bookRecord)
{
   isbn = bookRecord.isbn;
   author = bookRecord.author;
   title = bookRecord.title;
   year = bookRecord.year;
   publisher = bookRecord.publisher;
}

BookRecord::~BookRecord()
{
   // Currently does nothing ...
}

void BookRecord::setIsbn(string isbn)
{
   this->isbn = isbn;
}

string BookRecord::getIsbn() const
{
   return isbn;
}

void BookRecord::setAuthor(string author)
{
   this->author = author;
}

string BookRecord::getAuthor() const
{
   return author;
}

void BookRecord::setTitle(string title)
{
   this->title = title;
}

string BookRecord::getTitle() const
{
   return title;
}

void BookRecord::setYear(string year)
{
   this->year = year;
}

string BookRecord::getYear() const
{
   return year;
}

void BookRecord::setPublisher(string publisher)
{
   this->publisher = publisher;
}

string BookRecord::getPublisher() const
{
   return publisher;
}
/**
 *  Main.cpp
 */

#include <fstream>
#include <iostream>
#include <list>
#include <math.h>
#include <string.h>
#include <stdio.h>

#include "BookRecord.h"

using namespace std;

//
// Prototype declarations 
//
void menu();
void add_book();
void delete_book();
void display_book_inventory();

//
// Declare the book list and an STL linked list variable
// list <BookRecord> book_list;
// Main program.  Calls the functions to load the address book into 
// memory then launches the user menu.
//
int main(int argc, char **argv)
{
   menu();
      
   return 0;
}

// 
//  This function adds a record to the linked list.
//
void add_book()
{
   BookRecord book_record;
   string tmpstr;

   cout << endl << "** ADD BOOK TO INVENTORY **" << endl << endl;

   getline(cin, tmpstr);  // Ignore the first CR

   cout << "ISBN> ";
   getline(cin, tmpstr, '\n');
   book_record.setIsbn(tmpstr);

   cout << "Author> ";
   getline(cin, tmpstr, '\n');
   book_record.setAuthor(tmpstr);

   cout << "Title> ";
   getline(cin, tmpstr, '\n');
   book_record.setTitle(tmpstr);

   cout << "Year> ";
   getline(cin, tmpstr, '\n');
   book_record.setYear(tmpstr);

   cout << "Publisher> ";
   getline(cin, tmpstr, '\n');
   book_record.setPublisher(tmpstr);

   // Add to the linked list
   book_list.push_back(book_record);

   return;
}

// 
// This function deletes a book from the linked list based on the given
// ISBN.
//
void delete_book()
{
   cout << endl << "** DELETE BOOK FROM INVENTORY **" << endl << endl;
  
   string tmp_isbn_str;
   bool found_entry = false;
   BookRecord book_record;

   cout << "ISBN> ";
   cin >> tmp_isbn_str; 

   list<BookRecord>::iterator i;
   for (i = book_list.begin(); i != book_list.end(); ++i)
   {
      book_record = *i;

      if (book_record.getIsbn() == tmp_isbn_str)
      {
        found_entry = true;
        i = book_list.erase(i);
      }
   }
                                                                             
   if (found_entry)
      cout << "Entry deleted." << endl;
   else
      cout << "Entry not found." << endl;

   return;
}

// 
// This function displays all of the books that are contained in the 
// linked list.
//
void display_book_inventory()
{
   cout << endl << "** DISPLAY BOOK INVENTORY **" << endl << endl;

   string continue_display;
   BookRecord book_record;

   list<BookRecord>::iterator i;
   int j=0;
   for (i = book_list.begin(); i != book_list.end(); ++i, j++)
   {
      book_record = *i;

      cout << "ISBN:        " << book_record.getIsbn() << endl;
      cout << "Author:      " << book_record.getAuthor() << endl;
      cout << "Title:       " << book_record.getTitle() << endl;
      cout << "Publisher:   " << book_record.getPublisher() << endl;
      cout << "---------------------------------------------" << endl;

      // Print the first 10 record, then ask the user if he/she wants
      // to continue displaying ...
      if (j != 0 && j % 10 == 0 )
      {
         cout << "Continue Display? (y,n) ";
         cin >> continue_display; 
         if (strncmp(continue_display.c_str(), "y", 2))
         {
            return;
         }

         // Print the separator bar again
         cout << "---------------------------------------------" << endl;
      }
   }

   return;
}

// 
// This function provides the main menu for the user, and display the // options and accepts user input.
//
void menu()
{
   char option[3];
   bool exit_program = false;
  
   while (!exit_program)
   {
      // Allow the user to select options to add, delete, modify, and 
      // list all data in the address book.
      cout << endl << "** BOOK INVENTORY V1.0 MAIN MENU **" << endl << endl;
      cout << "   1. Add entry" << endl;
      cout << "   2. Delete entry" << endl;
      cout << "   3. Display entries" << endl;
      cout << endl << "   99. Exit program" << endl << endl;
   
      cout << "Option> ";
      cin >> option;
   
      // Process user entry
      switch (atoi(option))
      {
          case 1:
            add_book();
            break; 
   
          case 2:
            delete_book();
            break; 

          case 3:
            display_book_inventory();
            break; 
 
          // Save address book and then exit program
          case 99:
            exit_program = true;
            break;

          default:
            cerr << "Error: unknown option!" << endl;
            break;
      }
   }

   return;    
}

Here's the build report if that might make things any clearer:

1>------ Build started: Project: Book Program, Configuration: Debug Win32 ------
1>Compiling...
1>Main.cpp
1>c:\users\bj\school\data structures and algorithms\book program\book program\main.cpp(74) : error C2065: 'book_list' : undeclared identifier
1>c:\users\bj\school\data structures and algorithms\book program\book program\main.cpp(74) : error C2228: left of '.push_back' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\bj\school\data structures and algorithms\book program\book program\main.cpp(95) : error C2065: 'book_list' : undeclared identifier
1>c:\users\bj\school\data structures and algorithms\book program\book program\main.cpp(95) : error C2228: left of '.begin' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\bj\school\data structures and algorithms\book program\book program\main.cpp(95) : error C2065: 'book_list' : undeclared identifier
1>c:\users\bj\school\data structures and algorithms\book program\book program\main.cpp(95) : error C2228: left of '.end' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\bj\school\data structures and algorithms\book program\book program\main.cpp(102) : error C2065: 'book_list' : undeclared identifier
1>c:\users\bj\school\data structures and algorithms\book program\book program\main.cpp(102) : error C2228: left of '.erase' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\bj\school\data structures and algorithms\book program\book program\main.cpp(127) : error C2065: 'book_list' : undeclared identifier
1>c:\users\bj\school\data structures and algorithms\book program\book program\main.cpp(127) : error C2228: left of '.begin' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\bj\school\data structures and algorithms\book program\book program\main.cpp(127) : error C2065: 'book_list' : undeclared identifier
1>c:\users\bj\school\data structures and algorithms\book program\book program\main.cpp(127) : error C2228: left of '.end' must have class/struct/union
1> type is ''unknown-type''
1>Build log was saved at "file://c:\Users\BJ\School\Data Structures and Algorithms\Book Program\Book Program\Debug\BuildLog.htm"
1>Book Program - 12 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Recommended Answers

All 9 Replies

You need to make sure that you have all the files in the same project, or if you are compiling on the command line put BookRecord.cpp first.

As for devC++ the version of g++ it uses is too out of date and the IDE itself is no longer being developed. You should be fine with whatever compiler you have now, we just need to figure out why it's not liking your other CPP file.

commented: Jonsca always tries to help and always seems to give great advice!!! Awesome daniweb member!!! +1

>> 'book_list' : undeclared identifier

In Main.cpp line #26, the missing variable has been commented out, so you need to remove
the comments to have the book_list declared.

...
// Declare the book list and an STL linked list variable
list <BookRecord> book_list;
...

On a side note, does your instructor consider the following a good/safe practice?

...
char option[3];

// Get some input, hoping that the input will fit into 'option' 
// without an out-of-bounds write.
cin >> option;
commented: mitrmkar's respnse to my post was the one that solved it! Thanks a bunch mitrmkar!!! +1

so you need to remove
the comments to have the book_list declared.

mit, It wouldn't let me +rep you (since I did yesterday) but yeah.

Yeesh (slaps my forehead), sorry Bobbie Jean.

what compiler are you using? If it's Visual C++, you'll get nowhere with that code; use either Code::Blocks, or, what I use / recommend, Dev-C++:

http://prdownload.berlios.de/codeblocks/codeblocks-10.05mingw-setup.exe //code::blocks
http://prdownloads.sourceforge.net/dev-cpp/devcpp-4.9.9.2_setup.exe //dev-c++, prefered

Hi,

I am still a bit new at programming but I was wondering, why does the compiler even matter? I mean, if you write/have a working C++ code, shouldn't it build and run with any compiler that handles C++ code? I don't mean that disrespectfully but I was honestly wondering. Besides that, based on someone else's response, I was able to get it to build and run just fine and I AM using Visual C++. So I was just wondering what you know that makes a difference in the compiler that I use.

You need to make sure that you have all the files in the same project, or if you are compiling on the command line put BookRecord.cpp first.

As for devC++ the version of g++ it uses is too out of date and the IDE itself is no longer being developed. You should be fine with whatever compiler you have now, we just need to figure out why it's not liking your other CPP file.

Hey Jonsca!

You never fail to try to help! Thanks!!! It seems that my compiler IS fine because I got it sorted out now. Thanks for the response!

>> 'book_list' : undeclared identifier

In Main.cpp line #26, the missing variable has been commented out, so you need to remove
the comments to have the book_list declared.

...
// Declare the book list and an STL linked list variable
list <BookRecord> book_list;
...

On a side note, does your instructor consider the following a good/safe practice?

...
char option[3];

// Get some input, hoping that the input will fit into 'option' 
// without an out-of-bounds write.
cin >> option;

Hi,

Thank you so much!!! I (obviously) didn't even notice/catch that. I guess sometimes it just takes another pair of eyes to see something. That worked perfectly!!!

As far as what my instructor considers good/safe practices, I have no idea! Just for my knowledge, if you know a better/safer way (and if you don't mind explaining why) I'd really love to know about it. I'm VERY inexperienced at programming so I don't always know/am taught the best methods. Sometimes I think my instructors show us harder things just so that the easier/better way is...well, easier but they don't usually go back and say something like, "Now, it is actually better/easier to do...'x'". So I feel that I don't always get the best method of doing things from my instructors. So any advice you have to offer, I am all ears!!! :)

mit, It wouldn't let me +rep you (since I did yesterday) but yeah.

Yeesh (slaps my forehead), sorry Bobbie Jean.

Jonsca,

Hey again! Sorry for what? You mean because someone else got around to telling me what was wrong with the code before you? If that's what you meant, don't sweat it! It actually usually IS you that is the first to give me some useful information/advice. It's a team effort and we're all here to help each other out, right? Well, I MAY have only helped one person so far and even then I'm not sure because I never saw another post from them regarding their initial post. Anyway, my point is that I've not been much help to anyone so far but I hope once I have more experience/confidence I can offer help. (Sheepish expression) I think I'm getting better but I CERTAINLY have not "arrived" yet, ya know? Anyway, you always try to help and I really appreciate it. Take care!

Bobbie Jean


Hey again! Sorry for what? You mean because someone else got around to telling me what was wrong with the code before you? If that's what you meant, don't sweat it!

It was more speaking to my having tunnel vision LOL

Anyway, you always try to help and I really appreciate it. Take care!

Never a problem!

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.