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:







I thought I was the only one that did it…
Hi,
now we are three;). All of my apps actually contain 4 different alert dialogs that mostly serve for debugging purposes. FrmCustomAlert easily is the most-used function of my life;)
Best regards and thank you for talking back
Tam Hanna
You know you can implement those a little simpler as:
#define Upper(c)(c&’_')
#define Lower(c)(c|’ ‘)
(That’s right, and with underscore, and or with space will do your case shifting. Ugly, eh?
)
Hi,
slick!
Gotta look at how this works…
Best regards
Tam Hanna