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.

Jayren
  • 11
  • 1
  • Did you check the actual value of `currenttime` to see if it was in the format you expected? – Nathan Pierson Dec 11 '22 at 18:25
  • let me try that real quick. Mon Dec 12 02:32:43 2022 was the format – Jayren Dec 11 '22 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"`? – Nathan Pierson Dec 11 '22 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](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#SYDNEY). Each of these are valid definitions, and your code will be better if it clarifies what you intend. – Howard Hinnant Dec 12 '22 at 03:17

1 Answers1

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.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • i put in the struct but red lines appeared under the dot of .tm_mday, saying expected an expression – Jayren Dec 11 '22 at 18:52
  • @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. – Jerry Coffin Dec 11 '22 at 19:16
  • 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 Dec 11 '22 at 19:20
  • @jayren: you can use `localtime` and `std::put_time` to write out the end time. – Jerry Coffin Dec 11 '22 at 19:24
  • where should i implement it? – Jayren Dec 11 '22 at 19:25
  • @Jayren: you generally implement it where you need it. Thinking about it, you're starting with a `struct tm`, so you don't really need `localtime` (which converts from `time_t` to `tm`), you just need `std::put_time`. Alternatively, if you're happy with the result from `ctime`, you could just use `std::cout << ctime(&et);` (after the call the `mktime`). – Jerry Coffin Dec 11 '22 at 19:31