got a problem in ltoa ()

This is a discussion on got a problem in ltoa () within the C++ Programming forums, part of the General Programming Boards category; hello, i have a problem in converting an octal number to its binary form, using ltoa... well i have a ...

  1. #1
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493

    got a problem in ltoa ()

    hello, i have a problem in converting an octal number to its binary form, using ltoa...
    well i have a small program in C , that does this job , just fine ,
    but i have to make it in C++, meaning i will not need any scanf() , so here is the problem , i dont know how i can feed the ltoa ! if i convert a decimal number to octal (suppose i is the decimal number , i convert it to octal and save it again in 'i' , when i use it in ltoa, it wont work!!)
    (see the following example, it just works fine, but if i omit scanf("%o",&i) , and i feed the ltoa (the number 77 which is octal )it wont work! if i use a decimal number and convert it to octal and then feed the ltoa , again it wont work!! where is the problem ? is there any counterpart for C++? or a way to manipulate octals ? i mean we have "%0" in C , do we have such thing is C++ that returns an octal number?
    Code:
    /* ltoa example */
    #include <cstdio>
    #include <cstdlib>
    
    int main ()
    {
      int i,k;
      char buffer [sizeof(long)*8+1];
      char * str;
      printf ("Enter a number: ");
      scanf ("%o",&i);
      ltoa (i,buffer,10);
      printf ("decimal: %s\n",buffer);
      ltoa (i,buffer,16);
      printf ("hexadecimal: %s\n",buffer);
      ltoa (i,buffer,2);
      printf ("binary: %s\n",buffer);
      system("pause");
      return 0;
    }
    Last edited by Masterx; 01-17-2009 at 12: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


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    The global main function returns an int, and ltoa is non-standard. Unfortunately, there is no direct standard library replacement for ltoa, except for the special cases of octal, decimal and hexadecimal. As a basic example:
    Code:
    #include <iostream>
    
    int main()
    {
        int num;
        std::cout << "Enter a number in octal: ";
        std::cin >> std::oct >> num;
    
        std::cout << "decimal: " << std::dec << num << std::endl;
        std::cout << "hexadecimal: " << std::hex << num << std::endl;
    }
    You could use a stringstream to actually save the converted result to a string instead of printing it immediately.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by laserlight View Post
    The global main function returns an int, and ltoa is non-standard. Unfortunately, there is no direct standard library replacement for ltoa, except for the special cases of octal, decimal and hexadecimal. As a basic example:
    Code:
    #include <iostream>
    
    int main()
    {
        int num;
        std::cout << "Enter a number in octal: ";
        std::cin >> std::oct >> num;
    
        std::cout << "decimal: " << std::dec << num << std::endl;
        std::cout << "hexadecimal: " << std::hex << num << std::endl;
    }
    You could use a stringstream to actually save the converted result to a string instead of printing it immediately.
    would you explain more ? i have no clue what a string stream is !( havent read about that yet!
    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by Masterx
    would you explain more ? i have no clue what a string stream is !( havent read about that yet!
    Read about that part then
    You could search the Web if necessary.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    tanx ok.im on it
    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. #6
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    i couldnt manage to figure stringstream stuff out ! so i made my own Octal to Binary convertor
    please check this out and tell me if anything is wrong. many tanx in advance :
    Code:
    int Oct2Bin(int n)
    {
             int a[6],i=0,t=0;
             string str,b;
             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;
                       }
              }
             cout<<str;
    
             if(t==1)
              {
                    
                     return -1;
    return 0;          }
    }
    Last edited by Masterx; 01-17-2009 at 05:23 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


  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    What, exactly, about stringstreams did you fail to understand?
    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.

  8. #8
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by Elysia View Post
    What, exactly, about stringstreams did you fail to understand?
    tanx for your attention dear Elysia, well cause i was kinda in hurry i just googled the string stream and a bunch of links appeared! glancing at the examples , i couldnt find it out! anyway ,(i will go for it next time , ) would you now tell me why this program fails! (im sure thats because of loop where Memory[5][i] gets stuffed! )
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <cstdio>
    #include <string>
    using namespace std;
    
    string ob(int);
    
    
    int main(int argc, char *argv[])
    {
        int b,i=0;
        char memory[5][10];
        string str;
        cout<<"enter a number ";
        cin>>b;
        str = ob(b);
        if (str =="no")
        {
                cout<<"your number is not octal!!";
        }
        else 
        {
            for (int i=0;i<9;i++)
             {
                   memory[5][i]=str[i];
             
             }
             
         }
         //cout<<memory[5][i];
       // cout<<ob();
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    string ob(int n)
    {
             int a[6],i=0,t=0,it;
             string str,b,str1,str2,not_octal;
             
             
             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;     
                  }            
                  
                  cout<<str;
             
             if(t==1)
              {
                     not_octal="no";
                     //printf("Not an Octal number\n");
                     return not_octal;
    return str;          }
    }
    Last edited by Masterx; 01-17-2009 at 07:50 AM.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Code:
        int num;
        std::cout << "Enter a number in octal: ";
        std::cin >> std::oct >> num;
    
        std::stringstream stream;
        stream << num;
        std::cout << stream.str().c_str();
    Example: http://www.cppreference.com/wiki/io/...m/constructors

    As for your code, element 5 does not exist. 0-4 does.
    Furthermore, you MUST improve your indentation.
    And avoid working with legacy char.
    If you work with arrays, use std::tr1::array or boost::array.
    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. #10
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by Masterx View Post
    Code:
    {
             int a[6],i=0,t=0;
             string str,b;
             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;
                       }
              }
             cout<<str;
    
             if(t==1)
              {
                    
                     return -1;
    return 0;          }
    }
    Quote Originally Posted by Elysia View Post
    Code:
        int num;
        std::cout << "Enter a number in octal: ";
        std::cin >> std::oct >> num;
    
        std::stringstream stream;
        stream << num;
        std::cout << stream.str().c_str();
    Example: http://www.cppreference.com/wiki/io/...m/constructors

    As for your code, element 5 does not exist. 0-4 does.
    Furthermore, you MUST improve your indentation.
    And avoid working with legacy char.
    If you work with arrays, use std::tr1::array or boost::array.
    mant tanx dear Elysia . my bad ( i completely forgot that!!tanx again )
    about indentation , is the above post indented properly?
    and about boost arrays ! is it not necessary to install boost library before using them? or are they just ordinary header files?! (include them and use em!)
    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. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Refer to http://cpwiki.sourceforge.net/Indentation for indentation advice and how it's done properly.
    Boost::array exists in a header, so you only need the header and no compiling & installing. Unless your compiler supports TR1, in which case you should be able to find it in std::tr1::array in a header called "array" I believe (or some such).
    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. #12
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    by the way , that didnt help either! you know the program compiles fine , it ouputs the binary form , there a " Windows dont send error window " appears !! and it crashes!!
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <cstdio>
    #include <string>
    using namespace std;
    
    string Oct2Bin(int);
    
    
    int main(int argc, char *argv[])
    {
            int b,i=0;
            char memory[5][10];
            string str;
        
            cout<<"enter a number ";
            cin>>b;
        
            str = Oct2Bin(b);
            if (str =="no")
            {
                    cout<<"your number is not octal!!";
            }
            else 
            {
                for (int i=0;i<9;i++)
                 {
                       memory[4][i]=str[i];
             
                 }
             
             }
         //cout<<memory[5][i];
       // cout<<ob();
            system("PAUSE");
            return EXIT_SUCCESS;
    //in the name of God
    //Octal To Binary Convertor
    //Protype : string Oct2Bin( int );
    }
    
    string Oct2Bin(int n)
    {
             int a[6],i=0,t=0;
             string str,str1,str2,not_octal;
             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;
                  }
    
             cout<<str;
    
             if(t==1)
              {
                      not_octal="no";
                     //cout<<"Not a Octal number\n";
                     return not_octal;
    return 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


  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    int main()
    {
    	int Num;
    	std::cout << "Enter a number: ";
    	std::cin >> std::oct >> Num;
    	
    	std::stringstream sstr;
    	sstr << Num;
    	std::cout << sstr.str().c_str() << std::endl;
    }
    Although, don't ask me why the output is incorrect; I'm not much into this kind of thing.
    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.

  14. #14
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    well, so how should i know what is wrong with it! im really confused!
    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


  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 3 123 LastLast
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, 10: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, 04:46 PM

Tags for this Thread


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