C++ 存储日期的方法

C++ 存储日期的方法,c++,C++,我想知道是否有一种方法可以在不编写自己函数的情况下获得日期。例如访问为系统设置的日期。我试过了 char dateStr[9]; _strdate(dateStr); 使用包含的时间头,但我得到编译器错误,ISO C++禁止声明没有类型的ySrdReST。我认为_strdate已经在头中声明和定义了,所以它不需要类型定义。问题是我在一个结构中有这两行吗?有没有更好的储存日期的方法?谢谢 #include "std_lib_facilities.h" //Classes------------

我想知道是否有一种方法可以在不编写自己函数的情况下获得日期。例如访问为系统设置的日期。我试过了

char dateStr[9];
_strdate(dateStr);

使用包含的时间头,但我得到编译器错误,ISO C++禁止声明没有类型的ySrdReST。我认为_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();
}
#包括“std_lib_facilities.h”
//班级-----------------------------------------------------------------------
课堂用书{
公众:
Book(){};//默认构造函数
//操作员

朋友OsFrand和运算符< P>没有标准的C++函数作为SrDATE。是否将这些行放在结构内?原因如果没有代码,问题是不可能说出来的。我建议您真正想要的是)函数,或者它的一个亲戚。

从哪里开始?如果没有代码,很难说出来,但这里有一些想法

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

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

然而,我认为您的问题的真正答案与构造函数有关

struct Transaction
{
   char dateStr[9];
}
如果希望事务具有日期戳,则在构造函数中指定日期

struct Transaction
{
   char dateStr[9];

   Transaction()
   {
      _strdate(dateStr); // or asctime or whatever
   }
}
现在,无论何时创建事务,它都会自动填充来自_strdate的返回值,不管是什么

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

struct Transaction
{
  time_t theTime;

  Transaction()
  {
    theTime = time(NULL);
  }
}
#包括
#包括
结构事务
{
时间;
交易()
{
时间=时间(空);
}
}

Neil,你使用什么工具来遵循,以便你能如此快速地回答问题?代码已经发布。我将研究asctime函数。事实上,我觉得自己有点慢——我经常发现别人在我前面回答。我是一个笨手笨脚的人(在好的一天加上大拇指)打字员,这个答案我用了几次编辑和一个谷歌来获得它的状态。将会有一个向量来保存数据对象,包括日期。基本上,当用户签出一本书时,一个事务被存储,而不是让用户自己输入日期,我宁愿程序只从系统中获取日期这只是一个练习,不需要太完美。我可以编写一个Date类并在事务中包含Date,但我想知道是否有更干净的方法来存储简单的日期。strdate(dateStr)不起作用。这是第一个问题。