0

i want to set a date, so that if the current date exceed the date set, the function will not run. For example after a certain date user are not able to vote anymore.

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

i tried setting a constant but that did not work. it just go straight to the else statement.

asked Dec 11, 2022 at 18:05
4
  • Did you check the actual value of currenttime to see if it was in the format you expected? Commented Dec 11, 2022 at 18:25
  • let me try that real quick. Mon Dec 12 02:32:43 2022 was the format Commented Dec 11, 2022 at 18:31
  • So, considering that you're performing a string comparison, do you think that will be greater than or less than "2022年12月30日"? Commented Dec 11, 2022 at 18:38
  • When you think about date, also consider where on the planet this date is to be considered. When you consider all 24 hours of a day, usually there are two dates currently on the planet. You might think of the current date as specified by UTC. Or as specified by the local time zone which is set on the computer the code is running on. Or as specified by a specific time zone such as Australia/Sydney. Each of these are valid definitions, and your code will be better if it clarifies what you intend. Commented Dec 12, 2022 at 3:17

1 Answer 1

0

Rather than converting the dates to strings, and comparing the strings, I'd convert all the dates to time_ts, and compare them.

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
 // ...

As it stands, this theoretically might not be entirely portable. C++ designated initializers are required to be in the order the members are defined. This works with the major compilers (MS, gcc, clang) but in theory, it would probably be allowed for somebody to rearrange the fields of a struct tm, so that it wouldn't.

answered Dec 11, 2022 at 18:38
Sign up to request clarification or add additional context in comments.

6 Comments

i put in the struct but red lines appeared under the dot of .tm_mday, saying expected an expression
@Jayren: you probably have your compiler (or at least your IDE) set up to accept only older versions of C++. Designated initializers require C++ 20, so older compilers don't accept them.
time_t ct = time(0); string currenttime = ctime(&ct); time_t et = mktime(&enddate); string endtime = ctime(&et); I fixed the compiler problem. The problem now is that i need to put string if not the date wont output. The current time works fine, but the enddate part i cant get it to work. It brings me to xstring and said "Exception thrown access violation reading location"
@jayren: you can use localtime and std::put_time to write out the end time.
where should i implement it?
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.