Question:
C++ Decimal/Octal/Hex conversion program?
xtreme1131
2009-09-20 20:06:29 UTC
i had to make a program that converts decimal numbers to octal and hex. I finished up the octal and 90% of the hex, but for some reason my if-then statements are not working. Any help would be appreciated. Also, any advice on cleaning up my code would also be great. Im new to C++ so bear with me.

heres my code:

#include
using namespace std;

int main()
{
int d[10];
long x[9];
unsigned long n;
int a[8];
long b[7];
char A;
char B;
char C;
char D;
char E;
char F;

//Decimal to Octal
cout<<"Enter a decimal number"< cin>>n;

d[0]=n%8;
x[0]=n/8;
d[1]=x[0]%8;
x[1]=x[0]/8;
d[2]=x[1]%8;
x[2]=x[1]/8;
d[3]=x[2]%8;
x[3]=x[2]/8;
d[4]=x[3]%8;
x[4]=x[3]/8;
d[5]=x[4]%8;
x[5]=x[4]/8;
d[6]=x[5]%8;
x[6]=x[5]/8;
d[7]=x[6]%8;
x[7]=x[6]/8;
d[8]=x[7]%8;
x[8]=x[7]/8;
d[9]=x[8]%8;
x[9]=x[8]/8;
d[10]=x[9]%8;

//Decimal to Hex
a[0]=n%16;
b[0]=n/16;
a[1]=b[0]%16;
b[1]=b[0]/16;
a[2]=b[1]%16;
b[2]=b[1]/16;
a[3]=b[2]%16;
b[3]=b[2]/16;
a[4]=b[3]%16;
b[4]=b[3]/16;
a[5]=b[4]%16;
b[5]=b[4]/16;
a[6]=b[5]%16;
b[6]=b[5]/16;
a[7]=b[6]%16;
b[7]=b[6]/16;
a[8]=b[7]%16;

for (int i=0;i<9;i++)
if (a[i]<=9) {
a[i]= a[i];}
else if (a[i]==10) {
a[i]=A;}
else if (a[i]==11){
a[i]=B;}
else if (a[i]==12) {
a[i]=C;}
else if (a[i]==13){
a[i]=D;}
else if (a[i]==14){
a[i]=E;}
else if (a[i]==15){
a[i]=F;}


cout<<"Octal equivalent: "<
cout<<"Hexidecimal equivalent: "<system("pause");

return 0;
}
Five answers:
Mark aka jack573
2009-09-24 19:54:38 UTC
Firstly you are not making the chars A - F with any value.

Next, the way you are making the oct and hex numbers can be changed to do it much simpler.



I have changed your code slightly to make the oct and hex an 8 digit number. If you need more, just change the necessary places. Like if you need to oct to be 10, not 8, add 2 '0' to the start of the array oct, and change index to 9 before it goes into the while loop.



#include

using namespace std;



int main()

{

char oct[9] = {'0', '0', '0', '0', '0', '0', '0', '0', '\0'};

char hex[9] = {'0', '0', '0', '0', '0', '0', '0', '0', '\0'};

unsigned long n;

// or just

// int n;

char A = 'A';

char B = 'B';

char C = 'C';

char D = 'D';

char E = 'E';

char F = 'F';



cout << "Enter a decimal number" << endl;

cin >> n;



unsigned long copy = n;

// or

// int copy = n;

int temp;

int index = 7;

while (copy > 0) // The oct number.

{

temp = copy % 8;

oct[index] = temp;

copy = copy / 8;

index--;

}



copy = n;

index = 7;

while (copy > 0) // The hex number.

{

temp = copy % 16;

if (copy < 10)

{

hex[index] = temp;

}

else if (temp == 10)

{

hex[index] = A;

}

else if (temp == 11)

{

hex[index] = B;

}

else if (temp == 12)

{

hex[index] = C;

}

else if (temp == 13)

{

hex[index] = D;

}

else if (temp == 14)

{

hex[index] = E;

}

else if (temp == 15)

hex[index] = F;

}

copy = copy / 16;

index--;

}



cout << "Octal equivalent: " << oct << endl;

cout << "Hexadecimal equivalent: " << hex << endl;



system("pause");



return 0;

}



You may notice that it is a lot simpler than your code. Study the differences and see why it works.



Good luck.
WiiNie
2009-09-20 20:22:06 UTC
Your problem is you assumed that char A contains the letter 'A' within it. Instead this is a variable holding a number inside (likely '0'). You are filling any part of the answer that should be a letter with a '0' instead of the letter you want to put in. Unfortunately a letter cannot be put into variable 'a' because it will be seen as a number. What I suggest is to scrap the loop changing the values in 'a' and instead turn your hex output line into a loop that prints a letter whenever a[i] is greater than 9.



So scrap this:

for (int i=0;i<9;i++)

if (a[i]<=9) {

a[i]= a[i];}

else if (a[i]==10) {

a[i]=A;}

else if (a[i]==11){

a[i]=B;}

else if (a[i]==12) {

a[i]=C;}

else if (a[i]==13){

a[i]=D;}

else if (a[i]==14){

a[i]=E;}

else if (a[i]==15){

a[i]=F;}



And put this for hex output:

cout<<"Hexidecimal equivalent: "

for (int i=0;i<9;i++)

