an atoi replacor

This is a discussion on an atoi replacor within the C++ Programming forums, part of the General Programming Boards category; this is a function imade the other day. i would like to know what do you think about it, and ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    29

    an atoi replacor

    this is a function imade the other day. i would like to know what do you think about it, and if you have any ideas how to improve it.
    Code:
    #include <iostream>
    #include <string>
    
    std::string ITOA(long Num){
        std::string result="";
        long * a = new long;
        long unsigned b = 0;
        for(*a=1;reinterpret_cast<long>(Num/(*a))>0;(*a)*= 10){
            b++;
        }
        *a/=10;
        long c = *a;
        while(result.length()<b){
            result+=(Num/(*a))+48;
            Num -= int((double(Num)/(c*100))*100)*c;
            if(*a!=1){
                *a/=10;
            }
        }
        delete a;
        return result;
    }
    long ITOA(std::string Num){
        long * result = new long;
        *result = 0;
        long NumOfZeroes = Num.length();
        long * Num1 = new long;
        for(long i=0;i<Num.length();i++){
            *Num1 = long(Num.c_str()[i])-48;
            for(long b=0;b<NumOfZeroes;b++){
                *Num1 *=10;
            }
            (*result) += *Num1;
            NumOfZeroes--;
        }
        return (*result)/10;
    }
    btw, c-strings support is almost the same although i didnt include it.

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Anything wrong with stringstreams?
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  3. #3
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69
    I really don't understand why you're allocating memory on the heap for those local variables... and I believe the second function should maybe be called "ATOI" instead, eh?
    And yeah, you're not gonna get the Nobel prize for this code or anything, if that's what you think. C++ already have similar functionality implemented through "stringstreams". However, I guess and hope you learned something from coding this... =)

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    29
    well, the heap is just for practice... and i prefer not to use functionality i can produce myself. anyway, i just wanted to know if you have any idea on how to improve it.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Quote Originally Posted by gftmc
    and i prefer not to use functionality i can produce myself.
    Why?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Me
    Join Date
    Jul 2006
    Posts
    71
    Quote Originally Posted by CornedBee
    Why?
    Yes...why?

    Why recreate the wheel?

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    29
    first, it is a good practice. second, im more content to use something i prduced. in small functions like this, it is easy.

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by gftmc
    first, it is a good practice. second, im more content to use something i prduced. in small functions like this, it is easy.
    Even though your functions have only been tested by you, while the standard library has been tested by everyone that uses your implementation of C++? Even though your functions may be less optimized than one from the standard library? Even though the functions that you build must use standard functions that you didn't build? What makes the standard functions that you build with more trustworthy than the standard functions that you don't?

    I agree that it's good practice to try to replicate certain functionality included in the standard library. However, in a serious application, you're better off leaving all that behind.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Quote Originally Posted by pianorain
    Even though your functions have only been tested by you, while the standard library has been tested by everyone that uses your implementation of C++? Even though your functions may be less optimized than one from the standard library? Even though the functions that you build must use standard functions that you didn't build? What makes the standard functions that you build with more trustworthy than the standard functions that you don't?
    And, arguably most importantly, more people are familiar with the standard library.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    You will never invent that good wheel... as the standard one is...
    I like dots

  11. #11
    Registered User
    Join Date
    Jun 2006
    Posts
    29
    ok, you didnt understood me. i like to reproduce functions, only because i feel better when using them. i use stl Classes always. i dont intend to make use of them in a large project. (something with a company or so) i just use them myself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble with atoi
    By NewbGuy in forum C Programming
    Replies: 2
    Last Post: 05-22-2009, 11:55 PM
  2. help with this
    By tyrantil in forum C Programming
    Replies: 18
    Last Post: 01-30-2005, 03:53 PM
  3. Problem with atoi in C
    By garagebrian in forum C Programming
    Replies: 5
    Last Post: 08-24-2004, 01:59 PM
  4. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 12:17 AM
  5. how to use atoi!!!
    By moon in forum C++ Programming
    Replies: 4
    Last Post: 01-21-2002, 08:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21