Thread: converting chars to ints

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    5

    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

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    why do you want to do this .... surely better would be to make your stack a template!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User Hoegje's Avatar
    Join Date
    Aug 2001
    Posts
    4
    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 !!

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    5
    actually, i am quite the newbie at programming, and I am trying to learn the basics before any advanced stuff. Can anybody help me?

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User ski6ski's Avatar
    Join Date
    Aug 2001
    Posts
    133
    >>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.
    Last edited by ski6ski; 09-01-2001 at 07:09 AM.
    C++ Is Powerful
    I use C++
    There fore I am Powerful

  7. #7
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Your casting a pointer to int to float?

    [code]
    int main(void)
    {
    int* i = new int(5);
    float j = *i;

    delete i;

    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting two 32-bit ints to a u_int64_t...
    By elfjuice in forum C Programming
    Replies: 8
    Last Post: 02-17-2008, 01:31 AM
  2. joining two chars together and converting to INT
    By tehprince in forum C++ Programming
    Replies: 5
    Last Post: 12-21-2007, 02:08 PM
  3. converting ints to chars
    By e66n06 in forum C Programming
    Replies: 4
    Last Post: 07-28-2007, 03:52 PM
  4. Converting Chars to Ints
    By sycorax in forum C++ Programming
    Replies: 2
    Last Post: 09-06-2005, 10:40 PM
  5. atoi not converting chars to ints?
    By C++Child in forum C++ Programming
    Replies: 13
    Last Post: 10-08-2004, 03:59 PM