Is this formatted correctly?

Hey guys! I am still very new to C++ and am currently learning about converting decimal to binary, and binary math. I decided to try and make a simple program that converts it for you, using the remainder method for conversion. My program works, but my question is do I have the format right? Or is it jumbled up. I don't know if I put things like the global variables and the prototyping in the correct spots.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
using namespace std;

void binarycompile(int remainder);

string binary;
string bit;

int main(){
    int remainder;
    int current;

    cout << "Type the decimal number to convert to binary:  ";
    cin >> current;
    cout << endl << endl << "Binary = ";

    do{
    remainder = current % 2;
    current = current / 2;
    binarycompile(remainder);
    }while(current > 0);

    binarycompile(2);
    cout << endl << endl << "Press enter to quit.";
    cin.get();
    cin.get();
    return 0;
}
void binarycompile(int remainder){
    switch (remainder){
        case 0:
                bit = "0";
                binary = bit + binary;
                break;
            case 1:
                bit = "1";
                binary = bit + binary;
                break;
            default:
                cout << binary;
        }
}
Last edited on
I think its really personal preference... It doesn't really matter unless its extremely bad.
Just found out that neg's will not work so I amended the code to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;

void binarycompile(int remainder);

string binary, bit;
int negcheck = 0;

int main(){
    int remainder;
    int current;

    cout << "Type the positive number to convert to binary:  ";
    cin >> current;
    if(current <= -1){
          negcheck = 1;
          current = current * -1;
    }
    cout << endl << endl << "Binary = ";
    do{
    remainder = current % 2;
    current = current / 2;
    binarycompile(remainder);
    }while(current > 0);

    binarycompile(2);
    cout << endl << endl << "Press enter to quit.";
    cin.get();
    cin.get();
    return 0;
}
void binarycompile(int remainder){
    switch (remainder){
        case 0:
                bit = "0";
                binary = bit + binary;
                break;
            case 1:
                bit = "1";
                binary = bit + binary;
                break;
            default:
                if(negcheck == 1){
                    binary = "-" + binary;
                }
                cout << binary;
        }
}
I recommend removing binary and bit as global variables by moving them under the function binarycompile. They're only used there, after all.
Last edited on
Ah very true. I had them as global before because instead of a default case with a cout in it, I had the function type as string and return binary; but it wouldn't return with the variables inside the function. Changing that now, thanks for pointing it out!
Topic archived. No new replies allowed.