tags:

views:

94

answers:

2

I was wondering if there is a way to get a date without writing your own function. Something such as accessing the date set for the system. I tried

char dateStr[9];
_strdate(dateStr);

with the included time header, but I got compiler error ISO C++ forbids declaration of _strdate with no type. I thought _strdate was already declared and defined in the header so it wouldn't need a type definition. Could the problem be that I have those 2 lines inside of a struct? Are there any better way to store a date? Thanks.

#include "std_lib_facilities.h"

//Classes-----------------------------------------------------------------------

class Book{
public:
       Book(){}; // default constructor
       //operators
       friend ostream& operator<<(ostream& out, const Book& val);
       bool Book::operator==(const Book& check);
       //member functions
       string title();
       string author();
       string genre();
       int copyright();
       void ISBN();
       bool checkout();
private:
        string title_;
        string author_;
        string genre_;
        int copyright_;
        int ISBN1;
        int ISBN2;
        int ISBN3;
        char ISBN4;
        bool checkout_;
};

class Patron{
public:
       Patron(){}; //default constructor
       //member functions
       string name();
       int libnumber();
       double setfee();
private:
        string name_;
        int libnumber_;
        double libfees;
};

class Library{
public:
       vector<Book> books;
       vector<Patron> patrons;
       struct Transaction{
              Book book;
              Patron patron;};
       vector<Transaction> transactions;
};
// Error Function---------------------------------------------------------------

void _error(const string& s)
{
     cout << endl;
     cout << "Error: " << s << endl;
     cout << endl;
}

// Book Member Functions--------------------------------------------------------

string Book::title()
{
       cout << "Title: ";
       getline(cin,title_);
       cout << endl;
       return title_;
}

string Book::author()
{
       cout << "Author: ";
       getline(cin,author_);
       cout << endl;
       return author_;
}

string Book::genre()
{
       cout << "Genre(Fiction, Nonfiction, Periodical, Biography, Children): ";
       cin >> genre_;
       cout << endl;
       if((genre_!="Fiction")&&(genre_!="Nonfiction")&&(genre_!="Periodical")&&
          (genre_!="Biography")&&(genre_!="Children")) _error("Invalid genre.");
       else return genre_;
}

int Book::copyright()
{
    cout << "Copyright: ";
    cin >> copyright_;
    cout << endl;
    return copyright_;
}

void Book::ISBN()
{
     cout << "ISBN (4 entries. Use spaces): ";
     cin >> ISBN1 >> ISBN2 >> ISBN3 >> ISBN4;
     if((ISBN1<0) || (ISBN2<0) || (ISBN3<0) || (ISBN1>9) || (ISBN2>9) || (ISBN3)>9)
               _error("Must be single digit.");
     else if(!isdigit(ISBN4) && !isalpha(ISBN4))
               _error("Must be single digit or letter.");
     else{ cout << endl;
           return;}
}

bool Book::checkout()
{
     char check;
     cout << "Checked out?(Y or N): ";
     cin >> check;
     switch(check){
     case 'Y':
          cout << endl;
          return true;
          break;
     case 'N':
          cout << endl;
          return false;
          break;
     default: 
              _error("Must be Y or N.");}
}

// Patron Member Functions------------------------------------------------------

string Patron::name()
{
       cout << "Name: ";
       getline(cin,name_);
       cout << endl;
       return name_;
}

int Patron::libnumber()
{
    cout << "Libnumber: ";
    cin >> libnumber_;
    cout << endl;
    return libnumber_;
}

double Patron::setfee()
{
       cout << "Fee due: ";
       cin >> libfees;
       cout << endl;
       return libfees;
}

// Patron Helper Functions------------------------------------------------------

bool isfee()
{
     char isfee_;
     cout << "Does patron have fee due?(Y or N): ";
     cin >> isfee_;
     cout << endl;
     if((isfee_!='Y') && (isfee_!='N')) _error("Must use Y or N.");
     else return(isfee_=='Y');
}

// Operator Overloads-----------------------------------------------------------

