Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / C program question

Author
Message
DK_
20
Years of Service
User Offline
Joined: 4th Jan 2004
Location:
Posted: 24th Mar 2004 04:00
I know C++ very well, but then I decided to try and learn a little C since it is compatible with Unix. I tried to write a fairly simple program that will convert the number you enter into Hexadecimal. Here is my code:



Output:
Type in a number to be converted:
7
The value 31 converted to Hex is: 37


Or something like that, the numbers are completely diff. then what they should be.
the last output line should say:
"The value 7 converted to Hex is: 7"

Please let me know what is wrong with my code.

TheAbomb12
21
Years of Service
User Offline
Joined: 14th Aug 2003
Location: Amist the blue skies...
Posted: 24th Mar 2004 07:36
you where using "getc()" which mean get char...

ch is an int not a char.

try this instead.



you might have noticed I used scanf(), it works in a similar manner as printf, except you need a '&' right before the variable.

Amist the Blue Skies...
Elleomea
21
Years of Service
User Offline
Joined: 2nd Aug 2003
Location: England
Posted: 24th Mar 2004 11:01
Alternatively you can convert char to int with atoi() (standard function, think it's in stdlib)

How much ham could a hamster stir, if a hamster could stir ham?
TheAbomb12
21
Years of Service
User Offline
Joined: 14th Aug 2003
Location: Amist the blue skies...
Posted: 24th Mar 2004 17:59 Edited at: 24th Mar 2004 17:59
Quote: "atoi() "


I had an annoying experience with this function.

If I remember right, you have to have the variable be a single dimension array with an index size of 1.

Amist the Blue Skies...
TheAbomb12
21
Years of Service
User Offline
Joined: 14th Aug 2003
Location: Amist the blue skies...
Posted: 24th Mar 2004 18:50 Edited at: 24th Mar 2004 19:20
I just made an easier function then atoi...



All you need to do is enter to character value of a number. If it is not a number a -1 is returned.

If you need a float value instead of an int, change all the ints into floats.

[edit]crap, I just noticed you can only return a single digit[/edit]

Amist the Blue Skies...
DK_
20
Years of Service
User Offline
Joined: 4th Jan 2004
Location:
Posted: 25th Mar 2004 01:55
Thanx that help.

I just noticed something really neat though, C and C++ compilers are the same. If you have a C compiler you have a C++ compiler and vice versa. It just depends on the libraries your compiler comes with, if you have a C compiler and want C++ you don't have to buy another one, all you have to do is download the file "iostream.h" and if you want to go from C++ to C get the file "stdio.h".

I figured this out by having those two files and then running the following program, (which actually works!):



I thought that that was interesting at least.

Elleomea
21
Years of Service
User Offline
Joined: 2nd Aug 2003
Location: England
Posted: 25th Mar 2004 03:16
C++ compilers can compile C programs, C compilers can't compile C++ prorgams, since C has no support for the object orientated paradigm (that's the whole reason for C++). And err, you never have to buy a C++ compiler, one of the best C++ compilers is G++, the GNU C++ compiler which is Free (as in Freedom and as in beer).

How much ham could a hamster stir, if a hamster could stir ham?

Login to post a reply

Server time is: 2024-09-21 13:36:48
Your offset time is: 2024-09-21 13:36:48