Thread: Program working fine until I make one change..plz help

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    28

    Program working fine until I make one change..plz help

    Hello everyone,

    This is a program which asks the user 10 times to give a rating to a television show on a scale of 1 to 10(inclusive). Am posting the before and after code. There is no problem with the before code.

    Before code

    Code:
    #include <stdio.h>
    
    int main ()
    
    {
        int ask, i, ratingcounter[i], response, display ;
    
        for (i = 1; i <= 10; i++)
        ratingcounter[i] = 0 ;
    
        printf ("Rate the television show on a scale of 1 to 10\n\n") ;
    
        for (ask = 1; ask <= 10; ask++)
        {
            scanf ("%i", &response) ;
    
            printf ("\n") ;
    
            if (response < 1 || response > 10)
            printf ("Bad response\n\n") ;
    
    
            else
            ratingcounter[response]++ ;
    
        }
    
        printf ("Rating        Number of responses\n") ;
        printf ("------        -------------------\n\n") ;
    
        for (display = 1; display <= 10; display++)
        printf ("  %i                   %i\n\n", display, ratingcounter[display]) ;
    
    
        return 0 ;
    
    }
    In the after code, I've added a new variable which increases the bad responses.
    I've made the changes that I've made in bold for easier view.

    After code
    Code:
    #include <stdio.h>
    
    int main ()
    
    {
        int ask, i, ratingcounter[i], response, display, br = 0 ;
    
        for (i = 1; i <= 10; i++)
        ratingcounter[i] = 0 ;
    
        printf ("Rate the television show on a scale of 1 to 10\n\n") ;
    
        for (ask = 1; ask <= 10; ask++)
        {
            scanf ("%i", &response) ;
    
            printf ("\n") ;
    
            if (response < 1 || response > 10)
    
            {
                printf ("Bad response\n\n") ;
                br++ ;
            }
    
    
    
            else
            ratingcounter[response]++ ;
    
        }
    
        printf ("Rating        Number of responses\n") ;
        printf ("------        -------------------\n\n") ;
    
        for (display = 1; display <= 10; display++)
        printf ("  %i                   %i\n\n", display, ratingcounter[display]) ;
    
        printf ("\nTotal bad responses = %i\n\n", br) ;
    
        return 0 ;
    
    }
    I've attached the error that I get after I compile/run the after code.
    What is wrong here, please help.
    Attached Images Attached Images Program working fine until I make one change..plz help-error-jpg Program working fine until I make one change..plz help-error1-jpg 
    Last edited by exus69; 11-12-2011 at 04:57 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Contrary to your statement that the "before" code has no problem, both your "before" and "after" code exhibit exactly the same fault.

    It is in this line (in your "before" code), specifically the bits I have highlighted in red.
    Code:
          int ask, i, ratingcounter[i], response, display ;
    i is uninitialized, so accessing its value here (as a dimension of ratingcounter) gives undefined behaviour. Changing i later on, or accessing some element of ratingcounter, does not magically cause the number of elements in ratingcounter to change, and accessing any element of the array ratingcounter also gives undefined behaviour.

    In both the "before" and "after" code, some random area of memory is being trashed. The only difference is that your "before" code exhibits no symptom but (thanks to the addition of the variable br) memory used by "after" code is laid out differently, and the random area of memory being trashed causes a symptom that the operating system detects, so the operating system terminates your program with prejudice.
    Last edited by grumpy; 11-12-2011 at 05:06 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    28

    Problem solved

    Thanks grumpy that solved the problem and you explained the "under the hood" part very well

    Solution: ratingcounter[11] instead of ratingcounter[i]
    Last edited by exus69; 11-12-2011 at 05:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program runs fine, then crashes 5% of the time
    By mat1z in forum C++ Programming
    Replies: 5
    Last Post: 01-30-2011, 04:28 AM
  2. fine with VALGRIND, but not working without it
    By cfdprogrammer in forum C Programming
    Replies: 7
    Last Post: 09-03-2009, 02:41 PM
  3. make change program help
    By Rob4226 in forum C++ Programming
    Replies: 8
    Last Post: 02-20-2008, 01:58 PM
  4. I dont get this working on gcc it works fine in windows
    By wise_ron in forum C Programming
    Replies: 8
    Last Post: 05-08-2006, 05:33 AM
  5. Replies: 6
    Last Post: 06-30-2005, 08:03 AM