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
//add two dec numbers
//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;
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;
}
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);
}