Search code examples
cscanf

How to get multiple or 1 input in the same line using scanf


I want to get an int and a float in the same line using scanf() which I know how to do, but I also want to be able to quit by entering one input ("-1") how can I do this?

while (input != -1){
  
    printf("Enter: ");
    scanf("%d %f", &input1, &input2);

    if(input1 == -1){
      input = -1;
}

Solution

  • Given the following conditions:

    • each line contains exactly one or two inputs
    • the first input is always a valid integer
    • using only one scanf() family function

    then you cannot do it in C. There must be a logical break to decide how to continue processing the line somewhere, and a single scanf() can’t do it alone.

    Option 1 • Break input into lines and then process with scanf

    Eric Postpischil’s answer shows a way to do it if you preprocess each line as a separate input string — by transforming EOL into EOF (or, more accurately, end of string).

    You can break input into lines any way you feel comfortable. The simplest and least problematic way would be to use a library function like fgets() or readline().

    Option 2 • Invoke scanf multiple times

    It is as simple as saying:

    while (1)
    {
      if (scanf( "%d", &input1 ) != 1) break;
      if (input1 == -1) break;
    
      if (scanf( "%f", &input2 ) != 1) complain_or_something();
    
      do_stuff_with_inputs( input1, input2 );
    }
    

    The brevity trap

    C’s syntax encourages programmers to try to write things with brevity, but this should not be your goal. Rather, your goal should be readability and correctness — even if this means code that requires two or three lines to express itself.

    If you find that a specific chunk of code would definitely benefit from some higher-level abstractions, write a helper function:

    bool read_int_and_float( int * n, float * f )
    {
      if (scanf( "%d", n ) != 1) return false;  // EOF or error       -->  false
      if (*n == -1) return false;               // first input == -1  -->  false
      if (scanf( "%f", f ) != 1) return false;  // EOF or error       -->  false
      return true;                              // both inputs good   -->  true
    }
    

    That function cannot be misread, and as it only does one task (obtaining a specifically-formatted line of input), managing your headspace becomes a billion times easier.

    So now you can write brief, readable code where it counts:

    while (read_int_and_float( &input1, &input2 ))
    {
      do_stuff_with_inputs( input1, intput2 );
    }
    

    You will find that correct and readable code lends itself to rather succinct structures anyway, but don’t shoot yourself in the foot by limiting yourself with a desire to be too pretty.