存储日期的方法

时间:2009-07-19 20:19:32

标签: c++

我想知道是否有办法在不编写自己的功能的情况下获得约会。访问系统日期等内容。我试过了

char dateStr[9];
_strdate(dateStr);

使用包含的时间标题,但我得到编译器错误ISO C ++禁止声明_strdate没有类型。我认为_strdate已经在标题中声明和定义,因此它不需要类型定义。可能问题是我在结构中有这两行吗?有没有更好的方法存储日期?感谢。

#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();
}

如果你去类库并进入Transaction结构,那就是我想存储日期的地方。

2 个答案:

答案 0 :(得分:1)

从哪里开始?没有看到代码就很难分辨,但这里有一些想法。

首先,您可能不应该将日期作为字符数组存储在结构中,因为这会使操作变得笨拙。最好将其作为 time_t 存储在其原生形式中,并根据需要将其转换为字符串。

其次,如果必须将其存储为ascii,请不要将其存储为char []。你只是要求缓冲区溢出等。使用字符串或向量

但是,我认为你的问题的真正的答案涉及构造函数。如果您希望Transaction保留日期,那么就像这样定义

struct Transaction
{
   char dateStr[9];
}

如果要将事务加上日期戳,请在构造函数中指定日期

struct Transaction
{
   char dateStr[9];

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

现在,无论何时创建事务,它都将自动填充_strdate的返回值,无论是什么。这是我用time_t做的方式。

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

struct Transaction
{
  time_t theTime;

  Transaction()
  {
    theTime = time(NULL);
  }
}

答案 1 :(得分:0)

没有strdate这样的标准C ++函数。是否将这些行放在“结构内部”会导致问题,如果不查看代码就无法说出来。我建议你真正想要的是asctime()函数,或者它的一个亲戚。