eliaskosunen/scnlib

Document or fix interaction of scn::prompt and result range()

dancingbug opened this issue · 3 comments

Hi. Modifying the first example given in the README, I have:

#include <scn/scn.h>
#include <cstdio>

int main() {
    int i, j;
    // Read an integer from stdin
    // with an accompanying message
    auto res = scn::prompt("What's your favorite number? ", "{}", i);
    scn::scan(res.range(), "{}", j);
    printf("Oh, cool, %d!\n", i);
    printf("Oh, even cooler, %d!\n", j);
}
// Example invocation:
// What's your favorite number? 345 6789
// Oh, cool, 345!
// Oh, even cooler, 9!

My "678" got eaten unexpectedly!
Please make scnlib give it back, or if this is expected, document obviously the behavior of using range() to scan leftover input from different possible types.

I am using version 1.1

The file handling stuff is a little iffy and hard to test when using stdin, but this definitely should have been caught. I'll look into this.

Temporary fix in v1.1.1, the snippet in your message now works.

Keeping the issue open for a more substantial fix, including design changes.

For the record, this has been fixed in 2.0.

#include <scn/scan.h>

int main() {
    auto res1 = scn::prompt<int>("What's your favorite number? ", "{}");
    auto res2 = scn::scan<int>(res1->file(), "{}");
    printf("%d, %d\n", res1->value(), res2->value());
}