Does anybody know much about C Programming? - GM Forum - Buick, Cadillac, Chev, Olds, GMC & Pontiac chat


Lounge For casual talk about things unrelated to General Motors. In other words, off-topic stuff. And anything else that does not fit Section Description.

Reply
 
 
 
Thread Tools Search this Thread
Old 04-23-2004, 01:57 AM   #1
Senior Member
Posts like a Turbo
Thread Starter
 
Join Date: Apr 2003
Location: Arkansas
Posts: 409
Thanks: 0
Thanked 0 Times in 0 Posts
parallelcircuits is on a distinguished road
Default Does anybody know much about C Programming?

Is there any programmers here? If so maybe I can get some help.

I am trying to read a unknown length of text separated by white space. Separate the words and place them in a binary tree. I can get it to work if I set up a exit string but the teacher will not accept it that way.

I am trying to use scanf("%*", input); where input is a char input[50];

It will read and separate the text but will not exit because it scanf still wants a string.

Does this make any sence to anybody?

Any help would be great.

Here is the main part of the program
Code:
while(1)
{
      curr = (node *)malloc(sizeof(node));
      curr->left = curr->right = NULL;
      scanf("%*", input);
      printf("%* ", input);
      if(isspace((int)input[0]) && isspace((int)input[1]) == 0)
      {
           break;
      }
      strcpy ( curr->word, input); 
      insert(&output, curr);
}
Here is what I am trying to do.
http://csce.uark.edu/~bss03/ceng1121l/bintree/
parallelcircuits is offline   Reply With Quote
Old 04-23-2004, 03:07 AM   #2
Senior Member
Certified Car Nut
 
Join Date: Mar 2003
Posts: 12,432
Thanks: 0
Thanked 0 Times in 0 Posts
J Wikoff is on a distinguished road
Default

Unfortuantly, that looks familiar. Fortunatly, for me, not you, that was a long time ago. I don't remember how.
J Wikoff is offline   Reply With Quote
Old 04-23-2004, 10:41 AM   #3
Senior Member
Certified GM nut
 
Join Date: Sep 2002
Location: Eden Prairie, MN
Posts: 1,728
Thanks: 0
Thanked 0 Times in 0 Posts
zzzzzeke is on a distinguished road
Default

sorry.....not proficient in C. when i had a problem with a different language i tried here http://www.tek-tips.com/, but it doesn't seem like they have a forum for C.
look around!
good luck
zzzzzeke is offline   Reply With Quote
Old 04-23-2004, 04:34 PM   #4
Senior Member
Posts like a Supercharger
 
Join Date: Mar 2004
Location: Columbia, MO
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
enmityst is on a distinguished road
Default Re: Does anybody know much about C Programming?

Quote:
Originally Posted by parallelcircuits
Is there any programmers here? If so maybe I can get some help.

I am trying to read a unknown length of text separated by white space. Separate the words and place them in a binary tree. I can get it to work if I set up a exit string but the teacher will not accept it that way.

I am trying to use scanf("%*", input); where input is a char input[50];

It will read and separate the text but will not exit because it scanf still wants a string.
Ug, I forgot how hard it is to try and read someone else'* code -- it'* like trying to find something in a kitchen you've never been to before

I'm a little confused as to why you're using scanf() here -- scanf() reads data from stdin (keyboard), parses it according to your delimiters, and stores it in the variable specified -- the way you have it set up, the program will enter the while loop, initialize a node, and then wait for keyboard input. If that'* what it'* supposed to do, sorry, I just can't really tell without seeing the rest of the program

Looking over the assignment, I was under the impression that the input was just going to be *one* string, words separated by whitespace, and a null terminator -- i.e.,
"now is the time for all good men", etc, and that you would have to parse that one large string into many small strings ("now", "is", etc), then store those small strings in the appropriate place (node->word), rather than using the keyboard to enter them one at a time.

If that is indeed the case, strtok() is the function you want to use: (excuse the quick 'n dirty programming -- I haven't written any in C in almost 5 years)

Code:
char string[]="this is an example string";
char *token; 
// may want to do a malloc() here to be safe

token = strtok(string, " ")
// this will pull the first token out of the string ("this"). the " " is your list of delimiters, which in your case is just a whitespace. so token will point to the first group of non-whitespace characters in the string -- i.e., the first word.

while (token != NULL) { // strtok returns a null pointer when it hits the end of the string
node->word = *token; 
// or however your pointers and everything are set up

token = strtok(NULL, " "); 
// strange thing about strtok -- it remembers what string it'* looking at and where it is. so after the first call, all you have to pass is a null pointer and your delimiter string, and it will remember where it'* at and return the pointer to the next token.
}
Or something like that. Check your API, like I said, I'm kind of rusty.

-b
enmityst is offline   Reply With Quote
Old 04-23-2004, 04:39 PM   #5
Senior Member
Posts like a Supercharger
 
Join Date: Mar 2004
Location: Columbia, MO
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
enmityst is on a distinguished road
Default

Oh, one thing I forgot to mention -- if there are any other functions that do anything to that string, you may want to copy the string to a buffer for strtok. strtok *will* modify the string passed to it, so if you have other functions depending on it, it may jack things up a bit.

For this same reason it'* important that the string that gets passed to strtok isn't initialized as const.

-b
enmityst is offline   Reply With Quote
 
 
Reply

Related Topics
Thread Thread Starter Forum Replies Last Post
Anybody know much about this car? chadow427 Lounge 15 12-07-2007 01:36 PM
does anybody know how to clean up old rusty manifolds? jachin Detailing & Appearance 5 12-31-2003 02:26 PM
Does anybody know if this camera can upload to computer? ssesc93 Lounge 2 07-16-2003 12:04 AM
Does anybody like Knight Rider as much as I do? ssesc93 Lounge 20 06-30-2003 01:52 PM
Does anybody know about rims fitting from other GM cars... brminder 1992-1999 7 03-31-2003 12:58 AM


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -4. The time now is 12:51 AM.


We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to Amazon.com and affiliated sites.