Thread: simple but not working

  1. #16
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40

    Cool the math way

    Alright, thanks for the code examples above.
    I will be studying them and will use them once I know and test them better.

    Below is the hardway in that it demonstrates the math and could be extended to other bases.

    As always your comments are welcome.

    Dave++

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <string>
    #include <cmath>
    #include <cstdlib>
    
    std::string DecToBin(float num)
    {
      std::string str = "";
      int i, expmax;
      double top;
      const double lg2 = log(2.0);
    
      if(num == 0) return("0");
    
      expmax = floor(log(num)/lg2);
      //  std::cout <<"expmax " << expmax << std::endl;
    
      for(i=expmax; i>=0; i--){
        top = num - pow(2.0,i);    // subtract the binary basis
        //    std::cout << num << " " << top << std::endl;
        if (top < 0){
          str = str + "0";
        }
        else {
          str = str + "1";
        num = top;
        }
      }
      return(str);
    }      
    
    int main()
    {
      int num;
      printf("Enter a whole number: ");
      scanf("&#37;i", &num);
      std::cout << DecToBin(num) << std::endl; 
      //  std::cout << log(2.0) << std::endl;
    }
    ____________
    If there are no spoons, then where did all the ice cream come from? ("7")
    Last edited by Dave++; 06-21-2007 at 07:04 PM.

  2. #17
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    just a thought Dave++, here is another way to work it. Not as good as Daved but it does work.
    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <sstream>
    
    using namespace std;
    
    int main()
    {
        unsigned long long Dec;
        std::stringstream ss;
        cout << "Number:";
        while ( cin >> Dec )
        {
            while ( Dec > 0 )
            {
                ss << (Dec&#37;2);
                Dec /= 2;
            }
            std::string binary = ss.str();
            reverse(binary.begin(), binary.end());
            cout << "Binary: " << binary << endl;
            cout << "New Number: ";
        }
    }

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    To Dave++:

    <stdio.h> is a C header file. Usually one uses <cstdio>, which puts the functions in <stdio.h> into the namespace std. I see you've included <cmath> instead of <math.h>, but you're using just plain pow() and log() etc. Use either <math.h> and pow() or <cmath> and std::pow.

    Also,
    Code:
    str = str + "0";
    is the same as
    Code:
    str += "0";
    which means, of course, that you can use either. But the latter requires less typing and is less error-prone.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #19
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40
    DWKS, Raigne,
    Thanks for the updates. I'll be using them.

    Dave

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    a = a + b and a += b may be semantically equivalent, but they might very well differ in efficiency. Typically, += is considerably more efficient.
    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. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For appending a single character, an alternative would be:
    Code:
    str.push_back('0');
    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. #22
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MacGyver View Post
    If you're returning a local string object, that could be why you're seg faulting. I don't think you should return local objects. Someone correct me if I'm wrong....
    You shouldn't return REFERENCES to local objects. Returning objects themselves is fine.

  8. #23
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You shouldn't return pointers to local objects, either. Returning objects by value is okay (assuming that the object has a proper copy constructor!) because the calling function gets a copy of the object.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [PAID] Very simple C script - Not working?
    By spadez in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-12-2009, 08:00 AM
  2. Simple program not working, don't know why
    By Bakster in forum C Programming
    Replies: 11
    Last Post: 01-29-2009, 01:56 PM
  3. Replies: 3
    Last Post: 09-12-2005, 09:08 AM
  4. Replies: 5
    Last Post: 02-02-2003, 10:56 AM
  5. simple program not working
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 03-04-2002, 11:36 PM