patterncppMinor
Hexadecimal number to decimal conversion
Viewed 0 times
conversionnumberhexadecimaldecimal
Problem
I am a semi-beginner in C++ and I have a project in C++ to convert a hexadecimal integer into decimal equivalent, but I extended it and now this program can also convert a fractional part.
Logic I have used:
I have knowledge on selection statements (
Note: This program is compiled with MinGW and CodeBlocks. My school teacher uses TurboC++.
Please provide advice on things which I can understand; don't get too advanced. I want to score full marks on my school project but also want to understand the code.
Logic I have used:
- First the whole program is inside a
do-whileloop so that the user can convert a number as many time as desired.
- Another
do-whileloop checks if the inputted number is valid or invalid. If invalid, it again asks the user for input.
- A
forloop counts the number of characters before the decimal point (if any).
- Another
forloop converts the characters before the decimal point into a decimal number.
- Another
forloop converts the characters after the decimal point into a decimal fraction and adds it to the original number.
- It prints it and asks if the user wants to again run the loop.
I have knowledge on selection statements (
if-else), loops (for, while, do-while), operators and other basic things in C++.Note: This program is compiled with MinGW and CodeBlocks. My school teacher uses TurboC++.
Please provide advice on things which I can understand; don't get too advanced. I want to score full marks on my school project but also want to understand the code.
// This is a C++ project for Unit Test 2 (2015-16). This program can convert a hexadecimal number into decimal equivalent.
#include
#include
#include
using namespace std;
int main()
{
char choice;
cout>hexd;
length=strlen(hexd);
for(int i=0;i=48 and hexd[i]=65 and hexd[i]=97 and hexd[i]=48 and hexd[i]=65 and hexd[i]=97 and hexd[i]=48 and hexd[i]=65 and hexd[i]=97 and hexd[i]>choice;
}while(choice=='Y' or choice=='y');
cout<<"\n\n **** THANK YOU FOR USING THIS SOFTWARE ****";
return 0;
}Solution
To take a quick first stab at some style issues:
-
Follow a consistent indentation style. This makes the code extremely readable, and you will thank yourself later when you come back to your code.
For example:
-
Try to break down your program into smaller code blocks that you can reason about them independently, and perhaps even test them independently.
For example, how about start with this template, and fill in the code:
-
Use
-
Your way of checking if the input string is hexadecimal is very awkward and unreadable. Whenever you use constants, you should declare them as consts, so you can update them without having to change the code.
A hexadecimal string has characters from one of the following set of characters:
If lambdas are not your style, you can also use the new c++11 loop style, which is much more readable.
To Be Continued..
-
Follow a consistent indentation style. This makes the code extremely readable, and you will thank yourself later when you come back to your code.
For example:
void foo()
{
do
{
bar();
} while(0);
}
int main()
{
foo();
}-
Try to break down your program into smaller code blocks that you can reason about them independently, and perhaps even test them independently.
For example, how about start with this template, and fill in the code:
// checks
bool isValid(const std::string &hexInput)
{
}
int64_t convertHexToDecimal(const std::string& hexString)
{
}
int main()
{
std::string input;
std::cin >> input;
while(isValid(input))
{
// perform the conversion
int64_t convertedDecimal = convertHexToDecimal(input);
// output the conversion
std::cout > input;
}
}-
Use
std::string liberally instead of a character array. I think this is a more c++11 style of going about it. This will allow you to utilize the wealth of STL. This is more recognizable, and reduces the changes of introducing bugs in your hand-rolled while/for loop. auto found = input.find('.');
if (found != std::string::npos)
{
std::cout << "Period found at: " << found << '\n';
}-
Your way of checking if the input string is hexadecimal is very awkward and unreadable. Whenever you use constants, you should declare them as consts, so you can update them without having to change the code.
A hexadecimal string has characters from one of the following set of characters:
0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F and the ., also the . should appear exactly once, and should probably not be the last character of the string. Also, you can use the characters directly for the comparison instead of the ASCII values. bool isValidHex = true;
// for_each will iterate between the the provided iterators
// for each position, you can pass a lambda function that checks
// the characters
std::for_each(
str.begin(),
str.end(),
[&isValidHex](char c)
{
// std::cout = 'a' && c = 'A' && c = '0' && c <= '9'))
{
// this is a valid hex.
}
else
{
isValidHex = false;
}
});If lambdas are not your style, you can also use the new c++11 loop style, which is much more readable.
for (char c : input)
{
// perform checks here
}To Be Continued..
Code Snippets
void foo()
{
do
{
bar();
} while(0);
}
int main()
{
foo();
}// checks
bool isValid(const std::string &hexInput)
{
}
int64_t convertHexToDecimal(const std::string& hexString)
{
}
int main()
{
std::string input;
std::cin >> input;
while(isValid(input))
{
// perform the conversion
int64_t convertedDecimal = convertHexToDecimal(input);
// output the conversion
std::cout << "..." << convertedDecimal << std::endl;
// get new input
std::cin >> input;
}
}auto found = input.find('.');
if (found != std::string::npos)
{
std::cout << "Period found at: " << found << '\n';
}bool isValidHex = true;
// for_each will iterate between the the provided iterators
// for each position, you can pass a lambda function that checks
// the characters
std::for_each(
str.begin(),
str.end(),
[&isValidHex](char c)
{
// std::cout << "Checking" << c << std::endl;
if ((c >= 'a' && c <= 'f') ||
(c >= 'A' && c <= 'F') ||
(c >= '0' && c <= '9'))
{
// this is a valid hex.
}
else
{
isValidHex = false;
}
});for (char c : input)
{
// perform checks here
}Context
StackExchange Code Review Q#107186, answer score: 6
Revisions (0)
No revisions yet.