Thread: How to find first bad number from float

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    6

    How to find first bad number from float

    Hi.I have this problem : I am entering numbers to float ... I want program to find out, which first number is not from specific interval. How to do it ? Example: Enter input : 5 10 20 30 50 46 . 30 is invalid. Here is the code :

    Code:
    while(scanf("%f",&input)!=EOF || input==0)
    {
    sum=input+sum;
    if (getchar() == '\n') break;
    };        //user input ended with enter/0
    
    if(input!=100 ||input!=50 ||input!=20 ||input!=10 ||input!=5 ||input!=2  ||input!=1 ||input!=0.5 ||input!=0.2 ||input!=0.1 ||input!=0.05 ||input!=0.02 ||input!=0.01) 
    printf("%f is invalid\n",input);
    Any help appreciated

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should be validating your input inside the loop, after each read, presumably before adding it to "sum".

    Code:
    if(input!=100 ||input!=50 ||input!=20 ||input!=10 ||input!=5 ||input!=2  ||input!=1 ||input!=0.5 ||input!=0.2 ||input!=0.1 ||input!=0.05 ||input!=0.02 ||input!=0.01)
    Your logic is flawed ... this will always be true.

    For instance, if "input" is 100, then "input!=50" is true, and since you're logic OR'ing, the whole expression will be true.

    You can make your validation much nicer with a simple loop and array - I'll leave the implementation up to you. I'd also strongly recommend putting your validation code in a separate function and returning a success/fail value (and react accordingly in the calling code).

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    6
    Thanks,so I made something like this:
    Code:
    
    while(scanf("%f",&input)!=EOF || input==0)
    {
    //scanf("%f",&input);
    sum=input+sum;
    if (getchar() == '\n') break;
    };
    
    for (int i=0;i<14;i++)
    {
        if(input!=notes[i])
        {
            printf("%f is invalid\n",input);
        }
            
    }
    I have all those numbers in array. Problem is,that printf is printing me just first number of float,for example: 10 20 30 40 50. 10 is invalid... I expected "30 is invalid"
    Last edited by Jakub Elia; 03-23-2015 at 03:55 PM.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It looks like you have the basic idea of the array/loop setup, but it's still not entirely correct. Also, you're still validating the numbers after the loop has ended. There are a few other issues with the code, also, but I figured we'd tackle them a few at a time.

    At this point, you should post your entire program (or if it's pretty big, just a simplified yet complete version we can compile and run ourselves).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to reserved int number and print as float number ?
    By alon4963 in forum C Programming
    Replies: 1
    Last Post: 11-24-2013, 06:46 AM
  2. float number division
    By hoistyler in forum C Programming
    Replies: 6
    Last Post: 01-14-2009, 03:13 AM
  3. Triming a float number
    By RocketMan in forum C Programming
    Replies: 3
    Last Post: 10-01-2008, 08:31 AM
  4. To find max float no.
    By Karam in forum C Programming
    Replies: 3
    Last Post: 07-22-2004, 02:53 AM
  5. testing if a number is a float
    By UnknownImage in forum C Programming
    Replies: 11
    Last Post: 02-13-2003, 11:10 AM

Tags for this Thread