Some logic error in my C code

R

Raj

Following is a code to print integer equivalent of a hexadecimal
number. If the entered number starts with a 0x or a 0X, calculation is
done after skipping these two characters. Now, this code works fine
except when i enter 0 as the 1st element & then follow it up with a
non 'x' or non 'X' character.
For e.g. When i enter 09, i get output 0... not only this, if i start
my input with a 0, then output always comes out to be 0. Otherwise the
code works fine. If anybody can help... thanx!

Code:

#include<stdio.h>
int main(void)
{
int n=0,i=0,c;
clrscr();
printf("Enter hexadecimal string\n");
while((c=getchar())!=EOF){
if(c=='0'){
if(i==0)
if(((c=getchar())=='x')||((c=getchar())=='X'))
continue;
}
i++;
if(c>=48&&c<=57)
n=16*n+(c-48);
else if(c>=65&&c<=70)
n=16*n+(c-65)+10;
else if(c>=97&&c<=102)
n=16*n+(c-97)+10;
}
printf("Corresponding integer value is: %d",n);
getch();
return 0;
}
 
M

Malcolm McLean

Raj said:
Following is a code to print integer equivalent of a hexadecimal
number. If the entered number starts with a 0x or a 0X, calculation is
done after skipping these two characters. Now, this code works fine
except when i enter 0 as the 1st element & then follow it up with a
non 'x' or non 'X' character.
For e.g. When i enter 09, i get output 0... not only this, if i start
my input with a 0, then output always comes out to be 0. Otherwise the
code works fine. If anybody can help... thanx!

Code:

#include<stdio.h>
int main(void)
{
int n=0,i=0,c;
clrscr();
printf("Enter hexadecimal string\n");
while((c=getchar())!=EOF){
if(c=='0'){
if(i==0)
if(((c=getchar())=='x')||((c=getchar())=='X'))
You've fallen victim to intelligent or guarding.
If c == 'x', the program knows that it must execute the "continue". So the
second half of the expression is never executed. If c does not equal x,
getchar() is aclled for a second time.
continue;
}
i++;
if(c>=48&&c<=57)
n=16*n+(c-48);
else if(c>=65&&c<=70)
n=16*n+(c-65)+10;
else if(c>=97&&c<=102)
n=16*n+(c-97)+10;
}
printf("Corresponding integer value is: %d",n);
getch();
return 0;
}
Replace all this logic with a call to isxdigit() to make sure you input is
valid
and wrap it up in a function

int xdigittoval(char ch)
{
static char *digits = "0123456789abcdef";
ch = tolower(ch);
assert(isxdigit((unsigned char) ch));
return strchr(digits, ch) - digits;
}

I apologise for the cast to unsigned char. Just a quirk of the standard. It
should never trigger anyway, but belt and braces is a good policy. Now you
don't need those error-prone and possibly non-portable ASCII values.
 
R

Raj

You've fallen victim to intelligent or guarding.
If c == 'x', the program knows that it must execute the "continue". So the
second half of the expression is never executed. If c does not equal x,
getchar() is aclled for a second time.








Replace all this logic with a call to isxdigit() to make sure you input is
valid
and wrap it up in a function

int xdigittoval(char ch)
{
static char *digits = "0123456789abcdef";
ch = tolower(ch);
assert(isxdigit((unsigned char) ch));
return strchr(digits, ch) - digits;

}

I apologise for the cast to unsigned char. Just a quirk of the standard. It
should never trigger anyway, but belt and braces is a good policy. Now you
don't need those error-prone and possibly non-portable ASCII values.

--
Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

Corrected the error...Thanx so much!!
 
C

Chris Dollin

Raj said:
Following is a code to print integer equivalent of a hexadecimal
number.

Why aren't you using `strtol` after reading the characters in?
If the entered number starts with a 0x or a 0X, calculation is
done after skipping these two characters. Now, this code works fine
except when i enter 0 as the 1st element & then follow it up with a
non 'x' or non 'X' character.
For e.g. When i enter 09, i get output 0... not only this, if i start
my input with a 0, then output always comes out to be 0. Otherwise the
code works fine. If anybody can help... thanx!

Code:

#include<stdio.h>
int main(void)
{
int n=0,i=0,c;
clrscr();

Non-Standard, undeclared, and unnecessary. Bin it.
printf("Enter hexadecimal string\n");
while((c=getchar())!=EOF){
if(c=='0'){
if(i==0)
if(((c=getchar())=='x')||((c=getchar())=='X'))

If the next character isn't 'x', it will compare the
/second next/ character with 'X'. So if the first
character is 'X', it will read another character to
see if it's 'x'.
continue;
}
i++;

What's `i` for? Nothing. Bin it.
if(c>=48&&c<=57)

You must be out of your mind. What are `48` and `57`,
n=16*n+(c-48);
else if(c>=65&&c<=70)

`65` and `70`,
n=16*n+(c-65)+10;
else if(c>=97&&c<=102)

`97` and `102` supposed to be?

If you're going to assume the character set is ascii, then
at least use character constants and comment your assumptions.
If not, use a portable and straightforward way -- I can think
of two while I'm still typing -- to translate a hex character
into the corresponding hex value.
 
K

Keith Thompson

Malcolm McLean said:
assert(isxdigit((unsigned char) ch)); [...]

I apologise for the cast to unsigned char. Just a quirk of the
standard. It should never trigger anyway, but belt and braces is a
good policy. Now you don't need those error-prone and possibly
non-portable ASCII values.

Good grief, don't apologize for writing correct code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top