Problem in 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;
}
 
C

CBFalconer

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.

I.e. you are specifying an octal number. Octal numbers terminate
on anything other than '0'..'7'.
 
B

Barry Schwarz

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();

No such standard function. Even if there was, who gave you permission
to remove other data from my screen?
printf("Enter hexadecimal string\n");
while((c=getchar())!=EOF){
if(c=='0'){
if(i==0)
if(((c=getchar())=='x')||((c=getchar())=='X'))

You don't want to call getchar twice. You know that the number begins
with a '0' so get the very next char and test it twice:
if (((c = getchar(x)) == 'x') || (c == 'X'))

Notice that horizontal white space makes the code easier to read.

With your code, if you entered 09, the first call to getchar picked up
the 9 and the second picked up something else (\n, EOF, ??).
continue;
}
i++;
if(c>=48&&c<=57)

This only works for ASCII and is needlessly obfuscated. Why not use
if (c >= '0' && c <= '9')
and be both clearer and portable. You might also want to check on the
isdigit function.
n=16*n+(c-48);

In the same vein, c-'0' is the portable way.
else if(c>=65&&c<=70)

Check out the isxdigit function.
n=16*n+(c-65)+10;
c-'A'

else if(c>=97&&c<=102)
n=16*n+(c-97)+10;

c-'a'

But both of these suffer from the fact that the letters are not
required to be contiguous the way the digits are.
}
printf("Corresponding integer value is: %d",n);
getch();

No such standard function.
return 0;
}


Remove del for email
 

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,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top