-3

I want to write below code with proper if clause.

  int main()
    {
        float a,b;
        printf("Enter Two number with space:\n");
        scanf("%f %f",&a,&b);
        //if(%a || b are char%)
           printf("Not correct format!")
        else
           printf("a and b: %.2f %.2f",a,b);
        
        return 0;
    }
Kristina
  • 261
  • 2
  • 13
  • It is not as easy – 0___________ Aug 09 '17 at 00:00
  • 3
    Amongst other questions, see one from yesterday: [Is there a way to check if a string can be a float in C?](https://stackoverflow.com/questions/45554639/) If you want to check a string, then `sscanf()` or `strtod()` are appropriate. There are almost certainly others that are relevant. – Jonathan Leffler Aug 09 '17 at 00:06
  • Please read the question first! After vote it or answer it. There is no same question with this in stack overflow – Kristina Aug 09 '17 at 00:34
  • 2
    Don't get snippy with those who are trying to help you; it doesn't help, – Jonathan Leffler Aug 09 '17 at 02:25
  • What exactly are you trying to find out? If you want to know whether both conversions succeeded, then you will need to test the value returned by `scanf()`, as you were told in the first comment. If you want to check that the rest of the line doesn't contain anything other than blanks, you have to work harder, as shown in the question linked to from yesterday. With no check on the result of `scanf()`, there is nothing you can write in the `if` clause after it that tells you reliably what happened during the input. If there's something else you need to know, you need to explain what it is. – Jonathan Leffler Aug 09 '17 at 02:28
  • `"Enter Two number with space:\n"` --> Is a space between the numbers _required_? Code like `scanf("%f %f",&a,&b);` tolerates a space but does not require it like `"123.4-567.8"` scans OK – chux - Reinstate Monica Aug 09 '17 at 02:45

1 Answers1

1
   #define INT_CONVERTED       (1 << 0)
    #define FLOAT_CONVERTED     (1 << 1)
    int ReadNumber(const char *str, double *db, int *in)
    {

        int result = (str == NULL || db == NULL || in == NULL) * -1;
        int len = 0;
        char *tmp;

        if (result != -1)
        {
            tmp = (char *)malloc(strlen(str) + 1);
            strcpy(tmp, str);
            for (int i = strlen(str) - 1; i >= 0; i--)
            {
                if (isspace(tmp[i]))
                {
                    tmp[i] = 0;
                    continue;
                }
                break;
            }
            if (strlen(tmp))
            {
                if (sscanf(tmp, "%lf%n", db, &len) == 1 && strlen(tmp) == len)
                {
                    result |= FLOAT_CONVERTED;
                }
                if (sscanf(tmp, "%d%n", in, &len) == 1 && strlen(tmp) == len)
                {
                    result |= INT_CONVERTED;
                }
            }
            free(tmp);
        }
        return result;
    }
0___________
  • 60,014
  • 4
  • 34
  • 74
  • 4
    See also [Is there a way to check if a string can be a float in C?](https://stackoverflow.com/a/45555411/15168) where this code previously appeared. – Jonathan Leffler Aug 09 '17 at 00:09