ostream& operator<<(ostream& out, const Book& val){
         out << "Title: " << val.title_ << endl;
         out << "Author: " << val.author_ << endl;
         out << "ISBN: " << val.ISBN1 << "-" << val.ISBN2 << "-" << val.ISBN3 << "-" << val.ISBN4 << endl;
         out << endl;
         return out;}

bool Book::operator==(const Book& check){
     return((ISBN1 == check.ISBN1) && (ISBN2 == check.ISBN2) && (ISBN3 == check.ISBN3)
             && (ISBN4 == check.ISBN4));}

// Main Helpers-----------------------------------------------------------------

void runBook()
{
     Library bookstore;
     char notfinished;
     bool bookfinished = false;
     while(!bookfinished)
        {
          Book book;
          book.title();
          book.author();
          book.genre();
          book.copyright();
          book.ISBN();
          book.checkout();
          bookstore.books.push_back(book);
          cout << "Do you wish to store another book?(Y or N): ";
          cin >> notfinished;
          if(notfinished == 'Y'){ 
                         cin.ignore();
                         cout << endl;}
          else if(notfinished == 'N'){
                                       bookfinished = true;
                                       cout << endl;}
          else _error("Must be Y or N");
          }
}

void runPatron()
{
     Library patronstore;
     bool patronfinished = false;
     char notfinished;
     while(!patronfinished)
    {
      Patron patron;
      patron.name();
      patron.libnumber();
      if(isfee()){
                  patron.setfee();}
      cin.ignore();
      runBook(); // Call book function
      patronstore.patrons.push_back(patron);
      cout << "Is there another patron?(Y or N): ";
        cin >> notfinished;
        if(notfinished == 'Y'){ 
                         cin.ignore();
                         cout << endl;}
          else if(notfinished == 'N') patronfinished = true;
          else _error("Must be Y or N");
    }
}

// Main-------------------------------------------------------------------------   

int main()
{    
 runPatron();
     keep_window_open();
}

If you go to class Library and into the Transaction struct, that is where I want to store the date.

A: 

There is no such standard C++ function as strdate. Whether putting those lines "inside a struct" causes a problem is impossible to say without seing the code. I suggest what you really want is the asctime() function, or one of its relatives.

anon
Neil, what tools do you use to follow SO so that you can answer so quickly?
Piotr Dobrogost
Code has been posted. I"ll look into the asctime function.
trikker
Actually, I think of myself as a bit slow - I often find others answering before me. I'm a two-finger (plus thumbs on a good day) typist, and this answer took me several edits and a google to get to the state its in.
anon
+1  A: 

Where to start? Difficult to tell without seeing the code, but here's some thoughts.

First, you should probably NOT store the date as a character array within your struct because that will make manipulation of it awkward. Better to store it in it's native form as a time_t, and convert it to/from strings as required.

Second, if you must store it as ascii, don't store it as char[]. You're just asking for buffer overruns etc. Use string or vector

However, I think the real answer to your question concerns constructors. If you want Transaction to hold a date, then define it like this

struct Transaction
{
   char dateStr[9];
}

If you want transactions to be datestamped, then assign the date in the constructor

struct Transaction
{
   char dateStr[9];

   Transaction()
   {
      _strdate(dateStr); // or asctime or whatever
   }
}

Now, whenever you create a Transaction, it will automatically be filled with the return value from _strdate, whatever that is. Here's how I'd do it with time_t.

#include <stdio.h>
#include <time.h>

struct Transaction
{
  time_t theTime;

  Transaction()
  {
    theTime = time(NULL);
  }
}
Roddy
There is going to be a vector<Transation> that will hold objects of data, including date.
trikker
@trikker, Yes, but so what?
Roddy
Basically whenever a user checks out a book, a transaction is stored and rather than having the person input the date on their own I'd rather the program just get the date from the system and store it on its own.
trikker
And this is just an exercise so it doesn't need to be perfect. I could write a Date class and include Date in transaction, but I'd like to know if there's a cleaner way to store a simple date.
trikker
the _strdate(dateStr) doesn't work. That was the question in the first place.
trikker
Got it working. Was a syntax problem. Thanks.
trikker