Most people make mistakes when programming. Today, I had an interesting debug session with Mr. Stuttner(he still does good LAN parties) today. Since the stuff we looked at may be interestinf for more of you, here is a brain-dump of what we did!

The program was designed to convert characters without using library functions(just macros), but it didnt work. It always converted to lower. Anyways, we decided to trace the program by intersting a few decoy alerts into the program:


#include "stdafx.h"

#define Upper(c)((c>='a'&&c< ='z')?(c-'a'+'A'):(c))
#define Lower(c)((c>='A'&&c< ='Z')?(c-'A'+'a'):(c))

int _tmain(int argc, _TCHAR* argv[])
{

char c;
printf("Buchstabe Eingeben der Gros geschreiben werden soll:\n");
scanf("%c",&c);
printf("Ihre Eingabe: %c\n",c);
if ((c<=65) && (c>=90))
{ printf("If");
c=Upper(c);
}
else
{ printf("Else");
c=Lower(c);
}

printf("Auswertung: %c",c);
scanf("%d",c);

return 0;
}

Now, we knew what happened. And indeed, the condition of the loop was erroneously formulated. This led to the following program:

#include "stdafx.h"

#define Upper(c)((c>='a'&&c< ='z')?(c-'a'+'A'):(c))
#define Lower(c)((c>='A'&&c< ='Z')?(c-'A'+'a'):(c))

int _tmain(int argc, _TCHAR* argv[])
{

char c;
printf("Buchstabe Eingeben der Gros geschreiben werden soll:\n");
scanf("%c",&c);
printf("Ihre Eingabe: %c\n",c);
if ((c>=65) && (c< =90))
{
c=Upper(c);
}
else
{
c=Lower(c);
}

printf("Auswertung: %c",c);
scanf("%d",c);

return 0;
}

This program worked better, but still was unreliable. It now simply returned the character given. SO, we fed in decoys yet again!

#include "stdafx.h"

#define Upper(c)((c>='a'&&c< ='z')?(c-'a'+'A'):(c))
#define Lower(c)((c>='A'&&c< ='Z')?(c-'A'+'a'):(c))

int _tmain(int argc, _TCHAR* argv[])
{

char c;
printf("Buchstabe Eingeben der Gros geschreiben werden soll:\n");
scanf("%c",&c);
printf("Ihre Eingabe: %c\n",c);
if ((c>=65) && (c< =90))
{ printf("Converting to upper");
c=Upper(c);
}
else
{ printf("Converting to lower");
c=Lower(c);
}

printf("Auswertung: %c",c);
scanf("%d",c);

return 0;
}

We now saw that the statements need to be exchanged! And so, finally, we exchanged the upper and lower macros to get a nice and working routine!

#include "stdafx.h"

#define Upper(c)((c>='a'&&c< ='z')?(c-'a'+'A'):(c))
#define Lower(c)((c>='A'&&c< ='Z')?(c-'A'+'a'):(c))

int _tmain(int argc, _TCHAR* argv[])
{

char c;
printf("Buchstabe Eingeben der Gros geschreiben werden soll:\n");
scanf("%c",&c);
printf("Ihre Eingabe: %c\n",c);
if ((c>=65) && (c< =90))
{
c=Lower(c);
}
else
{
c=Upper(c);
}

printf("Auswertung: %c",c);
scanf("%d",c);

return 0;
}

So, overall, this was funky! Indeed, debugging gets much easier if you insert decoy alerts into your program. This is much easier than using breakpoints,... . For the Palm OS, FrmAlert and FrmCustomAlert may be suitable decoys!

Related posts:

  1. Semicolons-the source of bugs
  2. Sorting filenames in a recursive manner
  3. Globals in DAs
  4. On defining multiple pointers in one line
  5. Determining if your app runs on a Zodiac