Let me start off by saying that this is homework, and I have most of it completed. However, I am stuck on how to convert decimal to hexadecimal. The assignment is to add two hexadecimal numbers and output the answer. If the length of the answer is greater than 10 digits, then output "Addition Overflow." I've completed the conversion of the two strings to decimal and added them together, now I think that I need to convert the answer back to a string. I know it's long, but if you would take the time to read my code, then it would be greatly appreciated. Here's my code:
// hex addition.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
void convertToDec(string, string);
void backToHex(double);
int _tmain(int argc, _TCHAR* argv[])
{
//assign values to last six hex characters
//convert both inputs to dec
//add two dec numbers
//convert back go hex for answer
string input;
string input2;
int digit = 0;
bool decision;
char choice;
cout << "Welcome to the Hexadecimal Addition Calculator!" << endl;
do
{
decision = true;
cout << "Enter two hexadecimal numbers (no more than ten digits): " ;
cin >> input;
cout << "Now enter another hexadecimal number to add to the first: ";
cin >> input2;
if (input.length() > 10)
{
cout << "Invalid input" << endl;
system("pause");
return 0;
}
convertToDec(input, input2);
cout << "Would you like to continue? (y/n)";
cin >> choice;
if (choice == 'n' || choice == 'N')
{
decision = false;
system("pause");
return 0;
}
if (choice == 'y' || choice == 'Y')
{
decision = true;
system("pause");
system("cls");
}
} while (decision = true);
system("pause");
return 0;
}
//used to convert the two inputs to decimal
void convertToDec( string input, string input2 )
{
double convert[10] = {0.0};
double convert2[10] = {0.0};
double result;
double result2;
double answer;
double digit1 = 1.0;
double digit2 = 16.0;
double digit3 = 16 * 16;
double digit4 = 16 * 16 * 16;
double digit5 = 16 * 16 * 16 * 16;
double digit6 = 16 * 16 * 16 * 16 * 16;
double digit7 = 16 * 16 * 16 * 16 * 16 * 16;
double digit8 = 16 * 16 * 16 * 16 * 16 * 16 * 16;
double digit9 = 16 * 16 * 16 * 16 * 16 * 16 * 16 * 16;
double digit10 = 16 * 16 * 16 * 16 * 16 * 16 * 16 * 16 * 16;
//reverses the inputted string
//converts A, B, C, D, E, and F to number values
for (int i = 0; i < input.length(); i++)
{
convert[i] = input[i];
if(input[i] == 'A')
convert[i] = 10;
if(input[i] == 'B')
convert[i] = 11;
if(input[i] == 'C')
convert[i] = 12;
if(input[i] == 'D')
convert[i] = 13;
if(input[i] == 'E')
convert[i] = 14;
if(input[i] == 'F')
convert[i] = 15;
}
if (input.length() == 1)
result = convert[0] * digit1;
else if (input.length() == 2)
result = (convert[0] * digit2) + (convert[1] * digit1);
else if (input.length() == 3)
result = (convert[0] * digit3) + (convert[1] * digit2) + (convert[2] * digit1);
else if (input.length() == 4)
result = (convert[0] * digit4) + (convert[1] * digit3) + (convert[2] * digit2) + (convert[3] * digit1);
else if (input.length() == 5)
result = (convert[0] * digit5) + (convert[1] * digit4) + (convert[2] * digit3) + (convert[3] * digit2) + (convert[4] * digit1);
else if (input.length() == 6)
result = (convert[0] * digit6) + (convert[1] * digit5) + (convert[2] * digit4) + (convert[3] * digit3) + (convert[4] * digit2) + (convert[5] * digit1);
else if (input.length() == 7)
result = (convert[0] * digit7) + (convert[1] * digit5) + (convert[2] * digit5) + (convert[3] * digit4) + (convert[4] * digit3) + (convert[5] * digit2) + (convert[6] * digit1);
else if (input.length() == 8)
result = (convert[0] * digit8) + (convert[1] * digit7) + (convert[2]) + (convert[3] * digit5) + (convert[4] * digit4) + (convert[5] * digit3) + (convert[6] * digit2) + (convert[7] * digit1);
else if (input.length() == 9)
result = (convert[0] * digit9) + (convert[1] * digit8) + (convert[2] * digit7) + (convert[3] * digit6) + (convert[4] * digit5) + (convert[5] * digit4) + (convert[6] * digit3) + (convert[7] * digit2) + (convert[8] * digit1);
else if (input.length() == 10)
result = (convert[0] * digit10) + (convert[1] * digit9) + (convert[2] * digit8) + (convert[3] * digit7) + (convert[4] * digit6) + (convert[5] * digit5) + (convert[6] * digit4) + (convert[7] * digit3) + (convert[8] * digit2) + (convert[9] * digit1);
//used to test dec results: cout << fixed << result << endl;
//converts for second input
for (int i = 0; i < input2.length(); i++)
{
convert2[i] = input2[i];
if(input2[i] == 'A')
convert2[i] = 10;
if(input2[i] == 'B')
convert2[i] = 11;
if(input2[i] == 'C')
convert2[i] = 12;
if(input2[i] == 'D')
convert2[i] = 13;
if(input2[i] == 'E')
convert2[i] = 14;
if(input2[i] == 'F')
convert2[i] = 15;
}
if (input2.length() == 1)
result2 = convert2[0] * digit1;
else if (input2.length() == 2)
result2 = (convert2[0] * digit2) + (convert2[1] * digit1);
else if (input2.length() == 3)
result2 = (convert2[0] * digit3) + (convert2[1] * digit2) + (convert2[2] * digit1);
else if (input2.length() == 4)
result2 = (convert2[0] * digit4) + (convert2[1] * digit3) + (convert2[2] * digit2) + (convert2[3] * digit1);
else if (input2.length() == 5)
result2 = (convert2[0] * digit5) + (convert2[1] * digit4) + (convert2[2] * digit3) + (convert2[3] * digit2) + (convert2[4] * digit1);
else if (input2.length() == 6)
result2 = (convert2[0] * digit6) + (convert2[1] * digit5) + (convert2[2] * digit4) + (convert2[3] * digit3) + (convert2[4] * digit2) + (convert2[5] * digit1);
else if (input2.length() == 7)
result2 = (convert2[0] * digit7) + (convert2[1] * digit5) + (convert2[2] * digit5) + (convert2[3] * digit4) + (convert2[4] * digit3) + (convert2[5] * digit2) + (convert2[6] * digit1);
else if (input2.length() == 8)
result2 = (convert2[0] * digit8) + (convert2[1] * digit7) + (convert2[2]) + (convert2[3] * digit5) + (convert2[4] * digit4) + (convert2[5] * digit3) + (convert2[6] * digit2) + (convert2[7] * digit1);
else if (input2.length() == 9)
result2 = (convert2[0] * digit9) + (convert2[1] * digit8) + (convert2[2] * digit7) + (convert2[3] * digit6) + (convert2[4] * digit5) + (convert2[5] * digit4) + (convert2[6] * digit3) + (convert2[7] * digit2) + (convert2[8] * digit1);
else if (input2.length() == 10)
result2 = (convert2[0] * digit10) + (convert2[1] * digit9) + (convert2[2] * digit8) + (convert2[3] * digit7) + (convert2[4] * digit6) + (convert2[5] * digit5) + (convert2[6] * digit4) + (convert2[7] * digit3) + (convert2[8] * digit2) + (convert2[9] * digit1);
// used to test dec result: cout << fixed << result2 << endl;
answer = result + result2;
backToHex(answer);
}
//converts answer in decimal to hex
void backToHex( double answer )
{
char hex[10] = {0};
//convert here
}
I'm not sure how to handle the conversion. Like I said, I think that I have to convert the "double answer" to a string, but I'm not sure how I would get the answer to base 16. I was thinking that it may be easier to convert the answer to binary and then to hex, but I'm not quite sure how to do that either. Any help would be greatly appreciated. Thank you for your time!