simple but not working

This is a discussion on simple but not working within the C++ Programming forums, part of the General Programming Boards category; Alright, thanks for the code examples above. I will be studying them and will use them once I know and ...

  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,046
    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,893
    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
    21,648
    For appending a single character, an alternative would be:
    Code:
    str.push_back('0');
    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

  7. #22
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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,046
    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.

Page 2 of 2 FirstFirst 12
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, 07:00 AM
  2. Simple program not working, don't know why
    By Bakster in forum C Programming
    Replies: 11
    Last Post: 01-29-2009, 12:56 PM
  3. Replies: 3
    Last Post: 09-12-2005, 09:08 AM
  4. Replies: 5
    Last Post: 02-02-2003, 09:56 AM
  5. simple program not working
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 03-04-2002, 10:36 PM

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