Hi remember me? I posted here a few months ago with a problem using a stack. Well now Iím using the same stack code again but trying to make the stack with pointers (yay Iím learning pointers!). Anyway I have been playing with this code for a long time and I am nearly insane because it is not working and I cant find out where the problem is. I know it is probably a stupid and small mistake of mine but I cant find it.
When this program is run if the input is 123 the output should be 321
Iím sure that this code is very easy to understand for anyone who knows C better than me :) so please tell me what is wrong with my code before I go crazy.
Thankyou.

I don't understand.
If getchar returns an int, and I want it to be an int, and the person inters an int then why do I need to check if it is an int and convert it to an int?

Can you please show me exactly how to do it?

I have been looking at this code for so long and I just cant find the problem :(

One reason I don't understand why I need to check if it is an int is because I have done similar programs before and it works like this..... I am sure i'm missing something so please show me what it is as I have gone a complete blank :(

Thankyou

07-25-2002

Hammer

When the user enters this
>123<enter>
the first character you program will see is the 1. Even though it's a 1 as far as the users see, the program will see it as it's ASCII equivalent (I assume your on a PC/Unix enviroment).

Look at the ASCII chart and you'll see that 1 is actually 0x31.

So, you must convert the character '1' (as entered by the user) to an int 1, to give you what you want. Do this by using the code I gave in my last post (there are other ways too, but this one is simplest for converting single characters, imo).

07-25-2002

Unregistered

So I need to right isdigit()?
I hope I get this done ok... i'll see what I can do and post what happens.
Thankyou

07-25-2002

Hammer

Quote:

Originally posted by Unregistered So I need to right isdigit()?
I hope I get this done ok... i'll see what I can do and post what happens.

Does this mean getchar() returns a char and I am "converting it" to an int? (It's called getchar so I guess thats what it does...)

And, what "exactly" does the line
temp = temp - '0';
Do?

Is this the prefered way to do it or is there a better way?

07-27-2002

Prelude

>Does this mean getchar() returns a char and I am "converting it" to an int? (It's called getchar so I guess thats what it does...)
It is called getchar, but it returns an int because it should also be able to return EOF, which is not a value that can be held by a char. It is all very confusing in this area, just remember that getchar returns the character set's integer value for that character. Meaning that if you entered 5 at the keyboard, getchar would return 53 for ASCII instead of 5.

>And, what "exactly" does the line
>temp = temp - '0';
>Do?
temp - '0' returns the integer value of the character contained in temp. So if temp is '5' then temp - '0' will return the integer value 5 instead of the ASCII value 53 or the EBCDIC value 245.

>Is this the prefered way to do it or is there a better way?
It is the easiest way for single digit numbers.

-Prelude

07-27-2002

Unregistered

Thanks, I think I understand that ok...

>It is the easiest way for single digit numbers.
What about larger numbers? What's another way to do it?

07-27-2002

Prelude

>What about larger numbers? What's another way to do it?
There are several standard library functions which will do this with multidigit numbers. Though they assume that such numbers are in a char array: