Issue
I wrote a program that does conversion from hex to decimal. all I got left is to check if the char is between a-f or A-F, maybe 0-9 as well. if it is not between them it will print "Illegal input".
My code:
int n, i;
char currentDigit;
unsigned long int sum, currentDigitInt;
printf("Enter the number of digits in the Hexadecimal number:");
scanf_s("%d", &n);
sum = 0;
printf("Enter the Hexadecimal number:\n");
for (i = n - 1; i >= 0; i--) {
scanf_s(" %c", ¤tDigit);
if (currentDigit >= 'a') {
currentDigitInt = (currentDigit - 'a') + 10;
}
else if (currentDigit >= 'A') {
currentDigitInt = (currentDigit - 'A') + 10;
}
else
currentDigitInt = currentDigit - '0';
sum += currentDigitInt * pow(16, i);
}
printf("The decimal number is: %u", sum);
The output I need:
Enter the number of digits in the Hexadecimal number: 2 Enter the Hexadecimal number: QQ Illegal input
Solution
There are multiple problems in your code:
you can test character ranges with the
&&operator. For example:if (currentDigit >= 'a' && currentDigit <= 'f')you combine these tests in a series of
if/elseand complain if none of the tests match.note also that the expression for
sumis incorrect too.sum += currentDigitInt * pow(16, i);should be;sum = sum * 16 + currentDigitInt;the
printfformat forunsigned longis%lu, not%u.you should test the return value of
scanf_sto properly detect a potential end of file.scanf_sexpects 2 extra arguments for conversion specifier%cand may not be available on all systems.note too that you do not need to ask for the number of hex digits, just break from the loop when the character entered is a newline for the second time.
Here is a corrected version:
#include <stdio.h>
int main() {
int currentDigit;
unsigned long int sum;
int invalid = 0;
printf("Enter the Hexadecimal number: ");
sum = 0;
while ((currentDigit = getchar()) ! EOF && currentDigit != '\n') {
int currentDigitInt;
if (currentDigit >= 'a' && currentDigit <= 'f') {
currentDigitInt = (currentDigit - 'a') + 10;
} else
if (currentDigit >= 'A' && currentDigit <= 'F') {
currentDigitInt = (currentDigit - 'A') + 10;
} else
if (currentDigit >= '0' && currentDigit <= '9') {
currentDigitInt = currentDigit - '0';
} else {
invalid = 1;
continue; // keep scanning the input until end of line
}
sum = sum * 16 + currentDigitInt;
}
if (invalid) {
printf("Invalid input\n");
} else {
printf("The decimal number is: %lu\n", sum);
}
return 0;
}
Notes:
- The C standard does guarantee that
0through9are consecutive but purists will argue that the lettersathroughfandAthroughFmight not be consecutive the execution character set. While the are correct, obscuring newbie programmers with these considerations is counter productive and quite excessive as these character ranges are consecutive in both ASCII and EBCDIC (the gaps in EBCDIC are betweeniandjand betweenrandsin both cases).
Answered By - chqrlie Answer Checked By - Marilyn (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.