#include <iostream> using namespace std; void decimal2Base(int num, int base, char *result, int offset = 0); void decimal2Base(int num, int base, char * result, int offset) { if (base > 36 || base < 2) result = "NaN"; else { int digitVal = num % base; *result = (digitVal < 10 ? digitVal + '0' : digitVal - 10 + 'A'); if (num >= base) decimal2Base(num / base, base, result + 1, offset + 1); else { result[1] = '\0'; result -= offset; //get result's length int length = 0; for (char *ptr = result; *ptr; ptr++) length++; //copy result to a temporary string char *temp = new char[length + 1]; for (char *ptr = result, *tempPtr = temp; *ptr; ptr++, tempPtr++) *tempPtr = *ptr; //copy back to result in reversed order for (int i = length - 1; i >= 0; i--) result[length - 1 - i] = temp[i]; //free memory of the temporary string delete [] temp; } } } int main() { int num; int base; char result[20]; while (true) { cout << "Enter number: "; cin >> num; if (num < 0) cout << "Enter base: "; cin >> base; if (base > 36 || base < 2) decimal2Base(num, base, result); cout << result << endl; cout << endl; } cout << "\nEnd - Press enter to exit . . . "; cin.sync(); cin.get(); return 0; }
can anyone help in converting this code to c++ in order to understand it clearly because we use c++ only