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.

Does anybody know much about C Programming?

Thread Tools
 
Old 04-23-2004, 12:57 AM
  #1  
Senior Member
Posts like a Turbo
Thread Starter
 
parallelcircuits's Avatar
 
Join Date: Apr 2003
Location: Arkansas
Posts: 409
Likes: 0
Received 0 Likes on 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/
Old 04-23-2004, 02:07 AM
  #2  
Senior Member
Certified Car Nut
 
J Wikoff's Avatar
 
Join Date: Mar 2003
Posts: 12,433
Likes: 0
Received 2 Likes on 2 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.
Old 04-23-2004, 09:41 AM
  #3  
Senior Member
Certified GM nut
 
zzzzzeke's Avatar
 
Join Date: Sep 2002
Location: Eden Prairie, MN
Posts: 1,728
Likes: 0
Received 1 Like on 1 Post
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
Old 04-23-2004, 03:34 PM
  #4  
Senior Member
Posts like a Supercharger
 
enmityst's Avatar
 
Join Date: Mar 2004
Location: Columbia, MO
Posts: 175
Likes: 0
Received 0 Likes on 0 Posts
enmityst is on a distinguished road
Default Re: Does anybody know much about C Programming?

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
Old 04-23-2004, 03:39 PM
  #5  
Senior Member
Posts like a Supercharger
 
enmityst's Avatar
 
Join Date: Mar 2004
Location: Columbia, MO
Posts: 175
Likes: 0
Received 0 Likes on 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
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
chadow427
Lounge
15
12-07-2007 12:36 PM
jachin
Detailing & Appearance
5
12-31-2003 01:26 PM
ssesc93
Lounge
2
07-15-2003 11:04 PM
ssesc93
Lounge
17
06-29-2003 09:41 PM
brminder
1992-1999
7
03-30-2003 11:58 PM



Quick Reply: Does anybody know much about C Programming?



All times are GMT -4. The time now is 08:39 AM.