Click here to Skip to main content
15,878,959 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How to link C code to which Bluetooth library ? ( Linux) Pin
Richard MacCutchan27-Dec-22 22:03
mveRichard MacCutchan27-Dec-22 22:03 
QuestionInsert a Container App Pin
Glenn Meadows 202227-Dec-22 5:07
Glenn Meadows 202227-Dec-22 5:07 
AnswerRe: Insert a Container App Pin
Richard MacCutchan27-Dec-22 22:06
mveRichard MacCutchan27-Dec-22 22:06 
SuggestionRe: Insert a Container App Pin
David Crow28-Dec-22 4:49
David Crow28-Dec-22 4:49 
QuestionHaving trouble with a function in C Pin
BuilderboiYT26-Dec-22 15:38
BuilderboiYT26-Dec-22 15:38 
AnswerRe: Having trouble with a function in C Pin
Victor Nijegorodov26-Dec-22 20:12
Victor Nijegorodov26-Dec-22 20:12 
AnswerRe: Having trouble with a function in C Pin
Richard MacCutchan26-Dec-22 22:10
mveRichard MacCutchan26-Dec-22 22:10 
AnswerRe: Having trouble with a function in C Pin
Mircea Neacsu26-Dec-22 22:34
Mircea Neacsu26-Dec-22 22:34 
As Victor was saying, this is the tough part of a programmer's job: finding the errors in your code. Most of the time you are on your own when doing it but in this case I'll try to do part of it with you hoping the experience will be useful.

Before we start, when posting code here, try to paste it between the <pre></pre> tags. It makes it for a much nicer and easier to read code.
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)
Now, ask yourself, what is the value of days_left_in_month when the computer tries to evaluate the while loop for the first time? The answer is that it is not known. This is an "uninitialized local variable" bug and it is very common. The compiler has reserved space for the variable but its content can be anything. We have to fix this.
C
while (days_left_in_month > 0)
{
  days_left_in_month = days_in_month[*mm] - *dd;
  if (days_in_month[2] && is_leap_year (*yy) == true)
Oops, here is another bug: you wrote days_in_month[2] which is 28 and clearly not zero but what you probably had in mind was more like:
C
if (month == 2 && is_leap_year(*yy) == true)


If we fix the two errors we found your code will be looking something like this:
C
void add_days_to_date (int* mm, int* dd, int* yy, int days_left_to_add)
{
  int days_left_in_month;
  days_left_in_month = days_in_month[*mm] - *dd;
  if (*mm == 2 && is_leap_year (*yy))
    days_left_in_month++;
Now it's really the time to start a loop but this loop will have to run until the days_left_to_add is greater than days_left_in_month.

Here I'll stop and let you finish the code. Post back the result and I'll give you more feedback on algorithm and style.

EDIT - I see that while I was writing my long convoluting answer Richard had already made the same points in a more concise form Smile | :)
Mircea

GeneralRe: Having trouble with a function in C Pin
Richard MacCutchan26-Dec-22 23:39
mveRichard MacCutchan26-Dec-22 23:39 
QuestionInvalid comparator for STL map Pin
ForNow25-Dec-22 18:08
ForNow25-Dec-22 18:08 
AnswerRe: Invalid comparator for STL map Pin
Daniel Pfeffer25-Dec-22 19:36
professionalDaniel Pfeffer25-Dec-22 19:36 
GeneralRe: Invalid comparator for STL map Pin
ForNow25-Dec-22 21:33
ForNow25-Dec-22 21:33 
GeneralRe: Invalid comparator for STL map Pin
ForNow26-Dec-22 5:14
ForNow26-Dec-22 5:14 
GeneralRe: Invalid comparator for STL map Pin
Graham Breach26-Dec-22 6:15
Graham Breach26-Dec-22 6:15 
JokeRe: Invalid comparator for STL map Pin
ForNow26-Dec-22 6:51
ForNow26-Dec-22 6:51 
GeneralRe: Invalid comparator for STL map Pin
ForNow26-Dec-22 7:39
ForNow26-Dec-22 7:39 
GeneralRe: Invalid comparator for STL map solution Pin
ForNow26-Dec-22 8:31
ForNow26-Dec-22 8:31 
GeneralRe: Invalid comparator for STL map solution Pin
k505426-Dec-22 15:49
mvek505426-Dec-22 15:49 
GeneralRe: Invalid comparator for STL map solution Pin
ForNow26-Dec-22 21:06
ForNow26-Dec-22 21:06 
GeneralRe: Invalid comparator for STL map solution Pin
Graham Breach26-Dec-22 21:42
Graham Breach26-Dec-22 21:42 
GeneralRe: Invalid comparator for STL map solution Pin
ForNow27-Dec-22 1:22
ForNow27-Dec-22 1:22 
QuestionRead Access exception in xtree for map::insert Pin
ForNow24-Dec-22 17:18
ForNow24-Dec-22 17:18 
AnswerRe: Read Access exception in xtree for map::insert answer found Pin
ForNow25-Dec-22 9:55
ForNow25-Dec-22 9:55 
Questionoverloaded = operator not being invoked Pin
ForNow22-Dec-22 14:36
ForNow22-Dec-22 14:36 
AnswerRe: overloaded = operator not being invoked Pin
Mircea Neacsu22-Dec-22 15:24
Mircea Neacsu22-Dec-22 15:24 

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.