Hi there,
I need to overload istream in this way:
it needs to get string, then it has to parse this string and get number - can be longer than long long,
and then save this number in another string, which I will give to class method.

So far so good, then I need to return fail bit if there is no digit or "-" at start, again thats alright.
But problem comes when I need to return characters which are not digits after sucesfull digits scan back to istream - no idea how to do this....

Example:

string num;
string a="1215";
//some operations// num="1215"; - thats okay

a="-12"; num="-12" - thats okay

a="sad5456";
//some operations// -> failbit - need to return those characters back to istream

a="45654sds";
//some operations// num="45654" - need to return "sds" back to istream

So how am I supposed to return characters back to istream?? tried putback cycle, but then there was some blank char at istream and it didnt read anything :(

Thanks for answer!

Recommended Answers

All 2 Replies

So you are trying to read in a input from cin and you want to save only the inputs that starts with a number or a negative sign, and any other type of input you want to insert back into istream? Is that right? Do you have to reinsert them into istream? Why can't you just discard them?

I need to overload istream to get number with infinite length (memory limit), so best I think best way is to fill it string via istream and then parse it. If string starts with number, I need to extract it, and return rest of string to istream (if there is anything left), if string starts with other then digit or negative sign character, I need to set failbit and return whole string to istream. Dunno how to specify it more....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.