Thread: No input detection

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    No input detection

    I am writing a program to find roots of a cubic function, however, I am getting my inputs from a separate text file. So, there might be a case where I get something like this:
    1 2 3 4
    5 6 7 8
    9 0 1

    There might not be enough variables (which I need 4 of) in order to run the program, so scanf is stuck in an infinite loop waiting for an input. Is there something that can detect this problem. So far I have this worked out, but it doesn't work:
    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    int main()
    {
        double a, b, c, d, f, x=50.0;
        printf("enter:");
        scanf("%lf", &d);
        if(isspace(d))
            printf("No input.");
        return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Feb 2011
    Posts
    2
    I have tried this too, but no luck:

    Code:
    double a, b, c, d, f, x=50.0;
        printf("enter:");
        int ret1=scanf("%lf", &a);
        int ret2=scanf("%lf", &b);
        int ret3=scanf("%lf", &c);
        int ret4=scanf("%lf", &d);
        if(ret1||ret2||ret3||ret4==0)
            printf("No input.");
    It seems like I need a dynamic input tester of some sort. Because it scans for the first value, for the second value, for the third, and so on, but if I press enter, there is nothing happening.

  3. #3

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by oboy View Post
    I am writing a program to find roots of a cubic function, however, I am getting my inputs from a separate text file. So, there might be a case where I get something like this:
    1 2 3 4
    5 6 7 8
    9 0 1

    There might not be enough variables (which I need 4 of) in order to run the program, so scanf is stuck in an infinite loop waiting for an input. Is there something that can detect this problem. So far I have this worked out, but it doesn't work:
    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    int main()
    {
        double a, b, c, d, f, x=50.0;
        printf("enter:");
        scanf("%lf", &d);
        if(isspace(d))
            printf("No input.");
        return 0;
    
    }


    You may want to experiment with this....

    Code:
    #include <stdio.h>
    int main (void)
      { double input[4];
         int incount = 0;
     
        while ( incount < 4)
          { rprintf ("Enter value #%d :",incount); 
            if (scanf("%lf", input[incount]) > 0)
              incount++; 
            printf "\n"); }
    
         return 0; }
    To see how this works, check out the return value from scanf() in your documentation...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IDE for C embedded advanced editing
    By Undici77 in forum Tech Board
    Replies: 32
    Last Post: 01-16-2010, 05:17 PM
  2. Changing integer input into string, wrong value
    By Ace2187 in forum C Programming
    Replies: 3
    Last Post: 11-14-2009, 02:28 AM
  3. Allocating input to an array
    By ij.cohen in forum C Programming
    Replies: 2
    Last Post: 10-12-2009, 10:48 AM
  4. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  5. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM