One error I see is in line 10. it should be answer[i] = answer[i] + result[i] + result2[i]; otherwise the carry-over wont work.

This function seems OK, the problem is most likely with the format of the results - you seem to keep them starting from the highest exponent and omit the leading zeroes.
Try debugging it. Set a breakpoint at the beginning of this function and see what the results look like.

And two tips:
Use pass by reference or pass by pointer here - right now you just unnecessarily copy those arrays.
Make char hex[16] const - this will help the compiler optimize this function.

One error I see is in line 10. it should be answer[i] = answer[i] + result[i] + result2[i]; otherwise the carry-over wont work.

This function seems OK, the problem is most likely with the format of the results - you seem to keep them starting from the highest exponent and omit the leading zeroes.
Try debugging it. Set a breakpoint at the beginning of this function and see what the results look like.

And two tips:
Use pass by reference or pass by pointer here - right now you just unnecessarily copy those arrays.
Make char hex[16] const - this will help the compiler optimize this function.

I'm new to c++, and I don't know how to implement those things that you're talking about. I'm very stressed, this program is due tomorrow. I didn't slack off, I've been trying to get this for a while

Sorry, for line ten it should be answer[i] += answer[i] + result[i] + result2[i]; forgot to add the plus after copying.

Didn't your teachers tell you how to debug? Argh...

The place where your mistake lies - that I can't check with just this much code. That is why I asked you to check it yourself, but you say you don't know how.

Either enter your IDE's name in google adding "debugging tutorial" or post the whole code.

Sorry, for line ten it should be answer[i] += answer[i] + result[i] + result2[i]; forgot to add the plus after copying.

Didn't your teachers tell you how to debug? Argh...

The place where your mistake lies - that I can't check with just this much code. That is why I asked you to check it yourself, but you say you don't know how.

Either enter your IDE's name in google adding "debugging tutorial" or post the whole code.

Here's my full code:

// 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;
	cout << result[0] << " " <<  result[1] << endl;
	cout << result2[0] << " " << result2[1] << endl;
	cout << "-------------------------" << endl;
	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;
		
	for (int i = 0; i < 10; i++)
		cout << answer[i] << " " ; cout << endl;

	
}

Ok, now I get it.

It was in this function.
Your problem is that you output the numbers reversed. Just change the loops in lines 221 and 224 for

for (int i = 9; i >= 0; i--)

should work.

And really, DO learn debugging - with it I discovered your problem in matter of minutes. Plus, debugging in Visual Studio is IMO pretty intuitive.

Ok, now I get it.

It was in this function.
Your problem is that you output the numbers reversed. Just change the loops in lines 221 and 224 for

for (int i = 9; i >= 0; i--)

should work.

And really, DO learn debugging - with it I discovered your problem in matter of minutes. Plus, debugging in Visual Studio is IMO pretty intuitive.

Well, that fixes the problem with how it outputs, but it doesn't output the right answer. Try entering in AB for the first input, and 1 for the second. It gives you BB. Which means that it added the 1 to the A, rather than the B like it's suppowed to. How do I fix this?

My bad.

Turns out reversing the loops wasn't such a good idea. It doesn't fix anything, just brakes some more.

The point here is, that if you convert strings to numbers for computation, they should be of the same length. For the program to work properly, you have to add '0' at the beginning of both strings until their length is 10.

My bad.

Turns out reversing the loops wasn't such a good idea. It doesn't fix anything, just brakes some more.

The point here is, that if you convert strings to numbers for computation, they should be of the same length. For the program to work properly, you have to add '0' at the beginning of both strings until their length is 10.

I see what you mean. I'm not sure how to accommodate for that though. What would you suggest I do? Please excuse my ignorance, I probably sound very helpless, but I am trying.

while(input.length()<10)input="0"+input;

same for input2

while(input.length()<10)input="0"+input;

same for input2

When add this into the for loop in my "convertToDec" function, I get a runtime failure unless there are 0's before the number.

Padding to the left is the same as right justifying. You first need to determine the length of both strings. Then substract the length of the shorter string from length of the longer string. Create a string of zeros as long as the difference between the 2 strings and addend the shorter string. So:

EFFF + 1

