Search code examples
c++cin

how to take formatted input in c++?


Well i know how to do this in c, for example:

#include <stdio.h>

int main()
{
    int a,b,c;
    scanf("%d:%d,%d",&a,&b,&c);
    printf("%d %d %d",a,b,c);

    return 0;
}

But for to do this in c++? Can cin be use like scanf?


Solution

  • Since input format is "%H:%M,%s" I suspect that a time is an input.
    In such case there is better simpler way to do it:

        std::tm t{};
        while(std::cin >> std::get_time(&t, "%H:%M,%S")) {
            std::cout << std::put_time(&t, "%H %M %S") << '\n';
        }
    

    https://godbolt.org/z/Y5o9cYc4G