Hi i made a program for conversion decimal to binary number ... but i need to convert the big dottef number into dotted binary like this

For example 10.10.0.13 is <00001010.00001010.00000000.00001101>

8-bit of every integer ..

This is making me confuse how to make it ? my conversion code is below can anyone help me for modified to above program of this code .

#include <iostream>
using namespace std;

int main() {
    int n;  // number to convert to binary

    while (cin >> n) {
        if (n > 0) {
            cout << n << " (decimal) = ";
    
            while (n > 0) {
                cout << n%2;
                n = n/2;
            }
    
            cout << " (binary) in reverse order" << endl;
        } else {
            cout << "Please enter a number greater than zero." << endl;
        }
    }
  return 0;
}

Recommended Answers

All 6 Replies

Make your decimal to binary into a function that returns a string (with the bits going the proper direction, this is not difficult.

Chop up the address string into it's elements, place them into an array or 4 individual variables.

Use a cout statement with 4 calls to your function: cout<<"<"<<dec2bin(firstpart)<<"."<<dec2bin(secondpart)<<"."<<etc.

your code is based on the premise that cin extraction into an int datatype is '.' delimited, but i believe that this is not the case.

the bulk of your problem, in my opinion, occurs at line #7 when the program attempts to assign a huge multi-decimal number into an int data type.

my suggestion is to extract and evaluate each 8-bit segment individually.

[Beat]

Make your decimal to binary into a function that returns a string (with the bits going the proper direction, this is not difficult.

Chop up the address string into it's elements, place them into an array or 4 individual variables.

Use a cout statement with 4 calls to your function: cout<<"<"<<dec2bin(firstpart)<<"."<<dec2bin(secondpart)<<"."<<etc.

i understand but i dont know much about the calling the string...

can you help me out about this program thanks

your code is based on the premise that cin extraction into an int datatype is '.' delimited, but i believe that this is not the case.

Correct. It is not. Integer extraction STOPS at the decimal, but it does not THROW OUT the decimal. It remains in the stream.

Thus if you enter "5.6" and read an int followed by a float, the int is 5, the float is 0.6. Try to read two ints in a row and you fail because all the second int has is a decimal to work with and it can't use it.

The OP can either read in the numbers as a string, as mentioned, then parse the string to get the ints, or he can set up a char variable to read and throw away that pesky dot (actually not so pesky. It "delimits" quite well, but as mentioned, it remains in the stream).

Or how about getline with a '.' as the delimiter?

To the OP: You need to take a tutorial on strings.

http://www.cprogramming.com/tutorial/string.html

Correct. It is not. Integer extraction STOPS at the decimal, but it does not THROW OUT the decimal. It remains in the stream.

Thus if you enter "5.6" and read an int followed by a float, the int is 5, the float is 0.6. Try to read two ints in a row and you fail because all the second int has is a decimal to work with and it can't use it.

The OP can either read in the numbers as a string, as mentioned, then parse the string to get the ints, or he can set up a char variable to read and throw away that pesky dot (actually not so pesky. It "delimits" quite well, but as mentioned, it remains in the stream).

Or how about getline with a '.' as the delimiter?

To the OP: You need to take a tutorial on strings.

http://www.cprogramming.com/tutorial/string.html

yes agree with this ... so what about this program ? what should i do further now? any path that you can show ?

yes agree with this ... so what about this program ? what should i do further now? any path that you can show ?

Same advice as the other two. Read it in as a string, then parse the string to extract the integers. The string library has everything you need to do this:

http://www.cplusplus.com/reference/string/string/

There are other ways too (i.e stringstreams, using getline, reading in the dot as a char as mentioned, etc.), of course, but reading it in as a string and parsing the string and converting to an integer will work just fine. As to HOW to parse a string, you need to understand a bit about strings first, so you need to take some tutorials first. Otherwise the technical explanations about "how" won't make sense. But the various "find" and "substr" and "c_str" functions from string and atoi and/or strtol from cstdlib have everything you need to do the job.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.