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!

Recommended Answers

All 42 Replies

Are you allowed to use standard C functions? sprintf() can do all that for you, just a matter of giving it the correct forma spcification. E.g. "%x" converts to hex and "%f" to float

Are you allowed to use standard C functions? sprintf() can do all that for you, just a matter of giving it the correct forma spcification. E.g. "%x" converts to hex and "%f" to float

Unfortunately, no. I need to write my own function to do it

Here's the function that I've made to do this, however, it's not perfect. It only works correctly for single digit numbers. Double digits and up are problematic.

//converts answer in decimal to hex
void backToHex( double answer )
{
	int remainder;
	int a = answer;
	int b = a / 16;
	char hex[11];
	for (int k = 1; k < 10; k++)
	{
		remainder = a % 16;

		if (remainder == 10)
			hex[k] = 'A';
		else if (remainder == 11)
			hex[k] = 'B';
		else if (remainder == 12)
			hex[k] = 'C';
		else if (remainder == 13)
			hex[k] = 'D';
		else if (remainder == 14)
			hex[k] = 'E';
		else if (remainder == 15)
			hex[k] = 'F';
		hex[0] = b;
		if (b == 10)
			hex[0] = 'A';
		else if (b == 11)
			hex[0] = 'B';
		else if (b == 12)
			hex[0] = 'C';
		else if (b == 13)
			hex[0] = 'D';
		else if (b == 14)
			hex[0] = 'E';
		else if (b == 15)
			hex[0] = 'F';
	}
	if (hex[11] > 0)
		{
			cout << "----------Addition Overflow----------" << endl;
		}
	else 
		cout << hex[0] << hex[1] << endl;
}

If I entered AB for the first input, my output would be: D, but it should be A. Why is that? Is it because "answer" was a double and I changed it to an int? And I doubt that this will handle multiple digits. Any help would be greatly appreciated.

Here's the function that I've made to do this, however, it's not perfect. It only works correctly for single digit numbers. Double digits and up are problematic.

//converts answer in decimal to hex
void backToHex( double answer )
{
	int remainder;
	int a = answer;
	int b = a / 16;
	char hex[11];
	for (int k = 1; k < 10; k++)
	{
		remainder = a % 16;

		if (remainder == 10)
			hex[k] = 'A';
		else if (remainder == 11)
			hex[k] = 'B';
		else if (remainder == 12)
			hex[k] = 'C';
		else if (remainder == 13)
			hex[k] = 'D';
		else if (remainder == 14)
			hex[k] = 'E';
		else if (remainder == 15)
			hex[k] = 'F';
		hex[0] = b;
		if (b == 10)
			hex[0] = 'A';
		else if (b == 11)
			hex[0] = 'B';
		else if (b == 12)
			hex[0] = 'C';
		else if (b == 13)
			hex[0] = 'D';
		else if (b == 14)
			hex[0] = 'E';
		else if (b == 15)
			hex[0] = 'F';
	}
	if (hex[11] > 0)
		{
			cout << "----------Addition Overflow----------" << endl;
		}
	else 
		cout << hex[0] << hex[1] << endl;
}

If I entered AB for the first input, my output would be: D, but it should be A. Why is that? Is it because "answer" was a double and I changed it to an int? And I doubt that this will handle multiple digits. Any help would be greatly appreciated.

Ouch!
Use a character array for the HEX characters. Use remainder as an index into that array to 'convert' the remainder into a hex digit. Now your 30-line conversion is 2 lines.
That it should make it easier to see what you need to do next.

Ouch!
Use a character array for the HEX characters. Use remainder as an index into that array to 'convert' the remainder into a hex digit. Now your 30-line conversion is 2 lines.
That it should make it easier to see what you need to do next.

This is what you meant right?

char hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
	

	for (int k = 0; remainder < 10; k++)
	{
                remainder = a % 16;
		hex[remainder];
	
	}

