3 Replies - 732 Views - Last Post: 29 October 2014 - 07:54 PM Rate Topic: -----

#1 numbers_game  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-September 14

Wrote a decimal to Binary program in C++ , need feedback

Posted 29 October 2014 - 06:30 PM

Wrote a decimal to binary program in C++, im fairly new to C++ and I need feedback on whether the code is well-written or not. Some people told me it was alright, some told me it's so-so , one guy suggested I should do something else other than coding, i just need more opinions. Thanks all.

#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <fstream>
#include <locale>
#include <sstream>


using std::cout;
using std::cin;
using std::vector;
using std::string;
using std::endl;
using std::cerr;
using std::stringstream;


int convert_to_bin(int user_conv_num)
{
    stringstream bin_in_string;//stringstream to store int result in

    if(user_conv_num == 0)
    {

        return 0;

    }


     convert_to_bin(user_conv_num / 2);//We call convert_to_bin function while dividing user_conv_num in the parameter because the modulus operator does not divide for us

     bin_in_string<<user_conv_num % 2;//Get the reminder using the modulus operator, this does not divide user_conv_num by two, it only gets the remainder and writes it to bin_in_string


     cout<<bin_in_string.str();//print result

     return 0;
}

int main()
{
    long int user_conv_num = 0;


cout<<"(#)";

cin>>user_conv_num;

    try
    {


      if(user_conv_num < 0) //if negative number
        throw 1; //throw int exception


      else
        {

        convert_to_bin(user_conv_num); //if not a negative number , do the conversion

        }

    }

    catch (int) //catch int exception if true
    {

        cerr<<"Negative integer";

    }

}




Is This A Good Question/Topic? 0
  • +

Replies To: Wrote a decimal to Binary program in C++ , need feedback

#2 infernorthor  Icon User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Wrote a decimal to Binary program in C++ , need feedback

Posted 29 October 2014 - 07:01 PM

Well for one I would have it return a string instead of just print it.
An iterative method may be more efficient. And you could use bitshift.
Was This Post Helpful? 0
  • +
  • -

#3 numbers_game  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-September 14

Re: Wrote a decimal to Binary program in C++ , need feedback

Posted 29 October 2014 - 07:23 PM

Bit Shift was recommended but i have no idea even where to start with it, on top of that i never used it before.
Was This Post Helpful? 0
  • +
  • -

#4 infernorthor  Icon User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Wrote a decimal to Binary program in C++ , need feedback

Posted 29 October 2014 - 07:54 PM

bitshift is one of the bitwise operation, look that up.
It allows you to directly interact with the bits.

bitshift >> << shift the bits by a number of digit.

int x = 1;
  x = x << 1; // or x <<= 1;
  // x == 2 now since 10 in binary is 2
  


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1