Quick C++ question

mosdef

Banned
May 14, 2000
2,253
0
0
How do I convert chars to integers (and vice-versa). Also, can I use the increment operation with chars? Thanks.

-mosdef
 

satori

Senior member
Nov 2, 1999
471
0
0
atoi() is the quick and easy one.

I think the function definitions are in stdlib.h

 

mosdef

Banned
May 14, 2000
2,253
0
0
What are number-like operations that can be done to chars? Any web site out there where I can get more info? Heres my problem: I want to take an input of an integer, but it is possible for the user to enter a char. So I was thinking of reading in a char, unless there is a way to check whether an input is an integer or not. Let's say I can't use atoi() and itoa().

-mosdef
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
mosdef:

A char is still a number. A char (unless it's a wide char) is only 8 bits, and the smallest (short on most systems) int is 16 bits. atoi() is a good function, but itoa() isn't ANSI (not last I checked anyway). You might look into the sprintf() function, as it will allow you to convert the int into a char array, and vice versa. Just note that the maximum value for an unsigned char is 255, and although you can zero extend (by way of a cast) a char to an int, if you go over 255, you'll lose precision on the conversion back to char.

If you need examples, let me know.