Scan characters depending on a pattern

  c++, input, pattern-matching, scanf

I found this code in a competitive programming book for ICPC. The objective is to scan inputs which have a pattern where there are N numbers like 0.4586… (starting with 0. and terminating with …). I tried giving the inputs but on pressing enter key the entire while loop executes at once and I don’t get the chance to input the numbers. So I tried using spaces but that doesn’t work either. Nothing gets scanned in the while loop however the code works fine if I remove the while loop.
Can someone explain this behaviour.

#include <bits/stdc++.h>
using namespace std;

char digits[100];                                       

int main() {
    int N;
    scanf("%d", &N);
    
    while (N--) {                                       
        scanf("0.%[0-9]...", &digits);                  
        printf("the digits are 0.%sn", digits);
    }
}

Source: Windows Questions C++

LEAVE A COMMENT