I'm writing a program that adds two hexadecimal numbers of up to ten digits.
I'm trying to use the POW function, but for some reason it's underlined. Why is that?
// 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 = pow(16, 0);
int digit2 = pow(16, 1);
int digit3 = pow(16, 2);
int digit4 = pow(16, 3);
int digit5 = pow(16, 4);
int digit6 = pow(16, 5);
int digit7 = pow(16, 6);
int digit8 = pow(16, 7);
int digit9 = pow(16, 8);
int digit10 = pow(16, 9);
//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;
if (input.length() == 2)
result = (convert[1] * digit2) + (convert[0] * digit1);
if (input.length() == 3)
result = (convert[2] * digit3) + (convert[1] * digit2) + (convert[0] * digit1);
if (input.length() == 4)
result = (convert[3] * digit4) + (convert[2] * digit3) + (convert[1] * digit2) + (convert[0] * digit1);
if (input.length() == 5)
result = (convert[4] * digit5) + (convert[3] * digit4) + (convert[2] * digit3) + (convert[1] * digit2) + (convert[0] * digit1);
if (input.length() == 6)
result = (convert[5] * digit6) + (convert[4] * digit5) + (convert[3] * digit4) + (convert[2] * digit3) + (convert[1] * digit2) + (convert[0] * digit1);
if (input.length() == 7)
result = (convert[6] * digit7) + (convert[5]) + (convert[4] * digit5) + (convert[3] * digit4) + (convert[2] * digit3) + (convert[1] * digit2) + (convert[0] * digit1);
if (input.length() == 8)
result = (convert[7] * digit8) + (convert[6] * digit7) + (convert[5]) + (convert[4] * digit5) + (convert[3] * digit4) + (convert[2] * digit3) + (convert[1] * digit2) + (convert[0] * digit1);
if (input.length() == 9)
result = (convert[8] * digit9) + (convert[7] * digit8) + (convert[6] * digit7) + (convert[5]) + (convert[4] * digit5) + (convert[3] * digit4) + (convert[2] * digit3) + (convert[1] * digit2) + (convert[0] * digit1);
if (input.length() == 10)
result = (convert[9] * digit10) + (convert[8] * digit9) + (convert[7] * digit8) + (convert[6] * digit7) + (convert[5]) + (convert[4] * digit5) + (convert[3] * digit4) + (convert[2] * digit3) + (convert[1] * digit2) + (convert[0] * digit1);
}