Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

more C++....


ghost's Avatar
0 0

Ok even more C++ from me! how would i make a program that could convert Binary to Hex, or decimal to binary. that sort of thing. would you have something like:

#include <iostream>

using namespace std;

int main()
{
              int bin;
              int hex;

              cout<<"enter numbers blah blah blah\n";
              cin>> bin;
              cin.ignore();



0000 == 0
0001 == 1
0010 == 2
0011 == 3
0100 == 4
0101 == 5
0110 == 6
so on...

cout<<"your conversion blah blah blah\n" << hex <<"blah blah blah";
return main(); 
}
cin.get();
}

but then it would be limited to just 6 numbers ( although it wouldnt take long to do the other 9 )

——————note––––––––––––––––

i wrote that code, and binary to hex of the top of my head dont crash your computers trying to fix every syntax error.


spyware's Avatar
Banned
0 0

This is the worst C++ code I ever saw. You apperantly don't understand how to code efficient. You need a formula, not a huge list to convert stuff.


ghost's Avatar
0 0

as i said it was of the top of my head, and i dont know of any formula or where to put it. the only vague idea of got of something like this is a calculator, there easy.

i can be code efficient, when i know how to do stuff.


ghost's Avatar
0 0

As well as writing formulas, if you plan to do multiple types of conversions, such as text to binary and text to hex, you need to learn how to create functions, define them and use them, this will create better code


ghost's Avatar
0 0

This is basic stuff, first you have to be familiar with the language you're using, then find out how to do it algorithmically by hand. Then try to implement the algorithm into your preferred language. Some times the way you do it by hand may not be the best way to accomplish the task. The sign of a decent programmer is to be able to solve problems within the constraints that are given to them.

Here's an example of a function in C++ that take an integer as an argument and outputs the binary equivalent.

void OutputBinary(int dec) {
    int x;
    for(x = 0; x < 32; x++) {
        cout << ((dec>>(31-x)) & 1);
    }
    cout << "\n";
}

It should work. Depending on the integer you should get unnecessary leading 0's… w/e I was just giving an example.

EDIT: mistyped a word :x