1.11M Members

I'm writing a program to add two hex numbers of up to ten digits, and I'm recieving
an "assertion failure," and I can't figure out why. Any help would be greatly appreciated. Thank you for you time!

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

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

using namespace std;

void convertToDec(string, string);

int _tmain(int argc, _TCHAR* argv[])
{

//assign values to last six hex characters
//convert both inputs to dec
//convert back go hex for answer

string input;
string input2;
string hex[16] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
int digit = 0;
bool decision;
char choice;

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;
}

void convertToDec(string input, string input2)
{
int convert[10] = {0};
int result;
int digit1 = 1;
int digit2 = 16;
int digit3 = 16 * 16;
int digit4 = 16 * 16 * 16;
int digit5 = 16 * 16 * 16 * 16;
int digit6 = 16 * 16 * 16 * 16 * 16;
int digit7 = 16 * 16 * 16 * 16 * 16 * 16;
int digit8 = 16 * 16 * 16 * 16 * 16 * 16 * 16;
int digit9 = 16 * 16 * 16 * 16 * 16 * 16 * 16 * 16;
int digit10 = 16 * 16 * 16 * 16 * 16 * 16 * 16 * 16 * 16;

//converts A, B, C, D, E, and F to number values
for (int i = input.length(); i >= 0; 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[1] * digit1;
if (input.length() == 2)
result = (convert[2] * digit2) + (convert[1] * digit1);
if (input.length() == 3)
result = (convert[3] * digit3) + (convert[2] * digit2) + (convert[1] * digit1);
if (input.length() == 4)
result = (convert[4] * digit4) + (convert[3] * digit3) + (convert[2] * digit2) + (convert[1] * digit1);
if (input.length() == 5)
result = (convert[5] * digit5) + (convert[4] * digit4) + (convert[3] * digit3) + (convert[2] * digit2) + (convert[1] * digit1);
if (input.length() == 6)
result = (convert[6] * digit6) + (convert[5] * digit5) + (convert[4] * digit4) + (convert[3] * digit3) + (convert[2] * digit2) + (convert[1] * digit1);
if (input.length() == 7)
result = (convert[7] * digit7) + (convert[6]) + (convert[5] * digit5) + (convert[4] * digit4) + (convert[3] * digit3) + (convert[2] * digit2) + (convert[1] * digit1);
if (input.length() == 8)
result = (convert[8] * digit8) + (convert[7] * digit7) + (convert[6]) + (convert[5] * digit5) + (convert[4] * digit4) + (convert[3] * digit3) + (convert[2] * digit2) + (convert[1] * digit1);
if (input.length() == 9)
result = (convert[9] * digit9) + (convert[8] * digit8) + (convert[7] * digit7) + (convert[6]) + (convert[5] * digit5) + (convert[4] * digit4) + (convert[3] * digit3) + (convert[2] * digit2) + (convert[1] * digit1);
if (input.length() == 10)
result = (convert[10] * digit10) + (convert[9] * digit9) + (convert[8] * digit8) + (convert[7] * digit7) + (convert[6]) + (convert[5] * digit5) + (convert[4] * digit4) + (convert[3] * digit3) + (convert[2] * digit2) + (convert[1] * digit1);

}``````

I didn't try to understand what you are doing in your code. But, I see one mistake.
Variables like digit10 which is of int type cannot hold such a big value.
If int is of 4 bytes, it can go only till 2147483647 but digit10 is assigned with 68719476736.

I didn't try to understand what you are doing in your code. But, I see one mistake.
Variables like digit10 which is of int type cannot hold such a big value.
If int is of 4 bytes, it can go only till 2147483647 but digit10 is assigned with 68719476736.

Ah, okay. I'll fix that and see how it works out. Thanks!

I didn't try to understand what you are doing in your code. But, I see one mistake.
Variables like digit10 which is of int type cannot hold such a big value.
If int is of 4 bytes, it can go only till 2147483647 but digit10 is assigned with 68719476736.

It seems the assertion failure is coming from my for statement in line 81. The purpose of this for loop is to print the "input" string backwards, and assign those values the the array of "convert" array. How do you think I can modify this so that I won't have an assertion error? The error it said is "Expression: string subscript out of range."

If you have the value "ABCDEF" in input, what is the value of input.length()?

If i = input.length(), which character is input[i] ?
Remember, zero based values... :icon_wink:

It seems the assertion failure is coming from my for statement in line 81. The purpose of this for loop is to print the "input" string backwards, and assign those values the the array of "convert" array. How do you think I can modify this so that I won't have an assertion error? The error it said is "Expression: string subscript out of range."

Yes, might be. As I told you, I didn't go through the entire code. But at first glance I spotted out the integer overflow error with digit10 and digit9.
Go ahead with correcting the error pointed out by WaltP. It may solve the assertion problem. But at some point the integer overflow problem will trouble you. :)

If you have the value "ABCDEF" in input, what is the value of input.length()?

If i = input.length(), which character is input[i] ?
Remember, zero based values... :icon_wink:

In that case it's be 7 right? I'm sorry, arrays confuse me xD

In that case it's be 7 right? I'm sorry, arrays confuse me xD

Count the letters...

You