make effcientcy code?

This is my code

#include <iostream>
#include <cctype>
#include <cstdlib>
#include <conio.h>
using namespace std;

void bin (int [], int , int, int, int);
void oct (int [], int , int, int, int);
void hex (int [], int , int, int, int);

int main () {
do {
const int SUB = 15;
char ch[15];
int binOctHex[SUB] = {0}, dividend = 0,
quotient = -1, remainder = 0,
x = 0, z = 0;
system("cls");

cout << "Please enter a number between 1 and 32767: ";
cin >> ch;

while (atoi(ch) <= 0 || atoi(ch) > 32767) {

cerr << "Invalid entry; please reenter: ";
cin >> ch;
}
dividend = atoi(ch);

bin(binOctHex, dividend, x, quotient, remainder);
oct(binOctHex, dividend, x, quotient, remainder);
hex(binOctHex, dividend, x, quotient, remainder);

cout << endl;
cout << "Go again (Y/N)? ";
} while (toupper(getch()) != 'N');

cout << "\n";
return 0;
}

void bin (int binary[], int dividend1, int z, int quotient1, int remainder1) {
const int DIVISOR = 2;

while (quotient1 != 0) {
quotient1 = dividend1 / DIVISOR;
remainder1 = dividend1 % DIVISOR;
binary[z++] = remainder1;
dividend1 = quotient1;
}

cout << "The binary value is ";
for ( int y = z-1; y >= 0; y--)
cout << binary[y];
cout << endl;
}

void oct (int oct[], int dividend1, int z, int quotient1, int remainder1) {
const int DIVISOR = 8;

while (quotient1 != 0) {
quotient1 = dividend1 / DIVISOR;
remainder1 = dividend1 % DIVISOR;
oct[z++] = remainder1;
dividend1 = quotient1;
}

cout << "The oct value is ";
for ( int y = z-1; y >= 0; y--)
cout << oct[y];
cout << endl;
}

void hex (int hex[], int dividend1, int z, int quotient1, int remainder1) {
const int DIVISOR = 16;

while (quotient1 != 0) {
quotient1 = dividend1 / DIVISOR;
remainder1 = dividend1 % DIVISOR;
hex[z++] = remainder1;
dividend1 = quotient1;
}

cout << "The hex value is ";
for ( int y = z-1; y >= 0; y--) {
if ( hex[y] >= 10)
hex[y] += 55;
if ( hex[y] < 10)
hex[y] += 48;

cout << (char)hex[y];
}
cout << endl;

}


is there anyway to make my code shorter?
Thank You
You are using mathematical approach, which makes sense when you convert from decimal to binary, but you forget that you are working with a computer. Here everything is in binary already. you only have to read it.
1
2
3
4
5
int input;
for(int i = 0; i < 32; i++){//try to figure out how to make it not print all the 0000...
    cout << inout & 0x1;
    input <<= 1;
}

I could have made it a bit differently, but this way it will be very easy to write functions for oct and hex.
Topic archived. No new replies allowed.