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

}

http://www.cplusplus.com/reference/clibrary/cmath/pow/

There is no pow(int, int) in cmath. You can either write your own or typecast an int to a double and use one of the ones provided by cmath.

double pow ( double base, double exponent );
long double pow ( long double base, long double exponent );
float pow ( float base, float exponent );
double pow ( double base, int exponent );
long double pow ( long double base, int exponent );

Raise to power
Returns base raised to the power exponent:

baseexponent
In C++, this function is overloaded in <complex> and <valarray> (see complex pow and valarray pow).

Parameters

base
Floating point value.
exponent
Floating point value.

He is right, there is no pow( int, int ), but maybe(slight maybe) you won't need that at all. There is a shortcut, you can change the base field format of the stream, so you can work on your hexadecimal numbers just like regular numbers. After all, everything in the memory is stored in a hexadecimal format, the rest is just representation.

#include <iostream>
#include <sstream>

int main ()
{
    long number, square;

    std::string number_as_string;
    std::stringstream ss;

    std::cout << "Hexadecimal: ";
    std::cin >> number_as_string;

    ss << std::hex << number_as_string;
    ss >> number;

    square = number * number;

    std::cout << "decimal: " << std::dec << number << std::endl;
    std::cout << "square(decimal): " << square << std::endl;
    std::cout << "square(hex): " << std::hex << square << std::endl;

    return 0;
}

I came up with this to demonstrate what I am thinking of. The program gives you back the square of a given hexadecimal number. I don't know if you can use any of it, but it might give you a different angel to approach the problem.

Edited 4 Years Ago by LRRR: n/a

This article has been dead for over six months. Start a new discussion instead.