Click here to Skip to main content
15,887,596 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Wordle 556 Pin
ChandraRam26-Dec-22 20:30
ChandraRam26-Dec-22 20:30 
GeneralRe: Wordle 556 Pin
pkfox26-Dec-22 22:16
professionalpkfox26-Dec-22 22:16 
GeneralRe: Wordle 556 Pin
OriginalGriff27-Dec-22 0:26
mveOriginalGriff27-Dec-22 0:26 
GeneralRe: Wordle 556 Pin
Sandeep Mewara27-Dec-22 1:18
mveSandeep Mewara27-Dec-22 1:18 
GeneralRe: Wordle 556 Pin
StarNamer@work27-Dec-22 1:49
professionalStarNamer@work27-Dec-22 1:49 
GeneralRe: Wordle 556 Pin
Cp-Coder27-Dec-22 8:26
Cp-Coder27-Dec-22 8:26 
GeneralRe: Wordle 556 Pin
jmaida27-Dec-22 16:32
jmaida27-Dec-22 16:32 
QuestionHaving trouble with a function in C Pin
BuilderboiYT26-Dec-22 15:36
BuilderboiYT26-Dec-22 15:36 
In my program, I am asking the user to input a date(in just integers ie 12 31 2019 367) and the number of days they add to it. In one of my functions, this is precisely what I am doing.

The user inputs 12 31 2019 367, and the program is meant to print 1 1 2021, but instead prints 1 1 2020(a year behind)...

What I did(Sorry for a lot of code, I tried to keep it simple and clean):
C++
int days_in_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
                      
void add_days_to_date(int *mm, int *dd, int *yy, int days_left_to_add)
{
  int days_left_in_month;
  while(days_left_in_month > 0)
  {
    days_left_in_month = days_in_month[*mm] - *dd;
 //   days_left_in_month = days_in_month[*mm] - *dd;
    if (days_in_month[2] && is_leap_year(*yy) == true)
    {
      days_left_in_month++;
    }
   } // end while
   printf("after while\n");
    if(days_left_to_add > days_left_in_month)
    {
      days_left_to_add -= days_left_in_month;
      *dd = 1;
      if(*mm == 12)
      {
        *mm = 1;
        (*yy)++;
      }
      else
      {
        (*mm)++;
      }
    }
    else
    {
      *dd += days_left_to_add;
      days_left_to_add = 0;
    }
}
int main()
{
  int mm, dd, yy, days_left_to_add;
  printf("Please enter a date between the years 1800 and 10000 in the format mm dd yy and provide the number of days to add to this date:\n");
  scanf("%d %d %d %d", &mm, &dd, &yy, &days_left_to_add);
 // printf("\nREAD\n");
  //These are pointers, so they have to be at certain location i.e. int* mm = &mm
  add_days_to_date(&mm, &dd, &yy, days_left_to_add);
  printf("%d %d %d\n", mm, dd, yy);
}
What I got after inputs:

Inputs: 12 31 2019 367

Output: 1 1 2020(meant to be 1 1 2021)

Thank you in advance and for your time and patience...

modified 27-Dec-22 10:45am.

AnswerRe: Having trouble with a function in C Pin
honey the codewitch26-Dec-22 15:46
mvahoney the codewitch26-Dec-22 15:46 
GeneralRe: Having trouble with a function in C Pin
jmaida26-Dec-22 17:26
jmaida26-Dec-22 17:26 
GeneralRe: Having trouble with a function in C Pin
obermd27-Dec-22 4:41
obermd27-Dec-22 4:41 
GeneralInternational CCC - 2022-12-27 Pin
Randor 26-Dec-22 13:03
professional Randor 26-Dec-22 13:03 
GeneralRe: International CCC - 2022-12-27 Pin
DRHuff26-Dec-22 13:22
DRHuff26-Dec-22 13:22 
GeneralRe: International CCC - 2022-12-27 Pin
Randor 26-Dec-22 13:42
professional Randor 26-Dec-22 13:42 
GeneralRe: International CCC - 2022-12-27 Pin
DRHuff26-Dec-22 14:05
DRHuff26-Dec-22 14:05 
Generalworldle 339 Pin
jmaida26-Dec-22 10:34
jmaida26-Dec-22 10:34 
GeneralChat GTP Oops, lol Pin
Marc Clifton26-Dec-22 9:15
mvaMarc Clifton26-Dec-22 9:15 
GeneralRe: Chat GTP Oops, lol Pin
CPallini27-Dec-22 1:01
mveCPallini27-Dec-22 1:01 
GeneralGrowing pains PinPopular
honey the codewitch26-Dec-22 4:21
mvahoney the codewitch26-Dec-22 4:21 
GeneralRe: Growing pains Pin
abmv26-Dec-22 5:41
professionalabmv26-Dec-22 5:41 
GeneralRe: Growing pains Pin
charlieg26-Dec-22 6:29
charlieg26-Dec-22 6:29 
GeneralRe: Growing pains Pin
Derek Hunter26-Dec-22 20:24
Derek Hunter26-Dec-22 20:24 
GeneralRe: Growing pains Pin
rob tillaart26-Dec-22 21:43
rob tillaart26-Dec-22 21:43 
GeneralRe: Growing pains Pin
CodeZombie6227-Dec-22 0:46
CodeZombie6227-Dec-22 0:46 
GeneralRe: Growing pains Pin
honey the codewitch27-Dec-22 2:28
mvahoney the codewitch27-Dec-22 2:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.