Author | Topic |
Toymods Social Secretary
Location: Sydney
Registered: July 2002
|
WAY OT: C programmers, I know there are some of you out there
|
Sat, 24 May 2003 07:47
|
|
Ok, this is waaaay outta left feild i know. But i am desperate.
Im having serious issues comprehending the use of pointers and structures. Im particular with an assignment i have for uni, which is due on monday.
The assignment is to do the following: "allow the user to enter an arbitrarily long string of characters from the keyboard, and when the user types the carriage return character (enter), your program should print all of the characters onto the screen in reverse order".
And it MUST use a stack, in the form of the following structure:
struct node
{
char ch;
struct node *ptr_node;
}
and then use the functions
int pop(struct node **top);
void push(struct node **top, char ch); /* not sure if char ch should be same as in the struct definition */
int is_empty(struct node *top);
The main first hurdle i face is how to read the charcters from the scanf operation into a node struct and then pass it to the push function everytime a character is pushed! Im lost!
HELP!
|
|
|
I supported Toymods
Location: Northern Beaches
Registered: May 2002
|
Re: WAY OT: C programmers, I know there are some of you out there
|
Sat, 24 May 2003 08:16
|
|
I think you can use a function getchar()
eg.
ch = getchar()
|
|
|
I supported Toymods
Location: Northern Beaches
Registered: May 2002
|
|
|
I supported Toymods
Location: Northern Beaches
Registered: May 2002
|
|
|
Toymods Social Secretary
Location: Sydney
Registered: July 2002
|
Re: WAY OT: C programmers, I know there are some of you out there
|
Sat, 24 May 2003 09:29
|
|
hooray! thanks dude
that does help a fair bit. I was having issues linking all the bits i've learned over the years, you know how it gets sometimes....
|
|
|
Location: Canberra
Registered: October 2002
|
|
|
Toymods Social Secretary
Location: Sydney
Registered: July 2002
|
Re: WAY OT: C programmers, I know there are some of you out there
|
Sat, 24 May 2003 09:40
|
|
oops... forgot one thing.
I've totally blanked on what the ch!=input[0] part means. I think i missed that lecture
I understand the rest and can work with it, but the ch! operator has me temporarily baffled
|
|
|
I supported Toymods
Location: Northern Beaches
Registered: May 2002
|
Re: WAY OT: C programmers, I know there are some of you out there
|
Sat, 24 May 2003 12:53
|
|
the
ch != input[0]
its saying
while ch does not equal to the first character of your input
so that it stops when it gets to the beginning of your string
Im not sure if you can use the != like this though if its comparing the address or the values....
in c++ you have strings which makes it a little easier and can't remeber with c.
if you are still stuck I could spend a couple minutes and try remember exactly how to do it if this doesn't work
|
|
|
I supported Toymods
Location: Northern Beaches
Registered: May 2002
|
Re: WAY OT: C programmers, I know there are some of you out there
|
Sat, 24 May 2003 13:46
|
|
just did a quick run through of code and there are some problems in it, that im getting core dumps
and a few problems, one more thing
ch = malloc(sizeof(char)); should be
ch = (char *) malloc(sizeof(char));
it gets you a start anyway
if I had time id love to do it as a little fun to remeber C, but I don't
|
|
|
Toymods Social Secretary
Location: Sydney
Registered: July 2002
|
Re: WAY OT: C programmers, I know there are some of you out there
|
Sat, 24 May 2003 14:03
|
|
its all good mate.. i took what you gave and used that as a starter to kick start my recollection of all things C.
Never touched C++, dont think i will either.
I avoided using the dyanmic memory allocation (malloc) and an array like you have done so far, since there is no real limit to the number of characters the user can enter for the sting before they hit enter, and C DOES NOT LIKE getting arrays that don't have pre-defined size (compilers spew at you!).
Instead i am still trying to figure out a way that either each character is automatically "scanned in" or "input" when the key is pressed, and does not wait for the return key to be presses, and thusly have each character dealt with like that. Although this is proving annoying.
The alternative im not 100% sure on.....
I have aced this subject thus far, and this assignment only being worth 15-20% of my mark... i should be fine. Especially if i go ahead and do the "optional advanced assignment" worth up to a BONUS 20 marks.
<edit> gimme a soldering iron, a pcb, some microcontrollers and other fun stuff ANYDAY.
[Updated on: Sat, 24 May 2003 14:05]
|
|
|
I supported Toymods
Location: Northern Beaches
Registered: May 2002
|
Re: WAY OT: C programmers, I know there are some of you out there
|
Sat, 24 May 2003 15:12
|
|
well use getchar() if you want to process as each key is pressed
hehe oops my thing up there would be find to print something backwards
but to put it on a stack like you want, then you want to run through it from beginning to end. Not end to front like ive done
|
|
|
Location: Brisbane
Registered: November 2002
|
Re: WAY OT: C programmers, I know there are some of you out there
|
Sat, 24 May 2003 22:47
|
|
i've got a c program from an assignment that reads a text file of any length without allocating memory. you might be able to change it to suit your case. If you would like a copy, let me know.
|
|
|
Location: Brisbane
Registered: May 2002
|
Re: WAY OT: C programmers, I know there are some of you out there
|
Sun, 25 May 2003 00:12
|
|
I have a document with some good stuff on calloc, malloc, and realloc if you need it.
Cheers
Wilbo
|
|
|
Location: Brisbane
Registered: November 2002
|
Re: WAY OT: C programmers, I know there are some of you out there
|
Sun, 25 May 2003 03:53
|
|
anyone know anything about two level page tables? i have to implement one in C and i'm having a few troubles..
|
|
|
Location: Cabramatta, NSW
Registered: May 2002
|
Re: WAY OT: C programmers, I know there are some of you out there
|
Mon, 26 May 2003 07:11
|
|
*shudder*
C sux0rz. Thanks for reminding me of how much it truly does suck though... Glad I'm a Java programmer again... hehe
|
|
|
Toymods Social Secretary
Location: Sydney
Registered: July 2002
|
Re: WAY OT: C programmers, I know there are some of you out there
|
Mon, 26 May 2003 12:35
|
|
bastard java guy.
|
|
|
Location: Cabramatta, NSW
Registered: May 2002
|
Re: WAY OT: C programmers, I know there are some of you out there
|
Mon, 26 May 2003 23:37
|
|
Two words: Dynamic Strings
Mwuahahaha!!
|
|
|
Toymods Social Secretary
Location: Sydney
Registered: July 2002
|
Re: WAY OT: C programmers, I know there are some of you out there
|
Tue, 27 May 2003 02:25
|
|
Max, as has been said a few times before:
"You are gay and you eat poo"
|
|
|
I supported Toymods
Location: Northern Beaches
Registered: May 2002
|
Re: WAY OT: C programmers, I know there are some of you out there
|
Tue, 27 May 2003 08:55
|
|
don't worry it took his java program 10 minutes to put that string onto the screen
|
|
|
Location: Helensvale, Queensland
Registered: March 2003
|
Re: WAY OT: C programmers, I know there are some of you out there
|
Tue, 27 May 2003 10:09
|
|
I have 3 C++ books for sale if anybody is interested. isaac@islade.com
|
|
|
I supported Toymods
Location: Northern Beaches
Registered: May 2002
|
Re: WAY OT: C programmers, I know there are some of you out there
|
Tue, 27 May 2003 20:58
|
|
what books they are and where you are located would be nice
|
|
|
Location: Helensvale, Queensland
Registered: March 2003
|
Re: WAY OT: C programmers, I know there are some of you out there
|
Wed, 28 May 2003 02:35
|
|
Idiots guide to C++, C++ from the ground up second edition, C++ the complete reference third edition
gold coast
|
|
|
Location: Cabramatta, NSW
Registered: May 2002
|
Re: WAY OT: C programmers, I know there are some of you out there
|
Wed, 28 May 2003 02:39
|
|
Ribbo wrote on Tue, 27 May 2003 18:55 | don't worry it took his java program 10 minutes to put that string onto the screen
|
Simple 80/20 rule dude. It'd take me 1/5th the time to write something in Java than in C or even C++.
And despite what C people will tell you, Java ain't that slow. Try using Poseidon or IntelliJ and you'll see how snappy a well written, feature-rich Java program can be.
C is nice, but you spend half the time coding and the other half doing work arounds for deficiencies in the language. Unless you use non-standard libraries (which opens another can of worms).
|
|
|
Location: ballarat
Registered: April 2003
|
Re: WAY OT: C programmers, I know there are some of you out there
|
Wed, 28 May 2003 04:57
|
|
i think that java is awesome to get an idea going in very little time, but i think the end product should be in c/c++ or java using a fast c/c++ dll/shared library to do the hard work.
|
|
|