Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

halp

Name: Anonymous 2008-03-18 20:07

Just trying to figure out why this doesn't work, don't tell me to do it "this way", I'm not interested. 

//dectohex.c
//17/03/08 16:48

#include <stdio.h>

int main()
{
    printf("Input a whole number: ");
    int num;
    num=getchar();
    printf("The decimal number %d converted to hexidecimal is %x!", num, num);
    return 0;
}

============
outputs:
C:\hymn-c>dectohex
Input a whole number: 15
The decimal number 49 converted to hexidecimal is 31!
C:\hymn-c>

The only thing I can think of is that getchar() is incompatible with numerical input?  I dunno...

Name: Anonymous 2008-03-18 20:11

getchar != parse_line_and_convert_to_integer

Name: Anonymous 2008-03-18 20:12

getchar getchar = new getchar();

Name: Anonymous 2008-03-18 20:17

throw new System.IO.GetCharException("Youre a dumbass");

Name: Anonymous 2008-03-18 20:54

Read SICP

Name: Anonymous 2008-03-18 20:57

>>5
Does that have anything to do with the topic or is it just your way of saying, "I've read SICP"?

Name: Anonymous 2008-03-18 21:00

getchar() is receiving not 15, but 1. In the printf() you`re just outputting 1 converted to ascii in decimal, try changing %d to %c.

Name: Anonymous 2008-03-18 21:33

>>7
Thanks, now it makes a bit more sense. 

Name: Anonymous 2008-03-18 22:31

>>8

PROTIP: scanf.

getchar() is doin' it wrong.

Name: Anonymous 2008-03-19 0:02

GOOD FREAKING FUCK, I'm sick of this shit on /prog/.  Thank you for chasing off all the EXPERT PROGRAMMERS.

Name: Anonymous 2008-03-19 0:16

>>10
You have been caught being trolled.

Name: Anonymous 2008-03-19 3:21

main(){int a=0,c;while((c=getchar())!=EOF&&c>='0'&&c<='9')a=a*10+c-'0';printf("%x\n",a);}

Name: Anonymous 2009-08-16 22:46

Lain.

Name: ​​​​​​​​​​ 2010-10-22 4:45

Name: Anonymous 2011-02-03 5:20

Name: Anonymous 2011-02-04 18:11

Don't change these.
Name: Email:
Entire Thread Thread List