Re: beginner with programming, how to learn to debug and few C general questions



bpascal123@xxxxxxxxxxxxxx wrote:

Hi,

I am first an accountant and decided to take on programming a few
years ago...
I have been first studying simple algorithms...

Now I have decided to first learn C (i'll give it 600 effective hours
- so btw 1 and 3 years) before going to c++ or perl, or python, java
or php or ruby on rails or vba, i'll see at that time what answers
most my needs either as a professionnal accountant or as a personal
project
i'd like to take on... Is there anything specific I could do with a
good knowledge of C only ?

So I have been having a hard time with the basics of pointers (20
hours). I really want to feel ok with this before getting any further.
I haven't looked at functions yet.

Here is a programm I know by heart - from a book - but in this code it
doesn't work althought it is very similar to other versions I have
been "coding" over and over more than 30 times ... and 10 % of the
time, there is a bug very similar to the one below I can solve by
looking carefully at the code or cannot solve if I have looked
carefully at the code. That's why I'd like to learn how to debug.

So far or so early with learning C, I'd like to use this opportunity
to learn how to debug and know about the tools that can help.

I have mainly been programming under Windows Xp DJGPP Dolorie... and I
have found a command : simify (link :
http://www.delorie.com/djgpp/v2faq/faq9_3.html ) to help debug.

===O===

FIRST, here is the culprit : (this prg inserts a symbol every 3 digits
if the number includes 4 or more digits)

===O===

#include <stdio.h> /* printf, putchar, gets, puts */
#include <conio.h> /* getche */

<conio.h> is part of your Windows library, not of C. Therefore, your
code is not only outside the scope of this group, but more importantly,
many regulars here will be unable to compile your code. (I for one have
no <conio.h> header.)

#include <stdlib.h> /* atoi */
#include <ctype.h> /* isdigit */
#include <string.h> /* strcpy, strlen */

#define BOOL int

/**********/

main()

This should be
int main(void)

Which is clearer and works with the latest standardized version of C.

{
char buffer[128] ;
char clearbuf[128] ;
char output[172] ;

char * pb = buffer, * pc = clearbuf, * po = output ;

int symbol ;
int i, k ;
int cnt ;

BOOL ok ;

/**********/

printf("\033[2J");
printf("\nCe prog separe...\n");


You can use puts() instead of printf(), which saves a fair bit of
overhead and automatically adds the \n to the end of every line.

/**********/

do
{
printf("\nSeparateur :");
symbol = getche() ;

What does this function do? If you need to get a single character, use
getc() or fgetc().


if ( symbol < 32 )
printf("Pas de caractere de control comme separateur !");
if ( isdigit(symbol) )
printf("Pas de chiffre comme separateur !" ) ;
} while ( symbol < 32 || isdigit(symbol) ) ;

/**********/


Please use proper indentation when posting on Usenet.

do
{
ok = 1 ;
printf("\nEntrez un nombre entier positif ");
gets(buffer) ;

Stop right here. NEVER ever EVER use gets(). It is impossible to use
safely, since it does no buffer size checking and therefore is inviting
users to overrun your buffer and run over who-knows-what memory.

Use fgets() instead, which will accept sizeof buffer as an additional
argument, and will check the size of the input.


cnt = 0 ;

while ( *(buffer + cnt) && ok )
{
if ( !isdigit(*(buffer + cnt) ) )
{
printf("Pas d'entier positif!") ;
ok = 0 ;
}
cnt++;
}
} while ( !ok ) ;

/**********/

if ( cnt > 1 && atoi(buffer) )

atoi() is also not recommended since it doesn't fail well if you pass it
invalid input. Use strtol() or strtoul() instead.

{
while ( *pb == '0' )
pb++ ;

while ( *pc++ = *pb++ )
;

strcpy(buffer, clearbuf);
}

/**********/

if ( (cnt = strlen(buffer)) > 3)
{
for ( i = cnt - 1, k = 0, pb = buffer ; i >= 0 ; i--, k++ )

Your spacing is abysmal. Please correct it.

{
po[k] = pb[i] ;

if ( ( (cnt - i) % 3 ) == 0 && i != 0 )
po[++k] = symbol ;
}
po[k] = '\0' ;

for ( i = k - 1 ; i >= 0 ; i-- )
putchar(*(output + i));
}
else
puts(buffer) ;
}


If you fix all of the above problems, it might fix whatever the issue
with your code is. But as it stands, I can neither compile nor read the
posted code. So please correct these things, fix the formatting and
repost if you are still having issues.

--
Andrew Poelstra <http://testing.wpsoftware.net/finex>


.