Thread: Simple coversion program hanging on input

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    69

    Simple coversion program hanging on input

    Dear All,

    I have written this programme that reads a set of n digits and converts them into a single decimal integer. For example if the input is an array of integers 2,3,4,12,4 then the output should be 234124. My program is hanging when I input a digit. Below is my code, please tell me the error:

    Code:
    #include<stdio.h>
    int main()
    {
        int *a,sum=0,i,n;
        printf("enter the number of digits");
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
               scanf("%d",&a[i]);
        }
        for(i=1;i<=n;i++)
        {
        printf("%d",a[i]);
        }
        system("pause");
        return 0;
    }
    Thank you for your reply.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Where are you allocating any memory for your variable a?

    Note: Your compiler should be warning you about uninitialized variables. If you are not getting them you need to check your compiler settings and insure warnings are being generated, and select the highest level of warnings.

    Jim

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    16
    No need to use a pointer here, just declare a as array, with some max size that you want.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Also, remember array indexes are zero based.

    Just a nit, but there is no conversion happening. The way your output is being handled has the effect of it looking as if a conversion has taken place. Regardless, you still get the output you expected.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Problem solved using the method suggested by Stevan. I still wanted to keep the array size dynamic, so that when the user
    can enter as many numbers as he/she wants.

    @Jimblumberg - As far as allocating memory to variable 'a' is concerned, what if I assign the value 0 to *a and then input numbers. Could you show me how to dynamically allocate the memory? Thank you.

    Quote Originally Posted by jimblumberg View Post
    Where are you allocating any memory for your variable a?

    Note: Your compiler should be warning you about uninitialized variables. If you are not getting them you need to check your compiler settings and insure warnings are being generated, and select the highest level of warnings.

    Jim

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    To dynamically allocate memory in C you use malloc to allocate the memory. Here are a couple of links that should help explain how to use dynamic memory allocation in C: Advanced Memory Management: Dynamic Allocation, Dynamic Data Structures: Malloc and Free, and DYNAMIC ALLOCATION.

    Don't forget to use malloc you must include stdlib.h.

    Jim

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Thank you for your prompt help.

    Quote Originally Posted by jimblumberg View Post
    To dynamically allocate memory in C you use malloc to allocate the memory. Here are a couple of links that should help explain how to use dynamic memory allocation in C: Advanced Memory Management: Dynamic Allocation, Dynamic Data Structures: Malloc and Free, and DYNAMIC ALLOCATION.

    Don't forget to use malloc you must include stdlib.h.

    Jim

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    1

    you can make it yourself ,here is my help for you

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    void main()
    {
        long int num[10],i,a,sum=0;
    
       printf("enter the number of terms::");
       scanf("%ld",&a);
    
       for(i=0;i<a;i++)
       {
        printf("\nenter the %d element::",i+1);
          scanf("%ld",&num[i]);
       }
    
        for(i=0;i<a;i++)
        {
           if(num[i]<10)
           sum=sum*10+num[i];
             else
              sum=sum*100+num[i];
          }
    
       printf("\n the output is = %ld",sum);
    
        getch();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-27-2011, 09:46 PM
  2. Program is hanging
    By mramazing in forum Networking/Device Communication
    Replies: 7
    Last Post: 03-18-2009, 04:13 PM
  3. Hang man program hanging
    By pieisgood in forum C++ Programming
    Replies: 14
    Last Post: 02-21-2009, 06:08 PM
  4. Can't figure out what keeps hanging up my program
    By shays in forum C Programming
    Replies: 7
    Last Post: 11-12-2007, 02:59 PM
  5. Code is hanging - Very simple C (Beginner)
    By pc_doctor in forum C Programming
    Replies: 10
    Last Post: 10-29-2007, 05:05 PM