0

I'm writing a program that takes two hexadecimal numbers and converts them to decimal form, and prints out their sum in decimal form. The maximum length of the numbers is 10. {submit.cs.ucsb.edu/submission/203504}. I feel confuse about the error messages. The problem wants the max length of the numbers is 10. Why the output like "ffffffffff" works

#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;
}
Darren Pan
  • 49
  • 2
  • 10
  • Number is a number. Hex or base 10 is just a way to represent it. For me the operation decimalToHex have no sense. Take a look to http://stackoverflow.com/questions/1070497/c-convert-hex-string-to-signed-integer . And finally print it in hex format std::cout << std::hex << decimal << end and remember. Number are numbers. Hex or decimal is just the way to present it. – Mquinteiro Jul 23 '16 at 18:30
  • Spend some time testing your code and Googling. – Jithin Pavithran Jul 23 '16 at 18:31
  • @Mquinteiro: If I want to set the maximum length of the number is 10, then I use an if statement, but I just get partial credit. Can you help me to take a look for it? submit.cs.ucsb.edu/submission/203493 – Darren Pan Jul 23 '16 at 19:31

1 Answers1

5

There is a much simpler way to do this:

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;
DimChtz
  • 4,043
  • 2
  • 21
  • 39
  • If I want to set the maximum length of the number is 10, then I use an if statement, but I just get partial credit. Can you help me to take a look for it? https://submit.cs.ucsb.edu/submission/203493 – Darren Pan Jul 23 '16 at 19:26
  • In that case, you need to find the length of the number. And this is another question – DimChtz Jul 23 '16 at 19:32
  • Or you can `if (hex1 > 9999999999 || hex2 > 9999999999) { /* length > 10, do something*/ }`, since you only care about length 10. – DimChtz Jul 23 '16 at 19:37
  • I try to use hex1=1, hex2=aaaaaaaaaaa; the expect output is additional overflow, but the output I get is 80000000. – Darren Pan Jul 23 '16 at 19:46
  • Okay, try to change `int hex1, hex2;` to `long long hex1, hex2;` – DimChtz Jul 23 '16 at 20:26
  • {https://submit.cs.ucsb.edu/submission/203504}. I feel confuse about the error messages. The problem wants the max length of the numbers is 10. Why the output like "ffffffffff" works? – Darren Pan Jul 23 '16 at 22:35