-
converting chars to ints
hello everyone
I am making a stack calculator. I have the code for the pop and push functions etc, but I have one problem. I need to convert all the numbers in the array to ints(doesnt have to be in the array). I have tried casting, and adding 0 to the number, but all they did was convert the number to the ascii representation. I tried atoi(), but apparently that will not work with one element of an array. Does anybody have any ideas? Thank you
-
why do you want to do this .... surely better would be to make your stack a template!
-
Indeed, make it a template functions, or maybe even just use the STL with it's container types. I'm sure there will be some sort of stack already available for you, although I am not sure which container to use exactly. It can't be hard to find though.
Why do some work again, when it has already been done for you ? Be as lazy as possible and use the STL !! ;)
-
actually, i am quite the newbie at programming, and I am trying to learn the basics before any advanced stuff. Can anybody help me?
-
you need a combination of static_cast<int>(tobecast) and a loop.Loop through your array and cast each element.Depending on what you are casting from it may be necessary to store the new values in a different array.
-
>>I need to convert all the numbers in the array to ints
Here is the reason why:
'conversion' : cannot convert from 'type1' to 'type2'
The compiler was unable to cast from ‘type1’ to ‘type2.’ If you’ve encountered this error on code which compiled with an earlier version of Visual C++, please read Technote: Improved Conformance to ANSI C++ for more information.
The following example illustrates this error.
Code:
int main()
{
int *i;
float j;
j = (float)i; /* cannot cast from
pointer to int to float */
}
edit: This is from MSDN for MSVC 6.0.
-
Your casting a pointer to int to float?
[code]
int main(void)
{
int* i = new int(5);
float j = *i;
delete i;
return 0;
}