-2

Normally I would use something like this:

    double value;
if (scanf("%lf", &value) == 1)
    printf("It's float: %f\n", value);
else
    printf("It's NOT float ... \n");

But this time I need to read two numbers at once

scanf("%lf %lf", &x, &y);

How do I check that?

Mr.Smith
  • 37
  • 1
  • 6

2 Answers2

1

As @SRhm mentioned in the comment section, you simply can use:

scanf("%lf %lf", &x, &y) == 2

to get two numbers from the user input. A quote from scanf - C++ Reference explain the return value of the function:

On success, the function returns the number of items of the argument list successfully filled.

scanf will return an integer representing the number of variables successfully read from the user input.

Omarito
  • 577
  • 6
  • 22
0

Some programmer dude has a good point. Unfortunately, this technique won't work if only the second input is a double but you want to distinguish that from the case where both are not doubles. In order to distinguish different cases, you might want to take in the input as a string by using the fgets() function, and then parse the string into different parts separately using sscanf() on the string multiple times.

Study the C standard library in detail and you'll find many wonderful library functions that can help you do many things.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
ComputerNerd
  • 63
  • 10