Thread: got a problem in ltoa ()

  1. #16
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    this is a better base conversion algorithm:

    Code:
    std::valarray<unsigned int> baseConvert(unsigned int value,unsigned int base)
    {
        unsigned int digits = 1+log((double)value)/log((double)base);
        std::valarray<unsigned int>result(digits);
        for(int i=0;i<digits-1;i++)
        {
            result[i]=value%base;
            value/=base;
        }
        result[digits-1]=value;
        return result;
    }
    check out the method of complements
    Last edited by m37h0d; 01-17-2009 at 12:41 PM. Reason: it makes more sense for lowest index to be lowest order of magnitude

  2. #17
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by laserlight View Post
    Why not have Oct2Bin take a string as its argument? If you really want it to take an int as an argument then you are merely expressing the number in binary as opposed to converting it from an octal representation to a binary representation.
    the reason i take an integer argument is that , this is an emulation project , this simple computer uses octal system to manipulate stuffs, and any program written in the language of this system must use octal! so i assume the user inputs his data, now its the time to convert the stuff to the machine language and store it in memory! thats why im trying to convert an integer octal number to its binary string , so that i can dump this low-level instruction in memory!

    Quote Originally Posted by m37h0d View Post
    this is a better base conversion algorithm:

    Code:
    std::valarray<unsigned int> baseConvert(unsigned int value,unsigned int base)
    {
        unsigned int digits = 1+log((double)value)/log((double)base);
        std::valarray<unsigned int>result(digits);
        for(int i=0;i<digits-1;i++)
        {
            result[i]=value%base;
            value/=base;
        }
        result[digits-1]=value;
        return result;
    }
    check out the method of complements
    many thanx pal . i appreciate it.

    i really need to know why my program crashes! please some one help me ! this part plays a crucial role in my project! if i fail to solve this , my project fails to dump instruction in machine language (binary format) into the memory (a 2D array)!!
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  3. #18
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    well, first thing's first:

    quit doing it the hard way and see if your problem goes away. if not, then debugging will be easier!

    also, i don't see why you're forcing the user to input something in valid octal representation - just convert dec->oct. any number is a valid octal number, it just may be represented in a different format (in our case, decimal)
    Last edited by m37h0d; 01-17-2009 at 01:10 PM.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by m37h0d
    this is a better base conversion algorithm
    No, it is a more general base conversion algorithm, but "worse" in this case since it does not take advantage of the one to one mapping between octal digits and three consecutive binary digits.

    Quote Originally Posted by Masterx
    the reason i take an integer argument is that , this is an emulation project , this simple computer uses octal system to manipulate stuffs, and any program written in the language of this system must use octal!
    That does not seem like a convincing reason to use an int argument. Once you have stored it as an int value, the original representation of the input no longer matters.

    Quote Originally Posted by Masterx
    so i assume the user inputs his data, now its the time to convert the stuff to the machine language and store it in memory! thats why im trying to convert an integer octal number to its binary string , so that i can dump this low-level instruction in memory!
    Why waste time reading the input as an int and then converting the int to a binary string? You might as well read the input as a numeric string in octal representation and then do the conversion to binary representation directly.

    Quote Originally Posted by Masterx
    i really need to know why my program crashes!
    Use a debugger to step through the program. This can help you pinpoint the crash.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    arguably so i suppose but i'm not totally convinced that's a substantive advantage

  6. #21
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by m37h0d View Post
    well, first thing's first:

    quit doing it the hard way and see if your problem goes away. if not, then debugging will be easier!

    also, i don't see why you're forcing the user to input something in valid octal representation - just convert dec->oct. any number is a valid octal number, it just may be represented in a different format (in our case, decimal)
    this feature is going to be implemented ! (some part of the program now supports decimal input too, )
    Quote Originally Posted by laserlight View Post
    No, it is a more general base conversion algorithm, but "worse" in this case since it does not take advantage of the one to one mapping between octal digits and three consecutive binary digits.


    That does not seem like a convincing reason to use an int argument. Once you have stored it as an int value, the original representation of the input no longer matters.


    Why waste time reading the input as an int and then converting the int to a binary string? You might as well read the input as a numeric string in octal representation and then do the conversion to binary representation directly.


    Use a debugger to step through the program. This can help you pinpoint the crash.
    its done.i'll do it this way. so far so good.
    before i head for debugging stuff(which i dont know how the stuffs work !) is it right to say that the program is ok ?. i mean i didnt do anything contrary to the basics of C++!?
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Masterx
    is it right to say that the program is ok ?. i mean i didnt do anything contrary to the basics of C++!?
    Well, what is the current code for which you want such an evaluation to be made?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    ive finaly figured it out! the crash is because of string str;
    in the loop
    Code:
                for (int i=0;i<9;i++)
                 {
                       memory[4][i]=str[i];
             
                 }
    i tried to access the str by using index ! and it showed me that string str is rather different than string str[length] !! so that was the catch!
    after that i noticed that the function doesnt return anything ! at least if in returns anything , that thing is empty!! why ? because i tried to copy str elements to a buffer using
    Code:
    char buffer[12];
    int length=0;
    
    length=str.copy(buffer,9,0);
    buffer[length]='\0';
    and then tried to print the stuffs (buffers) on the console screen! but nothing was printed!
    well where is the catch this time ? is there any other ways to copy this (string str) form of string to another string (like string str1[n])?
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  9. #24
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by laserlight View Post
    Well, what is the current code for which you want such an evaluation to be made?
    the codes we were discussing !here:
    for (int i=0;i<9;i++)
    {
    memory[4][i]=str[i];

    }
    (it seems i was wrong , idid it the wrong way .)
    Last edited by Masterx; 01-18-2009 at 05:20 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  10. #25
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    Quote Originally Posted by Elysia View Post
    If you work with arrays, use std::tr1::array or boost::array.
    Can you explain why? What's wrong with the built in arrays?

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    They are unsafe (at least boost::array will assert if you access out-of-bounds), does not provide you any means to get its length, and does not always work with well standard containers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by manzoor
    What's wrong with the built in arrays?
    Read Stroustrup's answer to the FAQ: What's wrong with arrays?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    what about me then ? any help?! how to copy str struffs to a char * based string?!!
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Masterx
    what about me then ? any help?! how to copy str struffs to a char * based string?!!
    Why do you want to use C style null terminated strings in the first place? I am afraid that I find your posts #23 and #24 to be rather incoherent due to the lack of context.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    well, in post 23 and 24 im saying that because i declared str as "string str" i couldnt use subscripting to point to each elements of str and then copying them to another string .
    when i tried to use these commands
    Code:
            for (int i=0;i<9;i++)
                 {
                       memory[4][i]=str[i];
             
                 }
    , the application compiles fine , without any error, but at runtime it crashes! so far i explained post 23 . and 24
    now why C Style null terminated string ! because to me they are easier to manipulate . i can convert them to integer if needed (using atoi() , which i cant use string str sytle ) and i can point to any element of it , whenever i want to ( using *(ptr+counter) ) while i cant use string str format !

    see to me C Style strings are awesome , I as a rookie dont need to do stuffs the hard way and get stuck in the middle of the way ! and you know thats because i dont know much about C++ and its strings . (and sadly im on a project that needs these knowledge ! ).

    now , i used that string str format , and i need to copy the stuff at least to another string that can be subscripted!(like string str2[10]) .

    hope it is clearer now .
    (and most of the strings manipulations are done using C Style strings in my project so far ) .
    Last edited by Masterx; 01-18-2009 at 09:50 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM

Tags for this Thread