if (a[i]<=9) {

cout<
else if (a[i]==10) {

cout<<"A";}

else if (a[i]==11){

cout<<"B";}

else if (a[i]==12) {

cout<<"C";}

else if (a[i]==13){

cout<<"D";}

else if (a[i]==14){

cout<<"E";}

else if (a[i]==15){

cout<<"F";}



Otherwise everything seems to be in the right place. Remember that if you use a char with numbers it will be used as a number. Also assign all variables a value before you use them, otherwise you are probably using them wrong. A char or string can store text but an int, long, float (numbers) will always be treated as a number and will always print out as a number unless you tell the computer to convert to a char with a type cast. Your code is functional but there is much repetition that can be replaced with loops. I would not change it one bit once it is working though as the code works and any changes will take extra time to write and can break the program. In the future when you have repetition ask yourself if it would be faster to find a way to write the code shorter or to repeat yourself. Either way works in the end.
Orvanis
2009-09-20 20:28:03 UTC
While a rather messy solution, it is a solution non-the-less so I will work with what you have, I did however remove the brackets shown below as they are not needed when you only have one line of code under the if statement.



The first and largest problem I see with your if, else if, else statements is that you are setting your a[i] = A, B, C, D, E, and F:



else if (a[i]==10)

    a[i]=A;

else if (a[i]==11)

a[i]=B;

else if (a[i]==12)

a[i]=C;

else if (a[i]==13)

a[i]=D;

else if (a[i]==14)

a[i]=E;

else if (a[i]==15)

a[i]=F;



However up towards the top you are declaring:

char A;

char B;

char C;

char D;

char E;

char F;



What "char A;" simply means is create a variable of type char with the name of A. So "char A='S';" would set the variable A equal to the character S. This is not what I think you intend to happen when you get to a[i] = A, as this would instead set a[i] = 'S'.



Instead, what you should have for your if statements is this to actually set them as characters:

else if (a[i]==10)

a[i]='A';

else if (a[i]==11)

a[i]='B';

else if (a[i]==12)

a[i]='C';

else if (a[i]==13)

a[i]='D';

else if (a[i]==14)

a[i]='E';

else if (a[i]==15)

a[i]='F';



However this would also present a problem for you as you have "a" set as an array of integers. In order to work with the HEX string you have to change it to char a[8]; This should get you completely done, or at least very close to done.
?
2009-09-21 02:02:50 UTC
Hallo,

that is the most complicated Decimal to Octal example I have seen so far!

My 10 cents: let int decimal is the number you want to convert.



int arrOct[20];

int i = 0; //index

int temp=decimal

while (temp > 0) {

arrOct[i] = temp%8;

i++;

temp/=8;

}

// print the number

for (intj=i-1; j>=0; j--)

cout << arrOct[j];



Similar with hex, however you have to consider the A - F cases.
2016-04-03 07:02:23 UTC
The problem statement tells you to use an int for hour and minute, and that's a good idea. You declared Eastern and Central as double for some reason, but you don't need these variables at all. You should also make sure the input is valid, and make it easier for the user to enter the time. Just adding/subtracting 1 to/from the hour isn't good enough either. You need to account for the midnight boundary. The problem statement also only says to convert from central to eastern, but then talks about the C/E timezone designation, which is confusing. Your interpretation was to allow conversion either way, and that's a good choice. So, putting it all together, your program should look something like this: #include #include #include #include #include using namespace std; const char delim = ':'; const int hoursPerDay = 24; const int minutesPerHour = 60; enum { Eastern = 'E', Central = 'C', NumZones = 2 } Zone; bool isValidTime(int, int); bool isValidZone(char); int main(int argc, char *argv[]) {     string in;     stringstream ss;     int hours, minutes, timeChange[] = { 1, -1 }, z;     char c, zone, *zTxt[NumZones] = { "Central", "Eastern" };     cout << "Time format: : " << endl;     cout << " where: HH = hours, 0 .. 23" << endl;     cout << " MM = minutes, 0 .. 59" << endl;     cout << " zone = 'C' for central, or 'E' for eastern" << endl << endl;     while (true) {         cout << "Enter time (HH:MM C|E) : ";         getline(cin,in);         ss.clear(); ss.str(in);         if ((ss >> hours >> c >> minutes >> zone) &&                 (c == delim) &&                 (isValidTime(hours, minutes) == true) &&                 (isValidZone(zone) == true)) {             z = (toupper(zone) == Eastern);             cout << setw(2) << setfill('0') << hours << ":"                       << setw(2) << setfill('0') << minutes << ' ' << zTxt[z]                       << " = ";             hours += timeChange[z];             if (hours < 0) {                 hours += hoursPerDay;             } else if (hours >= hoursPerDay) {                 hours -= hoursPerDay;             }             cout << setw(2) << setfill('0') << hours << ":"                       << setw(2) << setfill('0') << minutes << ' '                       << zTxt[(z + 1) % NumZones] << endl << endl;         }     }     return 0; } bool isValidTime(int h, int m) {     return (h >= 0) && (h < hoursPerDay) && (m >= 0) && (m < minutesPerHour); } bool isValidZone(char z) {     return (toupper(z) == Eastern) || (toupper(z) == Central); } #if 0 Sample run: Time format: :     where: HH = hours, 0 .. 23                   MM = minutes, 0 .. 59                   zone = 'C' for central, or 'E' for eastern Enter time (HH:MM C|E) : 12:30 C 12:30 Central = 13:30 Eastern Enter time (HH:MM C|E) : 23 : 20c 23:20 Central = 00:20 Eastern Enter time (HH:MM C|E) : 00 : 15 E 00:15 Eastern = 23:15 Central Enter time (HH:MM C|E) : 9.20c Enter time (HH:MM C|E) : 99.99 E Enter time (HH:MM C|E) : 9:20 C 09:20 Central = 10:20 Eastern Enter time (HH:MM C|E) : 13 : 20 Enter time (HH:MM C|E) : 13 : 20 x Enter time (HH:MM C|E) : 13 : 20 E 13:20 Eastern = 12:20 Central #endif


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...