在c++中设置特定的日期



我想设置一个日期,这样如果当前日期超过日期集,函数将不会运行。例如,在某个日期之后,用户不能再投票了。

#include<iostream>
#include<iomanip>
#include<string>
#include<stdlib.h>
#include<time.h>
#define enddate "2022-12-30"
using namespace std;
int nominationMenu() {
time_t ct = time(0);
string currenttime= ctime(&ct);
string candid_mykad, candidID;
string program;
int candidstudyyear;
if (currenttime<enddate) {
cout << setw(49) << setfill('-') << "-" << endl;
cout << setw(50) << setfill(' ') << left << "n                NOMINATION SESSION" << endl;
cout << setw(50) << setfill('-') << "n-" << endl;
cout << "PLease keep in mind that the voting session will only begin after the 26th of December." << endl;
cout << "One candidate per student.n" << endl;
system("pause");
cout << "Please enter candidate's information:" << endl;
cout << "nMyKad (without dashes '-') t:" << endl;
cin >> candid_mykad;
cout << "Student ID (full alphanumeric e.g. 22WMR12345) t: " << endl;
cin >> candidID;
cout << "Program t:" << endl;
cin.ignore(1000, 'n');
getline(cin, program);
cout << "Year of study t:" << endl;
cin >> candidstudyyear;
cout << "nYou have nominated a candidate." << endl;
}
else
cout << "Nomination session has ended. Please proceed to the voting session." << endl;
return 0;
}

我尝试设置一个常量,但没有工作。它直接转到else语句

与其将日期转换为字符串并比较字符串,不如将所有日期转换为time_ts,并比较它们。

struct tm enddate {
.tm_mday = 13, // one based
.tm_mon  = 11, // zero based
.tm_year = 122 // year - 1900
};
int nominationMenu() {
time_t ct = time(0);
time_t et = mktime(&enddate); // convert to a time_t
string candid_mykad, candidID;
string program;
int candidstudyyear;
if (ct < et) {                // compare the time_t's
// ...

从理论上讲,这可能不是完全可移植的。c++指定初始化式必须与成员定义的顺序一致。这适用于主要的编译器(MS, gcc, clang),但理论上,它可能允许某人重新排列struct tm的字段,因此它不会。

相关内容

  • 没有找到相关文章