-1

I need to write a program that reads in two hexadecimal numbers, converts them to decimal form, and prints out the sum of the two numbers in decimal. This is as far as I've gotten, I can't seem to get the right values to add up.

 #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;
}
David Barrios
  • 13
  • 1
  • 3
  • std::cin >> std::hex >> a >> b; std::cout << std::hex << a << ", " << b << '\n'; –  Oct 06 '14 at 19:22
  • See also this stack overflow about cout and hex and decimal conversion http://stackoverflow.com/questions/15892877/cout-print-hex-instead-of-decimal and this stack overflow about ignore http://stackoverflow.com/questions/5131647/why-would-we-call-cin-clear-and-cin-ignore-after-reading-input – Richard Chambers Oct 06 '14 at 19:24

2 Answers2

0

try this :

   int main(){

    int hex1;
    int hex2;
    cout << " Please enter a hexadecimal number: ";
    cin>>hex>>hex1;
    cout << " Please enter another hexadecimal value: ";
    cin>>hex>>hex2;
    cout<<"The decimal sum of  "<<hex1<<" and  "<<hex2<<" = "<<(hex1+hex2) ;
    return 0;
}
Rustam
  • 6,485
  • 1
  • 25
  • 25
  • Thanks Rustam, but not exactly what I need. The program basically needs to reads in two sets of hexadecimal values (e.i 45AF,12B3) and convert them to decimal form then add those values and print the result. – David Barrios Oct 06 '14 at 20:02
  • Then remove 'hex' from 'cout' line and check – Rustam Oct 06 '14 at 20:08
-1

If you have to write your own algorithm to convert from hex to decimal and vice versa, then I would recommend you use the following rough template:

#include <iostream>
#include <string>

using namespace std;

int hexToDecimal(string);
string decimalToHex(int);

int main()
{
    cout << "Enter a hex number: ";
    string hex1;
    cin >> hex1;

    cout << "Enter a hex number: ";
    string hex2;
    cin >> hex2;

    cout << "The sum of the numbers in hex is: ";
    cout << decimalToHex(hexToDecimal(hex1) + hexToDecimal(hex2));

    return 0;
}

int hexToDecimal(string hex)
{
    // Code here.
}

string decimalToHex(int decimal)
{
    // Code here.
}
wjmolina
  • 2,625
  • 2
  • 26
  • 35
  • Does it matter if the conversion functions return any values? for instance, can they be called as void in the parameter list instead? – David Barrios Oct 06 '14 at 19:57
  • You can make the conversion functions return nothing (void) by passing references instead, but why would you want to do that? – wjmolina Oct 06 '14 at 22:40