I know you probably think I'm dumb... But I'm trying to understand. Arrays are very new to me

commented: YES!! Plus what deceptikon said... +17

Close, don't forget 0. It makes all the difference. :) It's also possible to use a trick with strings and avoid typing out the whole array:

char const* hex = "0123456789ABCDEF";

// get the least significant digit in hex
char digit = hex[value % 16];

Except what's that loop for?

And what are you doing with hex[remainder];?

I know you probably think I'm dumb... But I'm trying to understand. Arrays are very new to me

No, just inexperienced. We were all there at one time...

Except what's that loop for?

And what are you doing with hex[remainder];?


No, just inexperienced. We were all there at one time...

I don't need the loop. I realize that now, and as for the "hex[remainder]," I changed it to "hex[answer % 16];" which gives me the last digit. I'm not quite sure how to proceed...

Except what's that loop for?

And what are you doing with hex[remainder];?


No, just inexperienced. We were all there at one time...

I figured out how to get the second digit, however, my problem from an earlier post still applies. If I enter "AB," then that in decimal is 171. If the second input is "1," then 1 + 171 would be 172. However, if I divide 172 by 16, then it spits out a "D." Rather than an "A," which it should be. It seems as though it has converted the second input to 49. Is that an error in my convertToDec function? How would I fix it?

Well, you you are going backwards and just broke your solution.

What is remainder?
What is the result of hex[remainder]?
What happens to that result without an = sign somewhere?
How does changing [remainder] to [answer % 16] solve your problem?

You need to start thinking through the problems, not using a shot-gun technique and hoping you hit upon the solution by trial and error.

Although it is a different task, this thread has a bunch of concepts spelled out you can use while trying to understand this weird programming lifestyle.

I've fixed the problem that I had earlier by adding another function to do the addition. Adding two digits together words perfectly, unless the answer is more than two digits. I'm not quite sure how to do this for more than one digit. Any suggestions? I can't post code right now, because I'm on a different computer, but I can provide what I've done later if necessary. Thanks!

Well, you you are going backwards and just broke your solution.

What is remainder?
What is the result of hex[remainder]?
What happens to that result without an = sign somewhere?
How does changing [remainder] to [answer % 16] solve your problem?

You need to start thinking through the problems, not using a shot-gun technique and hoping you hit upon the solution by trial and error.

Although it is a different task, this thread has a bunch of concepts spelled out you can use while trying to understand this weird programming lifestyle.

I realize that those things aren't what I need to do. I have an algorithm for what I need to do, but I'm not quite sure how to do it.
-Use an array of 10, with each element of the array representing the number of 16^whatever power's are in the answer
-Index hex array with the values of the above array

That sounds better in my head, I hope it makes sense.

>>Adding two digits together words perfectly, unless the answer is more than two digits. I'm not quite sure how to do this for more than one digit.

When you can't figure something out in your head, go back to pencil and paper. Things are generally easier to figure out that way. Here's one scenario that might work for you, assuming it is modeling the problem I think you are having.

Example 1:
AC + EF = [10][12] //AC as array of int 
        + [14][15] //EF as array of int
          -------- //add columns
          [24][27] //work right to left to get each cell less than 16
          [24][16 + 11] //16 with remainder 11, carry 16 as a 1 to next cell to left
          [24 + 1][11]          
          [25][11]
         [16 + 9][11] //16 with remainder 9, carry 16 as a 1 to next cell to left
         [1][9][11]  
           19B       //convert int values in each cell to hex char

Example 2:
FFF + 11 = [15][15][15]
         +     [ 1][ 1]
         ______________
           [15][16][16]
           [15][17][ 0]
           [16][ 1][ 0]
        [ 1][ 0][ 1][ 0]
            1010 is hex representation of the sum

In general:
1) convert hex string to int array where each char becomes an int with the same index as the char and has a value that ranges from 0-15.  Repeat for the other hex string
2) right justify the int arrays, padding the smaller array to the left if needed
3) add ints with same index to generate a sum and place that sum in the appropriate cell of the resultant int array

