why my programming is termination ...

This is a discussion on why my programming is termination ... within the C Programming forums, part of the General Programming Boards category; hi .i was just trying to know what i observer with this code... Code: #include<stdio.h> #include<conio.h> void main() { int ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    17

    why my programming is termination ...

    hi .i was just trying to know what i observer with this code...
    Code:
     #include<stdio.h>
      #include<conio.h>
      void main()
       {
        int a[10],i;
        clrscr();
        for(i=0;i<10;i++)
         scanf("%d",&a[i]);
        for(i=0;i<10;i++)
        printf("%d",a[i]);
       getch();
        }
    it is asking for input but accidentally i entered a dot (.)
    and it terminated why so...

    Code:
     #include<stdio.h>
      #include<conio.h>
      void main()
       {
        int a[10],i;
        clrscr();
        for(i=0;i<10;i++);
         scanf("%d",&a[i]);
        printf("%d",i);
       for(i=0;i<10;i++)
        printf("%d",a[i]);
    
       getch();
        }
    The value of i is 10...why

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If it wasn't ever 10, how would your loop ever end? Also, scanf doesn't like it when you mess up your input. Furthermore, main is not a void function. It returns an int; always.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    for(i=0;i<10;i++);
    Should not have the ';' on the end. Plus comments as noted by quzah.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    it is asking for input but accidentally i entered a dot (.)
    and it terminated why so...
    If scanf() is looking for a number in a stream and it comes across a character that isn't a digit or whitespace, it returns an error code and doesn't store anything in the variables passed to it. If you want to handle such an error, check the return value of scanf(); it will be the number of items successfully read (1 for "%i", 3 for "%i%i%i", etc).
    Code:
    int c;  /* must be an int to hold EOF */
    
    for(i = 0; i < 10; i ++) {
        printf("Enter number %d/10: ", i);
        while(scanf("%d", &x) != 1) {
            while((c = getchar()) != '\n' && c != EOF);
            printf("Invalid number, try again: ");
        }
    }
    There are several FAQs related to this.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. zombie to exist after the termination of main program..
    By anilchowdhury in forum Linux Programming
    Replies: 0
    Last Post: 02-22-2008, 11:35 AM
  2. Replies: 4
    Last Post: 06-23-2006, 07:03 PM
  3. **///Re: Abnormal prg termination
    By jhsurti in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 01:33 AM
  4. abnormal program termination
    By ProLin in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2002, 08:56 PM
  5. Termination
    By awkeller in forum C Programming
    Replies: 2
    Last Post: 10-14-2001, 08:24 AM

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