-4

I'm trying to find out the week number of a date in C++. I'm taking my date from the console as a string and dividing it into the date, month, and year.

This is my code. Please help me to find out the problem.

#include <iostream> 
using namespace std;
#include <string>
#include <cstring>
#include <ctime>
int main(void)
{
  struct tm tm;
  char timebuf[64];
  memset(&tm, 0, sizeof tm);
  string date;
  cout<<"Enter the date (dd-mm-yyyy) : "; //print statement is used to print message on console
  cin>>date; //taking input from user
  int day=stoi(date.substr(0,2));
  int month=stoi(date.substr(3,2));
  int year=stoi(date.substr(6,4));
  //cout << day << month << year << endl;
  tm.tm_sec = 0;
  tm.tm_min = 0;
  tm.tm_hour = 23;
  tm.tm_mday = day;
  tm.tm_mon = month;
  tm.tm_year = year;
  tm.tm_isdst = -1;
  mktime(&tm);

  if (strftime(timebuf, sizeof timebuf, "%W", &tm) != 0) {
    printf("Week number is: %s\n", timebuf);
  }

  return 0;
}
Biffen
  • 6,249
  • 6
  • 28
  • 36
  • 3
    It would help tremendously if you'd describe the problem you're seeing. – Jerry Coffin Dec 04 '20 at 01:18
  • 4
    It would also tremendously help if you took whichever C++ textbook taught you to include that non-standard header, and add `using namespace std;` -- whichever textbook you learned that from -- it will help you if you get rid of it and get a much better C++ textbook. The better textbook which will also explain to how to use `atoi` to do a much bettercharacter-to-integer conversion function than your code. – Sam Varshavchik Dec 04 '20 at 01:22
  • 3
    *#include"bits/stdc++.h" //header file that includes every* -- [No. please don't do this](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – PaulMcKenzie Dec 04 '20 at 01:29
  • Can your please correct it and give me the correct code. If the input is 04-11-2020 then the output should be 45 but it is giving 48. please correct it. – Tumpilli Samba Dec 04 '20 at 01:35
  • 1
    @TumpilliSamba that bit of information should be put in the question where it will more easily be found by future askers and answereres. – user4581301 Dec 04 '20 at 01:47
  • 1
    Most development environments come with a debugging utility., With a debugger you can run the program at a speed you control and be able to see exactly what thee program does as it does it. Typical usage is to step through the region where you believe the problem to be and keep an eye out for the program doing something you didn't expect. In a computer program the unexpected is a bug. – user4581301 Dec 04 '20 at 01:53

1 Answers1

1

A struct tm uses a 0-based month number. When your user enters 04-11-2020, they (presumably) mean the fourth of November, 2020. To get that when you put the data into the struct tm, you'll need to subtract 1 from the month number.

Years also start from 1900, so you'll need to subtract 1900 from your year number.

Alternatively, you could use std::get_time to read and parse the fields for you:

#include <iostream>
#include <iomanip>
#include <ctime>
#include <sstream>

int main() {    
    std::tm then{};

    std::istringstream in("04-11-2020");

    if (!(in >> std::get_time(&then, "%d-%m-%Y")))
        std::cerr << "Conversion failed\n";

    mktime(&then);

    // %W for 0-based, %V for 1-based (ISO) week number:
    std::cout << std::put_time(&then, "%V\n");
}

std::get_time knows how to convert from human-readable dates to the number ranges a tm requires, so you don't have to explicitly take the vagaries of tm into account this way (and yes, at least for me, this produces 45, as expected). One warning though: std::get_time expects zero-filled fields, so (for example) if you used 4-11-2020 instead of 04-11-2020, expect it to fail.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • If the correct output is 45 for 04-11-2020 (as stated in the comments), then the correct specifier is `%V`, the ISO week number definition. – Howard Hinnant Dec 04 '20 at 03:33
  • @HowardHinnant: Thank you. By the way, did the LWG coordinate on invading SO, or is it just coincidence that both you and Marshall were around, posting today? :-) – Jerry Coffin Dec 04 '20 at 06:19
  • We're taking over the world ... Or maybe SO is invading the LWG? ;-) – Howard Hinnant Dec 04 '20 at 15:06