4) using the result int array from #3 and working right to left, 
5) if int > 15 in this cell subtract 16 from this int
6) the remainder (or the original value if it was less than 16) remains in this cell
7) if the starting int was over 15 then add one to the cell to left of current cell
repeat for cell to left of current one until all cells evaluated
8) once all ints in the array have values under 16, convert each int back into a hex char and place it in the appropriate cell of the result hex string

Well, you you are going backwards and just broke your solution.

What is remainder?
What is the result of hex[remainder]?
What happens to that result without an = sign somewhere?
How does changing [remainder] to [answer % 16] solve your problem?

You need to start thinking through the problems, not using a shot-gun technique and hoping you hit upon the solution by trial and error.

Although it is a different task, this thread has a bunch of concepts spelled out you can use while trying to understand this weird programming lifestyle.

I think I've got it now, except for one part. Here's what I've done:

//converts answer in decimal to hex
void backToHex( double answer )
{
	char hex[16] = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};	
	int remainder[11] = {0};

	if (remainder[11] != 0)
	{
		cout << "----------Addition Overflow----------" << endl;
	}
	remainder[0] = answer / 16; 
	//remainder[1] = not sure what to put here, as I cannot use modulus
	remainder[2] = answer / 256; //16 to the 2
	remainder[3] = answer / 4096; //16 to the 3
	remainder[4] = answer / 65536; //16 to the 4
	remainder[5] = answer / 1048576; //16 to the 5
	remainder[6] = answer / 16777216; //16 to the 6
	remainder[7] = answer / 268435456; //16 to the 7
	remainder[8] = answer / 4294967296; //16 to the 8
	remainder[9] = answer / 68719476736; //16 to the 9

	for (int i = 0; i < 10; i++)
		cout << hex[remainder[i]];
}

For the second digit in the number, It would be ideal to use modulus, but since my answer is a double, that is not an option. How would you suggest that I go about getting that digit?

If you feel you must continue with your approach, and you really want to try to use modulus on a type double, then you could write your own function to mimic the modulus operator for ints.

take 2 variables x and y;
if x < y what is the remainder if you remove as many x's from y as you can.

OR in pseudocode, maybe somehting like this:

x, y, remainder 
remainder = y
while x less than or equal to remainder
  remainder = remainder - x

>>you really want to try to use modulus on a type double, then you could write your own function to mimic the modulus operator for ints.

or just call standard C function fmod()

If you feel you must continue with your approach, and you really want to try to use modulus on a type double, then you could write your own function to mimic the modulus operator for ints.

take 2 variables x and y;
if x < y what is the remainder if you remove as many x's from y as you can.

OR in pseudocode, maybe somehting like this:

x, y, remainder 
remainder = y
while x less than or equal to remainder
  remainder = remainder - x

Is there an easier approach that I'm not seeing?

If you feel you must continue with your approach, and you really want to try to use modulus on a type double, then you could write your own function to mimic the modulus operator for ints.

take 2 variables x and y;
if x < y what is the remainder if you remove as many x's from y as you can.

OR in pseudocode, maybe somehting like this:

x, y, remainder 
remainder = y
while x less than or equal to remainder
  remainder = remainder - x

Here's what I've done:

void backToHex( double answer )
{
	int a = answer;
	char hex[16] = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};	
	int remainder[11] = {0};

	if (remainder[11] != 0)
	{
		cout << "----------Addition Overflow----------" << endl;
	}
    remainder[0] = mod(answer, 16.0);
	cout << remainder[0];
}

double mod(double answer, double y)
{
	double remainder = y;
	while (answer <= remainder)
		remainder = remainder - answer;
	
	return remainder;
}

However, It always returns 16. Why is that?

