17

I want to get a string of numbers one by one, so I'm using a while loop with cin.get() as the function that gets my digits one by one.

But cin.get() gets the digits as chars and even though I'm trying to use casting I can't get my variables to contain the numrical value and not the ascii value of the numbers I get as an input.

Mike
  • 47,263
  • 29
  • 113
  • 177
Quaker
  • 1,483
  • 3
  • 20
  • 36

1 Answers1

36

cin.get can’t parse numbers. You could do it manually – but why bother re-implementing this function, since it already exists?*

int number;
std::cin >> number;

In general, the stream operators (<< and >>) take care of formatted output and input, istream::get on the other hand extracts raw characters only.


* Of course, if you have to re-implement this functionality, there’s nothing for it.

To get the numeric value from a digit character, you can exploit that the character codes of the decimal digits 0–9 are consecutive. So the following function can covert them:

int parse_digit(char digit) {
    return digit - '0';
}
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 1
    I am not allowed to use functions I didn't learn in lectures. – Quaker Nov 16 '12 at 18:04
  • 11
    @Eran Oh great, what a stupid requirement. Well, let me amend the answer. – Konrad Rudolph Nov 16 '12 at 18:04
  • The `static_cast` isn't needed. The compiler will convert the `char` value to `int` without being told. – Pete Becker Nov 16 '12 at 18:08
  • still using functions I'm not allowed to use. I can only use cin.get() and casting for the job. – Quaker Nov 16 '12 at 18:10
  • 2
    @Eran it's time to start thinking with your own head ;) – Barney Szabolcs Nov 16 '12 at 18:16
  • @Pete And I hate it for this. The cast *should* be required. I put it to make it clear that the cast was intended and is not a bug. – Konrad Rudolph Nov 16 '12 at 18:23
  • @Eran What do you mean? I *am* only using `cin.get()` and casting. Or aren’t you allowed to write your *own* functions? – Konrad Rudolph Nov 16 '12 at 18:24
  • @KonradRudolph - you put it in to make it clear that the **conversion** was intended. Nevertheless, it's a widening conversion, and it always preserves values. If you insist on `static_cast` for every widening conversion you're going to be writing an awful lot of casts. – Pete Becker Nov 16 '12 at 18:55
  • You really should check if it's a number before parsing. – Mooing Duck Nov 16 '12 at 18:58
  • @KonradRudolph - I take it back. There's no conversion here. `digit - '0'` has type int, and that's the type being returned. – Pete Becker Nov 16 '12 at 19:00
  • @Pete I concede the point about widening conversion in general. But the fact that `digit - '0'` returns an `int` in particular is IMHO pretty horrible (yes, other languages handle it the same, but nevertheless). But yes, the cast is redundant here no matter how you dice it. – Konrad Rudolph Nov 16 '12 at 19:04
  • @MooingDuck Yes. But then the function would have to react properly to this, and all clean means of reacting would be way beyond anything that OP seems to be able to use, and I prefer not to show a way of reacting improperly. My function thus requires a check *before* calling it. – Konrad Rudolph Nov 16 '12 at 19:06
  • 4
    @KonradRudolph - back in the olden days, some C programmers would cast the returned value of `malloc` "for documentation": `int *ip = (int*)malloc(3);`. If they forgot to `#include ` the compiler treated `malloc` as a function returning `int` (yes, I **did** say this was ancient C). On architectures where an `int` was a different size from a pointer this caused major problems. So I've **never** been a fan of using casts to document that the programmer expects the compiler to do what it would do anyway, because that cast can turn a compile-time error into a runtime error. – Pete Becker Nov 16 '12 at 19:17