Hey I need help fixing this error. It says out of range. Any help wiil be highly
ID: 3751309 • Letter: H
Question
Hey I need help fixing this error. It says out of range. Any help wiil be highly appreciated.
C++ program to add hexadecimal numbers. Other functions cannot be added, and it SHOULD NOT t be changed into decimal, perform the addiditon and convert back to hexadecimal
#include <iostream>
#include <vector>
#include <string>
// NOTE: The ONLY files that should be #included for this assignment are iostream, vector, and string
// No other files should be #included
using namespace std;
string addhex(string, string);
int main()
{
cout << "hexadecimal A4 + A5 = " << addhex("A4", "A5") << endl; //you should get 149
cout << "hexadecimal 2B + C = " << addhex("2B", "C") << endl; //you should get 37
cout << "hexadecimal FABC + 789 = " << addhex("FABC", "789") << endl; //you should get 10245
cout << "hexadecimal FFFFFF + FF = " << addhex("FFFFFF", "FF") << endl << endl; //you should get 10000FE
system("PAUSE");
return 0;
}
string addhex(string hex1, string hex2)
{
string result;
int sum = 0;
bool carry = false;
while (hex1.size() != hex2.size())
{
if (hex1.size() > hex2.size())
{
hex2.insert(0, "0");
}
else
{
hex1.insert(0, "0");
}
}
for (int i = hex1.size(); i >= 0; i--)
{
if (hex1.at(i) >= 0 && hex1.at(i) < 10)
{
sum += hex1.at(i) - '0';
}
else
{
sum += hex1.at(i) - 'A' + 10;
}
if (hex2.at(i) >= 0 && hex2.at(i) < 10)
{
sum += hex2.at(i) - '0';
}
else
{
sum += hex2.at(i) - 'A' + 10;
}
sum += (1 * carry);
carry = (sum >= 16);
sum = sum % 16;
if (sum <= 9)
{
result += '0' + sum;
}
else
{
result += 'A' + (sum - 10);
}
}
carry ? result += '1' : result = result;
return string(result.rbegin(), result.rend());
}
Explanation / Answer
Given below is the fixed code for the question.
Please do rate the answer if it was helpful. Thank you
#include <iostream>
#include <vector>
#include <string>
// NOTE: The ONLY files that should be #included for this assignment are iostream, vector, and string
// No other files should be #included
using namespace std;
string addhex(string, string);
int main()
{
cout << "hexadecimal A4 + A5 = " << addhex("A4", "A5") << endl; //you should get 149
cout << "hexadecimal 2B + C = " << addhex("2B", "C") << endl; //you should get 37
cout << "hexadecimal FABC + 789 = " << addhex("FABC", "789") << endl; //you should get 10245
cout << "hexadecimal FFFFFF + FF = " << addhex("FFFFFF", "FF") << endl << endl; //you should get 10000FE
system("PAUSE");
return 0;
}
string addhex(string hex1, string hex2)
{
string result;
int sum = 0, carry = 0;
//pad the smaller string with leading 0s to make same size
while (hex1.size() != hex2.size())
{
if (hex1.size() > hex2.size())
{
hex2 = hex2.insert(0, "0");
}
else
{
hex1 = hex1.insert(0, "0");
}
}
for (int i = hex1.size() - 1; i >= 0; i--)
{
sum = 0;
//get the digit from first number
if (hex1[i] >= '0' && hex1[i] <= '9')
{
sum += hex1[i] - '0';
}
else
{
sum += hex1[i] - 'A' + 10;
}
//get the digit from second number
if (hex2.at(i) >= '0' && hex2[i] <= '9')
{
sum += hex2[i] - '0';
}
else
{
sum += hex2[i] - 'A' + 10;
}
//add carry
sum += carry;
//adjust carry and sum, if sum exceeds 16, otherwise carry is 0
if(sum >= 16){
carry = 1;
sum = sum % 16;
}
else
carry = 0;
//convert to hex and store in result
if (sum <= 9)
{
result += ('0' + sum);
}
else
{
result += ('A' + (sum - 10));
}
}
//if any carry is still generated, overflow
if(carry == 1)
result += '1';
return string(result.rbegin(), result.rend());
}
output
------
hexadecimal A4 + A5 = 149
hexadecimal 2B + C = 37
hexadecimal FABC + 789 = 10245
hexadecimal FFFFFF + FF = 10000FE