Large Negative number or zero output on multiplication

Im creating a hexadecimal calculator (only does addition of two numbers) for a class and am running into an issue on this program that otherwise works. I have it set up so that it multiplies a digit (translated from character form) the user inputs by a multiple of 16 based on where it is in the input set of numerals. After about 8 it gives me a large negative number and after several more just a zero, Id really appreciate some help here as im panicking last minute since I thought I had the coding wrapped up. Also ignore anything related to teh function back conversion, it takes the sum and converts it back to hex, but I have no issues with that and especially not related to this, it simply doesnt return anything when I have this issue, which makes sense.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

int conversion(char num[128]);
void backconversion(char hex[128], int sum);

int _tmain(int argc, _TCHAR* argv[])
{
	bool loop = true;
	char num1[128];
	char num2[128];
	cout << "Enter two hexadecimal numerals to find the sum of: " << endl;
	while (loop == true)
	{
		char exit; //User inputs value of this to exit or not
		char hexnum[128];//Hex Numeral input by user
		int value1 = 0;
		int value2 = 0;
		int sum;
		int limit = 0;
		do
		{
			if (value1 == -1) //Function has detected non hex char
			{
				cout << "That is an invalid hexadecimal character, retry:" << endl;
			}
			cout << "1: ";
			cin >> num1;
			value1 = conversion(num1); //Conversion call
		} while (value1 == -1);
		cout << "+" << endl;
		do
		{
			if (value2 == -1) //Function has detected non hex char
			{
				cout << "That is an invalid hexadecimal character, retry:" << endl;
			}
			cout << "2: ";
			cin >> num2;
			value2 = conversion(num2); //Conversion Call
		} while (value2 == -1);
		sum = value1 + value2;
		// Counts digit in sum
		int count = 0;
		while (sum != 0) {
			count++;
			sum /= 10;
		}
		// If digits in sum are less than 128
		if (count < 129)
		{
			sum = value1 + value2;
			backconversion(hexnum, sum);
			cout << value1 << " + " << value2 << " = " << sum << "\t" << "Sum in Hex: ( ";
			while (static_cast<int>(hexnum[limit]) != -52 && static_cast<int>(hexnum[limit]) != 0)
			{
				cout << hexnum[limit];
				limit++;
			}
			cout << " )" << endl;
		}
		else
		{
			cout << "Addition Overflow" << endl;
		}
		// Wipes sum to hex conversion array
		for (int i = 0; i < limit; i++)
		{
			hexnum[i] = 0;
		}
		cout << "Would you like to exit (y/n): ";
		cin >> exit;
		if (exit == 'y')
		{
			loop = false;
		}
	}
	return 0;
}


int conversion(char num[128])
{
	int key[16] = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70 }; //Key to reference to
	int alphabetic; //The result of IsAlpha for comparison
	int pool = 0; //Result of conversion
	int characters = 0; //Used to find number of useable characters in array
	int power; //Used to determine what multiple of 16 the number is a multiple of
	int count = 0; //Constrains for loops, basically the static version of characters
	int valid = 0; //Used to check determine if valid hex
	//Conversion for number
	while (static_cast<int>(num[count]) != -52 && static_cast<int>(num[count]) != 0)
	{
		characters++;
		count++;
	}
	for (int i = 0; i < count; i++)
	{
		power = 1;
		//Checks to see if char is alpha and changes to uppercase for recognition if so
		if (static_cast<int>(num[i]) != -52)
		{
			alphabetic = isalpha(num[i]);
			if (alphabetic != 0)
			{
				num[i] = toupper(num[i]);
			}
		}
		else
		{
			break;
		}
		//Compares character ASCII values to the ones defined in the array to see if any match and adds corresponding number to sum
		for (int a = 0; a < 16; a++)
		{
			if (static_cast<int>(num[i]) == key[a])
			{
				valid++;
				//Calculates what the value of the number's place is and adds to relative pool
				if (characters - 1 != 0)
				{
					power = 16;
				}
				for (int p = 1; p < characters - 1; p++)
				{
					power *= 16;
				}
				pool += (a * power);
				characters--;
				break;
			}
		}
	}
	// If valid is equal to characters in char array
	if (valid != count)
	{
		return -1;
	}
		return pool;
}
1
2
3
4
5
6
7
8
#include <iostream>
#include <limits>

int main()
{
    std::cout << std::numeric_limits<int>::max() << '\n';
    std::cout << std::hex << std::numeric_limits<int>::max() << '\n';
}
Topic archived. No new replies allowed.