Thread: Binary Conversion not working...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Binary Conversion not working...

    It immediately exits for some reason?

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    struct bit
    {
           int value;
           int power;
           bit *nextbit;
           bit *prevbit;
    };
    
    int convert(string bin);
    int stringToInt(char b);
    int power(int base, int power);
    
    int main()
    {
        string bin;
        cout<<"Binary Number to Convert: ";
        getline(cin, bin);
        int baseten = convert(bin);
        cout<<baseten;
        cin.get();
    }
    
    int convert(string bin)
    {
        int size = bin.size();
        bit *current;
        vector<bit*> v;
        for(int i=0; i<size; i++)
        {
                current = new bit;
                v.push_back(current);
                current->power = i;
                current->value = 0;
        }
        //string bits together
        for(int i=0; i<size; i++)
        {
                current->prevbit=v.at(v.size()-i);
                current = v.at(v.size()-i);
        }
        //string bits together forward
        for(int i=0; i<size; i++)
        {
                current->nextbit = v.at(i);
                current = v.at(i);
        }
        //calculate value
        for(int i=0; i<size; i++)
        {
                v.at(i)->value = stringToInt(bin.at((size-1)-i));
        }
        //final calculation
        int converted=0;
        for(int i=0; i<size; i++)
        {
                converted += (power(v.at(i)->value, v.at(i)->power));
        }
        return converted;
    }
    
    int power(int base, int power)
    {
        if(power == 0)
        {
                 return base*1;
        }else{
              for(int i=0; i<power; i++)
              {
                      base *= base;
              }
        }
        return base;
    }
         
    int stringToInt(char b)
    {
        if(b == '1')
             return 1;
        else if(b=='0')
             return 0;
    }

    Thanks!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What do you mean "exits immediately". It should wait for you to type something in, then write the result to the screen - if it doesn't, then there's something wrong with your application.

    You may need a cin.ingnore() somewhere before your cin.get() to get rid of the newline that getline() didn't eat, and thus is sitting there ready for cin.get() - if that's what you mean by "exits immediately".

    By the way, you could probably do a binary to integer conversions in 15 lines (including main), whilst you have used some 50 or so. No need for a vector either, in my mind.

    Something like:
    Code:
    int o(std::string O)
    {
      int l = 0;
      for(size_t i = 0; i < O.length(); i++) {
        if (O[i] == '1')
          l |= (1 << i);
      }
      return l;
    }
    [This code is intentionally obfuscated so that you don't plug it straight in - if you put this code up for a tutor, you'll get minus-points for style - at least if I was the tutor. ]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by matsp View Post
    [This code is intentionally obfuscated ... ]
    my head hurts...
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Do you have a debugger so that you can step through the code? It should be pausing because of the cin.get(), so if it exits that's probably because it is crashing or having some other problem. For example, this line of code:
    Code:
    current->prevbit=v.at(v.size()-i);
    will throw an exception when i is 0, since v.size() is not a valid index. Since you are using at(), take advantage and add a try/catch block in main to catch an std::out_of_range exception and output an error message.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary conversion..
    By shwetha_siddu in forum C Programming
    Replies: 3
    Last Post: 07-31-2008, 03:24 PM
  2. Replies: 3
    Last Post: 07-04-2008, 12:39 PM
  3. Binary Conversion anyone?
    By NoobieGecko in forum C Programming
    Replies: 4
    Last Post: 03-07-2008, 01:34 PM
  4. Binary to BCD conversion
    By Andy_P in forum C Programming
    Replies: 4
    Last Post: 11-18-2005, 11:16 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM