Thread: got a problem in ltoa ()

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Masterx
    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 .
    Why do you need to point to the elements of the string? You certainly will not use subscript notation for that, even with a C style null terminated string. To copy to another std::string, just use assignment (unless you have more specific requirements).

    Quote Originally Posted by Masterx
    i can convert them to integer if needed (using atoi() , which i cant use string str sytle )
    You can use a string stream, or you can use atoi() (or better yet, strtol()) via the c_str() member function.

    Quote Originally Posted by Masterx
    and i can point to any element of it , whenever i want to ( using *(ptr+counter) ) while i cant use string str format !
    *(ptr+counter) is the long form of ptr[counter], which is the same syntax provided by std::string. You would be getting the char at the given index, not pointing to an element.

    Quote Originally Posted by Masterx
    see to me C Style Pointers are awesome
    I have my doubts, considering that you apparently were not aware that ptr[counter] is syntactic sugar for *(ptr+counter).
    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

  2. #32
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by laserlight View Post
    Why do you need to point to the elements of the string? You certainly will not use subscript notation for that, even with a C style null terminated string. To copy to another std::string, just use assignment (unless you have more specific requirements).


    You can use a string stream, or you can use atoi() (or better yet, strtol()) via the c_str() member function.


    *(ptr+counter) is the long form of ptr[counter], which is the same syntax provided by std::string. You would be getting the char at the given index, not pointing to an element.


    I have my doubts, considering that you apparently were not aware that ptr[counter] is syntactic sugar for *(ptr+counter).
    i use subscript notation because i do need to transfer the converted offsets to the memory which is a 2D array (Memory[row][i]) . see the memory should contain instruction in this way for example :
    instruction MIX offset
    row 1101 010 000 000 000
    instruction and MIX is already feeded to the memory using subscript notations , and i dont use subscript notation, what should i use instead ?

    and i use *(ptr+counter ) because i encountered problem using ptr[counter], i really dont know why , but that form didnt help me in some situations! but this *(ptr+counter) stuff works fine eachtime i use it. !

    and about string streams . unluckily i dont know about them the same thing goes to strtol() and c_str() ) .
    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. #33
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    by the way does it work if i try to copy a string to another string with different forms! i mean suppose i have the following :
    string str = " HEllO WORLD";
    string str1[15];
    does this workout? i mean "str1=str;"
    (no i just gave it a try , doest work! it says" incompatible types in assignment of `std::string' to `std::string[15]'"
    so in this case what should i do ?
    Last edited by Masterx; 01-18-2009 at 10:12 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


  4. #34
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Masterx
    i dont use subscript notation, what should i use instead ?
    My point is that you need to be clear on what you are talking about. You talked about using subscript notation to point to an element, but that is wrong. Normally, you use subscript notation to access an element at some index. You might go on to take the address of that element and make a pointer point to it, but that is not directly related to the use of subscript notation.

    Quote Originally Posted by Masterx
    and i use *(ptr+counter ) because i encountered problem using ptr[counter], i really dont know why , but that form didnt help me in some situations! but this *(ptr+counter) stuff works fine eachtime i use it. !
    You probably accidentally made a genuine fix while changing to the *(ptr+counter) notation. When ptr is a pointer and counter is an index (or vice versa, but that would not make as much sense), *(ptr+counter) is always equivalent to ptr[counter] (and also equivalent to counter[ptr], but that would be misleading).

    Quote Originally Posted by Masterx
    and about string streams . unluckily i dont know about them the same thing goes to strtol() and c_str() ) .
    Search the Friendly Web. For example, cppreference.com has some material on them.

    EDIT:
    Quote Originally Posted by Masterx
    by the way does it work if i try to copy a string to another string with different forms! i mean suppose i have the following :
    string str = " HEllO WORLD";
    string str1[15];
    does this workout? i mean "str1=str;"
    str is a std::string, str1 is an array of 15 std::string objects.

    Quote Originally Posted by Masterx
    so in this case what should i do ?
    You need to decide on what you want to do.
    Last edited by laserlight; 01-18-2009 at 10:18 AM.
    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. #35
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by laserlight View Post
    My point is that you need to be clear on what you are talking about. You talked about using subscript notation to point to an element, but that is wrong. Normally, you use subscript notation to access an element at some index. You might go on to take the address of that element and make a pointer point to it, but that is not directly related to the use of subscript notation.


    You probably accidentally made a genuine fix while changing to the *(ptr+counter) notation. When ptr is a pointer and counter is an index (or vice versa, but that would not make as much sense), *(ptr+counter) is always equivalent to ptr[counter] (and also equivalent to counter[ptr], but that would be misleading).


    Search the Friendly Web. For example, cppreference.com has some material on them.

    EDIT:

    str is a std::string, str1 is an array of 15 std::string objects.


    You need to decide on what you want to do.
    you use subscript notation to access an element at some index
    yes thats it , im trying to access an element ! sorry i couldnt convey it sooner!

    well you are the pro here , if you say that , ok . i will use that .

    and searchin , tanx , ill do it .

    but
    str is a std::string, str1 is an array of 15 std::string objects.
    if i need to copy this to that, how is it possible then ? a couple of copying functions i read about , couldnt help this one !!
    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


  6. #36
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Masterx
    if i need to copy this to that, how is it possible then ?
    As I said, you need to decide on what you want to do. For example, do you want to set each element of the string array to be a copy of the initial string?
    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

  7. #37
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by laserlight View Post
    As I said, you need to decide on what you want to do. For example, do you want to set each element of the string array to be a copy of the initial string?
    yes . i mean that.
    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


  8. #38
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Masterx
    yes . i mean that.
    In that case, a simple method would be to use a loop:
    Code:
    std::string str = "hello world";
    std::string str_array[15];
    const std::size_t str_array_size = sizeof(str_array) / sizeof(str_array[0]);
    
    for (std::size_t i = 0; i < str_array_size; ++i)
    {
        str_array[i] = str;
    }
    A more succinct method would be to #include <algorithm> and use std::fill(), e.g.,
    Code:
    std::fill(str_array, str_array + str_array_size, str);
    or std::fill_n(), e.g.,
    Code:
    std::fill_n(str_array, str_array_size, str);
    In the context of your earlier question about converting a numeric string in octal representation to a numeric string in binary representation a loop would probably be good enough.
    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

  9. #39
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Masterx View Post
    and about string streams . unluckily i dont know about them the same thing goes to strtol() and c_str() ) .
    I believe I already showed you an example on how to use string streams, no?

    As for .c_str():
    Code:
    std::string str = "10";
    long x = strtol(str.c_str());
    Or if you want:
    Code:
    std::string str = "10";
    long x = boost::lexical_cast<long>(str);
    The second example works to convert a given type to any type, so long as that conversion is supported and possible. You don't need to remember 5 or more functions to do a conversion. Just one.
    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.

  10. #40
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by laserlight View Post
    In that case, a simple method would be to use a loop:
    Code:
    std::string str = "hello world";
    std::string str_array[15];
    const std::size_t str_array_size = sizeof(str_array) / sizeof(str_array[0]);
    
    for (std::size_t i = 0; i < str_array_size; ++i)
    {
        str_array[i] = str;
    }
    A more succinct method would be to #include <algorithm> and use std::fill(), e.g.,
    Code:
    std::fill(str_array, str_array + str_array_size, str);
    or std::fill_n(), e.g.,
    Code:
    std::fill_n(str_array, str_array_size, str);
    In the context of your earlier question about converting a numeric string in octal representation to a numeric string in binary representation a loop would probably be good enough.
    many tanx dear laserlight . really tanx .
    Quote Originally Posted by Elysia View Post
    I believe I already showed you an example on how to use string streams, no?

    As for .c_str():
    Code:
    std::string str = "10";
    long x = strtol(str.c_str());
    Or if you want:
    Code:
    std::string str = "10";
    long x = boost::lexical_cast<long>(str);
    The second example works to convert a given type to any type, so long as that conversion is supported and possible. You don't need to remember 5 or more functions to do a conversion. Just one.
    tanx dear alysia , no you didnt give me any examples ( i think) .
    but thanks again . now its fine ...

    ------------------------------
    now the function works just fine . thank you all ( i used all of copy functions you ve told me , to remember them :d
    octal to binary convertor
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <cstdio>
    #include <string>
    using namespace std;
      // Octal to binary convertor
    string Oct2Bin(int);
    
    
    int main(int argc, char *argv[])
    {
            int b,i=0,j=0;
            char memory[5][10];
            char * str2,*ptr;
            string str;
    
    
    
            cout<<"enter a number ";
            cin>>b;
    
            str=Oct2Bin(b);
    
            ptr = new char [str.size()+1];
            strcpy (ptr, str.c_str());
    
            cout<<ptr<<endl;
            if (str=="no")
            {
                           cout<<"not an octal number\n";
            }
            cout<<str<<endl;
    
             system("PAUSE");
            return EXIT_SUCCESS;
    
    }
    
    string Oct2Bin(int n)
    {
    
    
             int a[6],i=0,t=0,num;
             string str,str1,str2,s;
    
             char buffer[9];
             char * ptr, * not_octal;
             string str_array[9];
             const size_t str_array_size = sizeof(str_array) / sizeof(str_array[0]);
    
             while(n!=0)
             {
                     a[i]=n%10;
                     n=n/10;
                     if(a[i]>7)
                     t=1;
                     i++;
             }
             i--;
             if(t==0)
              for(;i>=0;i--)
              {
                      switch(a[i])
                       {
                               case 0:
                               if (!str.empty())
                               str.append("000");
                               else
                               str="000";
                               break;
    
                               case 1:
                               if (!str.empty())
                               str.append("001");
                               else
                               str="001";
                               break;
    
                               case 2:
                               if (!str.empty())
                               str.append("010");
                               else
                               str="010";
                               break;
    
                               case 3:
                               if (!str.empty())
                               str.append("011");
                               else
                               str="011";
                               break;
    
                               case 4:
                               if (!str.empty())
                               str.append("100");
                               else
                               str="100";
                               break;
    
                               case 5:
                               if (!str.empty())
                               str.append("101");
                               else
                               str="101";
                               break;
    
                               case 6:
                               if (!str.empty())
                               str.append("110");
                               else
                               str="110";
                               break;
    
                               case 7:
                               if (!str.empty())
                               str.append("111");
                               else
                               str="111";
                               break;
                       }
              }
              str1="000000";
              str2="000";
                  if (str.length()==3)
                  {
                            str1.append(str);
                            str=str1;
                  }
    
                  if (str.length()==6)
                  {
                            str2.append(str);
                            str=str2;
                  }
    
    
            for (size_t i = 0; i < str_array_size; ++i)
            {
                       str_array[i] = str;
            }
    
              ptr = new char [str.size()+1];
              strcpy (ptr, str.c_str());
    
              //cout<<ptr;
             if(t==1)
              {
                      
                      s="no";
                    // cout<<"Not a Octal number\n";
                     return s;
              }
    return str;
    }
    Last edited by Masterx; 01-18-2009 at 01:19 PM.
    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


  11. #41
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Actually, I did. Refer to post #13.
    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. #42
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by Elysia View Post
    Actually, I did. Refer to post #13.
    yeah , my bad , i forgot about that when i was googling ? my apologies.

    thanks again from you all Great mates.
    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