Hello all. So this was a code I've been working on and trying to implement another feature to this switch program. I'm trying to add a hexadecimal conversion. My try at it starts at line 74-94. I was looking at other converting codes written by other people and I know I need to install:
 ``123456789101112`` ``````if (y==10) cout << 'A'; else if (y==11) cout << 'B'; else if (y==12) cout << 'C'; else if (y==13) cout << 'D'; else if (y==14) cout << 'E'; else if (y==15) cout << 'F';``````

somewhere into my code. Also, I have to deal with unexpected inputs other than what my switch is asking for. So I have to add throwing exceptions in my code to deal with them. I have no idea how these work. Any help is great, my code is posted below.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108`` ``````#include #include //Exception handling. using namespace std; int main() { int bArray[16] = { 0 }; int k = 0; int oArray[8] = { 0 }; int i = 0; int hArray[16] = { 0 }; int j = 0; int decimal = 0; double binary = 0; double octal = 0; int number; cout << "Enter 'B' to convert a decimal number to binary, 'O' for an octal conversion,and'H' for a hexadecimal conversion. " << "Hit space to quit program." << endl; while ((number = cin.get()) != ' ') { cout << "\nEnter a decimal number to be converted (No more than 65,535): " << endl; switch (number) { case 'B': case 'b': cin >> decimal; while ((decimal != 0) & (k <= 15)) { bArray[k] = decimal % 2; decimal = decimal / 2; k++; } cout << "Here is the binary conversion: "; for (k = 15; k >= 0; k--) { cout << bArray[k]; if ((k % 4) == 0) cout << " "; } cout << endl; break; case 'O': case 'o': cin >> decimal; while ((decimal != 0) & (i <= 7)) { oArray[i] = decimal % 8; decimal = decimal / 8; i++; } cout << "Here is the octal conversion: "; for (i = 7; i >= 0; i--) { cout << oArray[i]; if ((i % 4) == 0) cout << " "; } cout << endl; break; case 'H': case 'h': cin >> decimal; while ((decimal != 0)&(j <= 15)) { hArray[j] = decimal % 16; decimal = decimal / 16; j++; } cout << "Here is the hexadecimal conversion: "; for (j = 15; j >= 0; j--) { cout << hArray[j]; if ((j % 4) == 0) cout << " "; } cout << endl;; break; case '\n': case '\t': case ' ': break; default: cout << "Wrong decimal input." << "Enter a new number." << endl; break; } } cout << "Thank you." << endl;//end main }``````
This is a simple way to do it here. I can guarantee you it's not the most efficient but it's easy to code.

I'm sure you could work out a way to divide by 16 until 0 then loop through the remainder to get an exact value and save processor time.

Hopefully this gives you some idea on how to do it.

This basically subtracts one from the user input decimal and adds one to a hexidecimal array. Then when one of the hex array elements exceeds 15 it increments the next higher place value and sets the current place value to 0. The program loops through until user input has reached zero.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````using namespace std; int main() { int hex_array[8]; int user_input; for (int i = 0; i < 15; i++){ hex_array[i] = 0;//populate hex_array with 0's } cout << "Enter a decimal number to convert to hexidecimal: "; cin >> user_input; for (int i = user_input; i > 0; i--){ hex_array[7] = hex_array[7] + 1; if (hex_array[7] > 15){ for (int n = 6; n > -1; n--){ hex_array[n+1] = 0; hex_array[n] = hex_array[n] + 1; if (hex_array[n] < 16){ n = -1; } } } } cout << "\n\nConverted to Hexidecimal is: "; for (int i = 0; i < 8; i++){ if (hex_array[i] == 10){cout << 'A';} else if (hex_array[i] == 11){cout << 'B';} else if (hex_array[i] == 12){cout << 'C';} else if (hex_array[i] == 13){cout << 'D';} else if (hex_array[i] == 14){cout << 'E';} else if (hex_array[i] == 15){cout << 'F';} else {cout << hex_array[i];} } return 0; }``````
Last edited on
Topic archived. No new replies allowed.