If you feel you must continue with your approach, and you really want to try to use modulus on a type double, then you could write your own function to mimic the modulus operator for ints.

take 2 variables x and y;
if x < y what is the remainder if you remove as many x's from y as you can.

OR in pseudocode, maybe somehting like this:

x, y, remainder 
remainder = y
while x less than or equal to remainder
  remainder = remainder - x

If you feel you must continue with your approach, and you really want to try to use modulus on a type double, then you could write your own function to mimic the modulus operator for ints.

take 2 variables x and y;
if x < y what is the remainder if you remove as many x's from y as you can.

OR in pseudocode, maybe somehting like this:

x, y, remainder 
remainder = y
while x less than or equal to remainder
  remainder = remainder - x

Here's what I've done:

void backToHex( double answer )
{
	int a = answer;
	char hex[16] = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};	
	int remainder[11] = {0};

	if (remainder[11] != 0)
	{
		cout << "----------Addition Overflow----------" << endl;
	}
    remainder[0] = mod(answer, 16.0);
	cout << remainder[0];
}

double mod(double answer, double y)
{
	double remainder = y;
	while (answer <= remainder)
		remainder = remainder - answer;
	
	return remainder;
}

However, It always returns 16. Why is that?

I have been assigned a project to add two hexadecimal numbers of up to ten digits. I've taken the two inputs, and converted them to decimal. But I can't figure out how to convert the decimal number back to hexadecimal. I really need some help with this because the program is due Tuesday (Jan 31). I've been staring at a computer screen trying things all weekend, and I can't figure it out. Here's my code: it's long, so you don't have to read it if you don't want to. I tried to write a function to mimic modulus as well, and that didn't work out. Thanks for all of your help!

// hex addition.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

void convertToDec(string, string);
void add(double, double);
void backToHex(double);
double mod(double, 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;
		cout << endl;
		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");
		}
		else 
		{
			cout << "Invalid character entered." << endl;
			decision = false;
			system("pause");
			return 0;
		}
	} 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;	
	add(result, result2);

} 
void add(double result, double result2)
{
	
	double answer;
	//fixes values of result
	if (result == 48)
		result = 0;
	else if (result == 49)
		result = 1;
	else if (result == 50)
		result = 2;
	else if (result == 51)
		result = 3;	
	else if (result == 52)
		result = 4;
	else if (result == 53)
		result = 5;
	else if (result == 54)
		result = 6;
	else if (result == 55)
		result = 7;
	else if (result == 56)
		result = 8;
	else if (result == 57)
		result = 9;
	
	//fixes values of result2
	if (result2 == 48)
		result2 = 0;
	else if (result2 == 49)
		result2 = 1;
	else if (result2 == 50)
		result2 = 2;
	else if (result2 == 51)
		result2 = 3;
	else if (result2 == 52)
		result2 = 4;
	else if (result2 == 53)
		result2 = 5;
	else if (result2 == 54)
		result2 = 6;
	else if (result2 == 55)
		result2 = 7;
	else if (result2 == 56)
		result2 = 8;
	else if (result2 == 57)
		result2 = 9;
	
	cout << " " << result << endl;
	cout << "+" << result2 << endl;
	answer = result + result2;
	backToHex(answer);
}


//converts answer in decimal to hex
void backToHex( double answer )
{
	//char hex[16] = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};	
	int remainder[10] = {0.0};
	int digitPlace;
	
	if (answer >= 1 && answer < 10)
		digitPlace = 1;
	if (answer >= 10 && answer < 100)
		digitPlace = 2;
	if (answer >= 100 && answer < 1000)
		digitPlace = 3;
	if (answer >= 1000 && answer < 10000)
		digitPlace = 4;
	if (answer >= 10000 && answer < 100000)
		digitPlace = 5;
	if (answer >= 100000 && answer < 1000000)
		digitPlace = 6;
	if (answer >= 1000000 && answer < 10000000)
		digitPlace = 7;
	if (answer >= 10000000 && answer < 100000000)
		digitPlace = 8;
	if (answer >= 100000000 && answer < 1000000000)
		digitPlace = 9;
	if (answer >= 1000000000 && answer < 10000000000)
		digitPlace = 10;
	if (answer >= 10000000000 && answer < 100000000000)
		digitPlace = 11;
	if (answer >= 100000000000 && answer < 1000000000000)
		digitPlace = 12;
	if (answer >= 1000000000000 && answer < 10000000000000)
		digitPlace = 13;
	
	for (int i = 15; i > 0; i--)
	{
		
	}
}