//Right justify (pad to the left) the shorter string
EFFF has length 4
1 has length 1
4 - 1 = 3
create a string with 3 0s and addend 1 to the end to get 0001

//Then add EFFF and 0001 
string1 = EFFF
string2 = 0001

//convert hex char of string1 and string 2 to equivalent ints.
intArray1 = [14][15][15][15]
intArray2 = [ 0][ 0][ 0][ 1]

//add ints with same index
for(int i = 0; i < length; +i)
  intArray3[i] = intArray1[i] + intArray2[i];  //intArray3 = [14][15][15][16]

//normalize intArray3 so each int is 0-15, going right to left
intArray3 = [15][0][0][0]

//convert intArray3 to hex chars
char hexChar[17] = "0123456789ABCDEF";
string hexString = "";
for(int i = 0; i < length of intArray3; ++i)
  hexString += hexChar[intArray3[i]]

When I add this into the for loop in my "convertToDec" function, I get a runtime failure unless there are 0's before the number.

Well, I guess you have already handed in your homework since it is the end of Jan 31 so I guess I can spill some more beans. It seems as though your algorithm is more complex than necessary. But then maybe I don't know exactly what you are trying to accomplish as far as formatting, etc,. In any case, here is a method of converting a double decimal value (a whole positive number) into a hex string. Maybe you can find something helpful in it:

#include<iostream>
using std::cout; using std::cin; using std::endl;

int main()
{                              
   cout << "\n\nInput number: ";
   double answer; cin >> answer;
      
   double divisor = 1;
   for(; divisor <= answer; divisor *= 16);         //loop generates divisor
   divisor /= 16;                                   //adjusts divisor to be < answer 

   int hexDigit = 0;                                //holds a hex digit
   do
   {
     hexDigit = static_cast<int>(answer / divisor); //generates a hex digit

     if(hexDigit > 9)                              //if digit > 9, 
        cout << static_cast<char>(hexDigit + 55);  //then print A-F, 
     else                                          //else just print digit.
        cout << hexDigit;

     answer -= (hexDigit * divisor);               //answer now contains remainder
    divisor /= 16;                                 //adjust divisor for next loop


   }while(divisor >= 1);                           //end loop when divsor is < 1

   cout << endl << endl << endl;
   return 0;
}

Well, I guess you have already handed in your homework since it is the end of Jan 31 so I guess I can spill some more beans. It seems as though your algorithm is more complex than necessary. But then maybe I don't know exactly what you are trying to accomplish as far as formatting, etc,. In any case, here is a method of converting a double decimal value (a whole positive number) into a hex string. Maybe you can find something helpful in it:

#include<iostream>
using std::cout; using std::cin; using std::endl;

int main()
{                              
   cout << "\n\nInput number: ";
   double answer; cin >> answer;
      
   double divisor = 1;
   for(; divisor <= answer; divisor *= 16);         //loop generates divisor
   divisor /= 16;                                   //adjusts divisor to be < answer 

   int hexDigit = 0;                                //holds a hex digit
   do
   {
     hexDigit = static_cast<int>(answer / divisor); //generates a hex digit

     if(hexDigit > 9)                              //if digit > 9, 
        cout << static_cast<char>(hexDigit + 55);  //then print A-F, 
     else                                          //else just print digit.
        cout << hexDigit;

     answer -= (hexDigit * divisor);               //answer now contains remainder
    divisor /= 16;                                 //adjust divisor for next loop


   }while(divisor >= 1);                           //end loop when divsor is < 1

   cout << endl << endl << endl;
   return 0;
}

Hello, I did turn it in, and unfortunately it was incomplete, but I'm curious as to mine isn't working correctly. Here's my code:

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 = 9; i >= 0; i--)
	{
		answer[i] += answer[i] + result[i] + result2[i];
 
		if (answer[i] > 15)
		{
			answer[i - 1] = (answer[i] / 16);
			answer[i] %= 16;
		}
	}
	if (answer[0] > 15)
		cout << "----------Addition Overflow----------" << endl;
 
	else 
	for (int i = 0; i < 10; i++)
		cout << hex[answer[i]] << " "; cout << endl;
 
	
 
}

The problem seems to be somewhere in my first for statement with moving a value to the next element of the array. Instead of adding a 1, it seems to be adding a 2. I'm not sure why

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.