Like Tree1Likes
  • 1 Post By black_stallion

Code does not work when 'run' ing it but works fine while debugging?

This is a discussion on Code does not work when 'run' ing it but works fine while debugging? within the C Programming forums, part of the General Programming Boards category; I have a code to convert a number to words(e.g. 31415-->Thirty one thousand four hundred fifteen) The code compiles with ...

  1. #1
    Registered User black_stallion's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    22

    Exclamation Code does not work when 'run' ing it but works fine while debugging?

    I have a code to convert a number to words(e.g. 31415-->Thirty one thousand four hundred fifteen) The code compiles with no errors/warnings and outputs correctly when debugging. But nothing happens when I 'Run' it. I'm using Code::Blocks. Any help would be appreciated!

    Code:
    int main (void)
    {
    int *dataptr = NULL,num,digit_count;
    STACK* stack = create();
    dataptr = malloc(sizeof(int));
    printf("Enter number");
    scanf("%d",&num);
    while (num!=0)
    {
        *dataptr = num%10;
        num = num/10;
        push (stack,dataptr);
        digit_count ++;
    };
    convert (stack,digit_count);
    return 0;
    
    }
    
    
    void convert (STACK* stack,int digit_count)
    {
    int* datainptr1=NULL,*datainptr2=NULL;
    datainptr1 = (int*)pop(stack);
    switch (digit_count)
    {
        case 1: //some function
        break;
    
        case 2:
        {
            if (*datainptr1 == 1)
            {
            datainptr2 = (int*)pop(stack);
            switch (*datainptr2)
            {
                case 1:printf ("Eleven");
                break;
                case 2:printf ("Twelve");
                break;
                case 3:printf ("Thirteen");
                break;
                case 4:printf ("Fourteen");
                break;
                case 5:printf ("Fifteen");
                break;
                case 6:printf ("Sixteen");
                break;
                case 7:printf ("Seventeen");
                break;
                case 8:printf ("Eighteen");
                break;
                case 9:printf ("Nineteen");
                break;
                }
            }
            else
            {
               //more
            }
    
        }
        break;
    }
    }
    The 'pop' function in a custom header file(for stack simulation) returns a void* by the way. Any ideas on what's wrong?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Local integer digit_count is not initialized, and so when first incremented, is probably much higher (or lower) than 2, and much higher (or lower) than any of the cases in the switch. Running it inside the debugger may mean it is set to 0 for whatever reason. You should initialize it to zero explicitly and see if that makes a difference.

    If that works, you should put it back the way it was (uninitialized), throw a printf to report it's value at the very top, and run it in the debugger to see if it does, in fact set everything to zero.
    Last edited by MK27; 03-06-2012 at 07:03 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User black_stallion's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    22

    Smile

    Quote Originally Posted by MK27 View Post
    Local integer digit_count is not initialized, and so when first incremented, is probably much higher (or lower) than 2, and much higher (or lower) than any of the cases in the switch. Running it inside the debugger may mean it is set to 0 for whatever reason. You should initialize it to zero explicitly and see if that makes a difference.
    Thanks a ton! Indeed the debugger set its value to 0 explicitly.! Just about everything other than that variable had been initialized. Amusing how just one glitch can affect a program's output!
    Last edited by black_stallion; 03-06-2012 at 07:07 AM.
    MK27 likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Missing something, works fine until the end
    By cantcoderight in forum C++ Programming
    Replies: 6
    Last Post: 01-21-2012, 05:04 AM
  2. This works fine with IP 127.0.0.1 but fails with my REAL IP.
    By azjherben in forum Networking/Device Communication
    Replies: 15
    Last Post: 05-19-2009, 10:28 PM
  3. Compile issue in g++, works fine in VS
    By bean66 in forum C++ Programming
    Replies: 4
    Last Post: 04-20-2009, 09:48 AM
  4. It works just fine but
    By jcmhex in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2005, 06:53 PM
  5. My computer works just fine
    By Betazep in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 07-02-2002, 08:51 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21