double mod(double answer, double y)
{
	double remainder = y;
	while (answer <= remainder)
		remainder -= answer;
	
	return remainder;
}

That's a massive algorithm. Okay, well I don't know if you have to generate an algorithm for it or not, but if you don't I believe the ToString method still works in C++.

myDecimalValue->ToString("x");

I'm not 100% sure on it, but that's what we use in C# because it's simpler; I have an algorithm in C# but I don't have enough C++ knowledge to convert it over completely.

That's a massive algorithm. Okay, well I don't know if you have to generate an algorithm for it or not, but if you don't I believe the ToString method still works in C++.

myDecimalValue->ToString("x");

I'm not 100% sure on it, but that's what we use in C# because it's simpler; I have an algorithm in C# but I don't have enough C++ knowledge to convert it over completely.

I'm not sure if I can use that. My teacher wants us to do all of this manually

I need to add two hexadecimal numbers together. I've converted the two to decimal, but I can't figure out how to convert to hexadecimal. This needs to work for up to ten digits. I can't use standard i.o. or any other function. I need to do it manually. Here's my code, only the bottom lines really matter, but i included the rest for reference:

// hex addition.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

void convertToDec(string, string);
void add(double, double);
void backToHex(double);
double mod(double, 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;
		cout << endl;
		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");
		}
		else 
		{
			cout << "Invalid character entered." << endl;
			decision = false;
			system("pause");
			return 0;
		}
	} 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;	
	add(result, result2);

} 
void add(double result, double result2)
{
	
	double answer;
	//fixes values of result
	if (result == 48)
		result = 0;
	else if (result == 49)
		result = 1;
	else if (result == 50)
		result = 2;
	else if (result == 51)
		result = 3;	
	else if (result == 52)
		result = 4;
	else if (result == 53)
		result = 5;
	else if (result == 54)
		result = 6;
	else if (result == 55)
		result = 7;
	else if (result == 56)
		result = 8;
	else if (result == 57)
		result = 9;
	
	//fixes values of result2
	if (result2 == 48)
		result2 = 0;
	else if (result2 == 49)
		result2 = 1;
	else if (result2 == 50)
		result2 = 2;
	else if (result2 == 51)
		result2 = 3;
	else if (result2 == 52)
		result2 = 4;
	else if (result2 == 53)
		result2 = 5;
	else if (result2 == 54)
		result2 = 6;
	else if (result2 == 55)
		result2 = 7;
	else if (result2 == 56)
		result2 = 8;
	else if (result2 == 57)
		result2 = 9;
	
	cout << " " << result << endl;
	cout << "+" << result2 << endl;
	answer = result + result2;
	backToHex(answer);
}


