我正在编写一个程序,它接受两个十六进制数字并将它们转换为十进制形式,并以十进制形式打印出它们的总和。数字的最大长度为10. {submit.cs.ucsb.edu/submission/203504}。我对错误消息感到困惑。问题是数字的最大长度是10.为什么输出像“ffffffffff”一样
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
int hexToDecimal(string);
string decimalToHex(int);
int main()
{
long long hex1, hex2;
std::cout << "Enter first number:" << std::endl;
std::cin >> std::hex >> hex1;
std::cout << "Enter a second number:" << std::endl;
std::cin >> std::hex >> hex2;
if (hex1 >9999999999 || hex2 > 9999999999)
{
cout << "Addition Overflow" << endl;
}
else
{
std::cout << "The sum is "<< std::hex << hex1 + hex2 << "." << std::endl;
}
return 0;
}
答案 0 :(得分:5)
有一种更简单的方法可以做到这一点:
int hex1, hex2;
std::cout << "Enter first hex number:" << std::endl;
std::cin >> std::hex >> hex1;
std::cout << "Enter a second hex number:" << std::endl;
std::cin >> std::hex >> hex2;
std::cout << std::hex << hex1 + hex2 << std::endl;