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
/else
and complain if none of the tests match.note also that the expression for
sum
is incorrect too.sum += currentDigitInt * pow(16, i);
should be;sum = sum * 16 + currentDigitInt;
the
printf
format forunsigned long
is%lu
, not%u
.you should test the return value of
scanf_s
to properly detect a potential end of file.scanf_s
expects 2 extra arguments for conversion specifier%c
and 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
0
through9
are consecutive but purists will argue that the lettersa
throughf
andA
throughF
might 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 betweeni
andj
and betweenr
ands
in 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.