Hello Eto,
PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.
Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
|
I have been working with your program for awhile and found it does work, but needs a couple of tweaks.
You have two if statements with the format of:
if ('0' <= hStr[i] <= '9')
. This may look good on paper, but does not translate into code that works. For this I have most often seen and used:
if ('0' >= hStr[i] || hStr[i] <= '9')
. Notice the lhs of the (||) is (>=) for both. Since you are looking for something between (0) and (9) or (A) and (F). As an example anything < "A", i.e., between 33 and 64 dec. would be considered true. Then when you try to convert that into a decimal number it would be a problem.
In the function "getDecimalForm" I found that the two if statements worked better as an if/else if statement.
After you define your variables, in main, you might want to consider something like this:
1 2 3 4 5 6 7 8
|
cout << "\n Input 8 hex numbers (12A45F78): ";
while (cin >> hexadecimal && hexadecimal.size() != 8)
{
std::cout << "\n Invalid hex number!\n Must be 8 characters." << std::endl;
std::cout << "\n Input 8 hex numbers (12A45F78): ";
}
|
The "cout" statements can be changed to anything that you like.
What you have set up now is a great way of testing the program, but eventually you will need to input and verify the string. You may also want to verify that each element of the string is a digit or the letters "A" - "F". I have not worked on this yet, but what I have in mind may streach the "Do not call built-in library functions that accomplish these tasks automatically.", but since it is not part of converting the number it might work. I was thinking of using the header file "<cctype>" and the function "isdigit()". There is also "std::toupper()" to make sure that the letters are in upper case. Just a thought for now.
Hope that helps,
Andy