Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Analog scanf("%1d") for C++ (std::cin)

Tags:

c++

I'm looking for some analog scanf("%1d", &sequence) for std::cin >> sequence.

For example:

for ( ; scanf("%1d", &sequence) == 1; ) {
    printf("%d ", sequence);
}
stdin: 5341235
stdout: 5 3 4 1 2 3 5 

How does it work in C++ ?!

for ( ; std::cin >> *some_magic* sequence; ) {
    std::cout << sequence << " ";
}
like image 349
calav666 Avatar asked Jan 04 '15 15:01

calav666


1 Answers

you can do this if you want (the sequence variable must be of type char)

for ( ; std::cin.read(&sequence,1); ) {
    sequence-='0';
    std::cout << sequence << " ";;
}
like image 156
Meninx - メネンックス Avatar answered Oct 05 '22 03:10

Meninx - メネンックス