//converts answer in decimal to hex
void backToHex( double answer )
{
	//char hex[16] = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};	
	int remainder[10] = {0.0};
	int digitPlace;
	
	if (answer >= 1 && answer < 10)
		digitPlace = 1;
	if (answer >= 10 && answer < 100)
		digitPlace = 2;
	if (answer >= 100 && answer < 1000)
		digitPlace = 3;
	if (answer >= 1000 && answer < 10000)
		digitPlace = 4;
	if (answer >= 10000 && answer < 100000)
		digitPlace = 5;
	if (answer >= 100000 && answer < 1000000)
		digitPlace = 6;
	if (answer >= 1000000 && answer < 10000000)
		digitPlace = 7;
	if (answer >= 10000000 && answer < 100000000)
		digitPlace = 8;
	if (answer >= 100000000 && answer < 1000000000)
		digitPlace = 9;
	if (answer >= 1000000000 && answer < 10000000000)
		digitPlace = 10;
	if (answer >= 10000000000 && answer < 100000000000)
		digitPlace = 11;
	if (answer >= 100000000000 && answer < 1000000000000)
		digitPlace = 12;
	if (answer >= 1000000000000 && answer < 10000000000000)
		digitPlace = 13;
	
}

double mod(double answer, double y)
{
	double remainder = y;
	while (answer <= remainder)
		remainder -= answer;
	
	return remainder;
}

If I'm not mistaken there's a function in one of the stream library's that allowes you to change the type (if that's the correct word) of number. This might require creating a stream, which may or may not be what you want to do.

Hope this helps,
Jack

First of all, iterate over those powers, don't just put them in the code.

Putting a number to any given base always used a Greedy Algorithm and that is enough for you too.

The only part, that could get tricky here is computing the modulus for floating point numbers, but here is a decent thread on this topic. It IS a bit better than your solution (imagen numbers like 10e10 mod 10e1 - your way would take hours to compute).

I hope you understand this. If not, try googling a bit, or just ask for clarification.

BTW: haven't I seen a number of threads on this topic in this very forum a few days ago?

BTW2: @lxXTaCoXxl - not really, doubles aren't objects of a class in C++. Remember, C++ is not fully object.

An example of my interpretation of your request is to convert 3.141593 to a hex value of DB 0F 49 40.

If my interpretation of your problem is correct, you have to use a union structure containing your float and a byte array (unsigned char). Iterate thru this union and your homework is done.

assign answer to remainder on line 17.

on line 18 it should be while y is less than or equal to remainder.

on line 19 it should be remainder = remainder minus y.

Result of mod() will be to return the remainder when as many y can be removed from answer as possible.

Note: backToHex() as most recently posted will only put a value into remainder[0]. Every time backToHex is called a new remainder[] array will be made and only remainder[0] will ever be used. That is unlikely to get you where you want to go no matter which approach you use.

To convert a double to a hex (without using built in standard conversion) I would use a loop to check all values of power of 16 as y against the remainder of the input minus as many of the previous powers of 16 that could be removed. If the current power of 16 is bigger than the remainder then the value of that power in output would be 0. If not you need to determine how many times that power of 16 can be removed from the number and what the remainder would be after that. The number of times that the current power of 16 could be removed would be placed in the appropriate place in an int arrray representing the digital equivalent of a hex char and the remainder would be carried forward to be measured against the next lower power of 16 and the whole proces would be repeated until the remainder was under 16.

int intArrayAnswer[8]
int index = 0
power0f16 = 8;
remainder = input;
//find largest power of 16 smaller than input---in this example I assume that the largetst power of 16 to be expected in 8
while()
 currentP16 = pow(16, powerOf16)
 if(remainder < currentP16)
   break;
 else
   --powerOf16

//if powerOf16 more than 0 then 
while(remainder > 16)
  currentP16 = pow(16, powerOf16);
  count = 0;
  while(remainder >= currentP16)
    remainder -= currentP16
    ++count
  intArrayAnswer[index++] = count
intArrayAnswer[index] = remainder
//now intArrayAnswer should have up to 8 ints each ranging from zero to 15;
//convert each int in intArrayAnswer into a hex char to get answer as hex string.

I realized that I've been doing the assignment wrong. There was no need to get a real number. I just need to store the values in arrays. Here's what I've done: however, it seems to add the two arrays incorrectly

// hex addition.cpp : Defines the entry point for the console application.
//

