Feb 8, 2010 at 8:48pm UTC
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