【发布时间】:2014-10-06 19:13:29
【问题描述】:
我需要编写一个程序来读取两个十六进制数字,将它们转换为十进制形式,并以十进制打印出两个数字的总和。据我所知,我似乎无法获得正确的值来加起来。
#include <iostream>
#include <iomanip>
using namespace std;
void Hex_to_Dec(char &, char &);
int main()
{
char hex1;
char hex2;
cout << " Please enter a hexadecimal number: " << endl;
cin >> hex1;
cout << " Please enter another hexadecimal value: " << endl;
cin >> hex2;
Hex_to_Dec(hex1, hex2);
cout << "The decimal sum of" << hex1 << " and " << hex2 << " is " << hex1 + hex2 << endl;
return 0;
}
void Hex_to_Dec(char & hex1, char & hex2)
{
std::cin >> std::hex >> hex1;
std::cout << hex1 << std::endl;
std::cin >> std::hex >> hex2;
std::cout << hex2 << std::endl;
}
【问题讨论】:
-
std::cin >> std::hex >> a >> b; std::cout
-
另请参阅有关 cout 和十六进制和十进制转换的堆栈溢出 stackoverflow.com/questions/15892877/… 以及有关忽略 stackoverflow.com/questions/5131647/… 的堆栈溢出