/*
useful character functions:
char toupper( char ) -> myChar = toupper( myChar );
bool isdigit( char ) -> true if a digit
bool isalnum( char ) -> true if alphanumeric
char tolower( char )
bool isalpha( char ) -> true if alphabetic
*/

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

void convertToDec(string, string);
void reassign(int[], int[], int);
void backToHex(int[], int[], int);

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;
		cout << endl;
		if (input.length() > 10)
		{
			cout << "Invalid input" << endl;
			system("pause");
			return 0;
		}

		if (input2.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");
		}
		else 
		{
			cout << "Invalid character entered." << endl;
			decision = false;
			system("pause");
			return 0;
		}
	} while (decision = true);
	system("pause");
	return 0;
}

//used to convert the two inputs to decimal
void convertToDec( string input, string input2 )
{
	int convert[10] = {0.0};
	int convert2[10] = {0.0};
	
	//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;
		}
	//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;
		}
	int size = 10;
	reassign(convert, convert2, size);

} 


void reassign(int result[], int result2[], int size)
{
	
	//fixes values of result
	for (int i = 0; i < 10; i++)
	{
		if (result[i] == 48)
		result[i] = 0;
		else if (result[i] == 49)
			result[i] = 1;
		else if (result[i] == 50)
			result[i] = 2;
		else if (result[i] == 51)
			result[i] = 3;	
		else if (result[i] == 52)
			result[i] = 4;
		else if (result[i] == 53)
			result[i] = 5;
		else if (result[i] == 54)
			result[i] = 6;
		else if (result[i] == 55)
			result[i] = 7;
		else if (result[i] == 56)
			result[i] = 8;
		else if (result[i] == 57)
			result[i] = 9;
	}

	//fixes values of result2
	for (int i = 0; i < 10; i++)
	{
		if (result2[i] == 48)
		result2[i] = 0;
		else if (result2[i] == 49)
			result2[i] = 1;
		else if (result2[i] == 50)
			result2[i] = 2;
		else if (result2[i] == 51)
			result2[i] = 3;
		else if (result2[i] == 52)
			result2[i] = 4;
		else if (result2[i] == 53)
			result2[i] = 5;
		else if (result2[i] == 54)
			result2[i] = 6;
		else if (result2[i] == 55)
			result2[i] = 7;
		else if (result2[i] == 56)
			result2[i] = 8;
		else if (result2[i] == 57)
			result2[i] = 9;
	}

	int size = 10;
	backToHex(result, result2, size);
	
}

//converts answer in decimal to hex
void backToHex( int result[], int result2[], int size)
{
	char hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
	int answer[10] = {0};
	
	for (int i = 0; i < 10; i++)
	{
		answer[i] = answer[i] + result[i] + result2[i];
		if (answer[i] > 15)
		{
			answer[i + 1] = answer[i] / 16;
			answer[i] %= 16;
		}
	}
	if (answer[9] > 15)
		cout << "----------Addition Overflow----------" << endl;
	
	else 
	for (int i = 0; i < 10; i++)
		cout << hex[answer[i]];
	cout << endl;
	
}

I'm writing a program that adds two hexadecimal numbers. There seems to be a problem now. If I enter AB for the first input, and 1 for the second, then it gives me BB as the result. Which, obviously, is wrong. The answer should be AC. The problem is in this function, and I can't figure out how to fix it. Here's my code:

//converts answer in decimal to hex
void backToHex( int result[], int result2[], int size)
{
	char hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
	int answer[10] = {0};

	for (int i = 0; i < 10; i++)
	{
                answer[i] = answer[i] + result[i] + result2[i];
		if (answer[i] > 15)
		{
			answer[i + 1] = answer[i] / 16;
			answer[i] %= 16;
		}
	}
	if (answer[9] > 15)
		cout << "----------Addition Overflow----------" << endl;
	
	else 
	for (int i = 0; i < 10; i++)
		cout << hex[answer[i]];
	cout << endl << answer[0] << answer[1] << endl;
	
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.