Thread: Could'nt Terminate the Command Smoothly when Wrong type of input is given?PleaseHelp

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    1

    Question Could'nt Terminate the Command Smoothly when Wrong type of input is given?PleaseHelp

    Code:
    main()
    {
    double a=0,b=1,p=0,m,t=0;
    clrscr();
    printf("\n\n\n\n\n\n\n    Give Number of Desired Terms In Fibonacci Series : ");
    scanf("%lf",&m);
    do{
    a=0,b=1,t=0,p=0;
    printf("\n    ");
    do{
    t=a+b;
    printf("%.0lf    ",t);
    b=a;
    a=t;
    p++;
    }while(p!=m);
    printf("\n\n    Give Number of Desired Terms In Fibonacci Series : ");
    scanf("%lf",&m);
    }while(m!=0);<<Could'nt terminate loop smoothly for 
                      <<Non numeric input
    getch();
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You will need to see what scanf returns to see if it succeeded in reading what you told it to read. If it did not, you will need to clear out the pending input and try again.


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

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Scanf() is notoriously buggy with non-numerical input. If you really insist on using it, you could always fgets() to strtoul()/strtod().

    But you don't need to do any of that do/while looping for a simple fibonacci. Here's an array-based partial example that you might want to study:
    Code:
    array[0] = 0;
    array[1] = 1;
    
    for (i = 0; i < len; i++) {
        array[i] = array[i-1] + array[i-2];
    }
    Now, make it work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-31-2011, 04:50 PM
  2. Replies: 1
    Last Post: 09-24-2009, 01:28 AM
  3. How can terminate input when input specifed char?
    By Mathsniper in forum C Programming
    Replies: 5
    Last Post: 12-11-2006, 09:59 AM
  4. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  5. Moving a bitmap around the screen -- SMOOTHLY
    By Leeman_s in forum Windows Programming
    Replies: 16
    Last Post: 01-12-2003, 10:29 PM

Tags for this Thread