NEED HELP WITH A PROGRAM....ANYONE PLEASE HELP!

G

genestarwing

osmium said:
I suggest you use truex


So how far did it get, what test messages did you see?
I get the fgets() error when i try to run it.......i dont know if i am
using the command wrong but i posted a portion of my code.
 
G

genestarwing

osmium said:
I suggest you use truex


So how far did it get, what test messages did you see?
I get the fgets() error when i try to run it.......i dont know if i am
using the command wrong but i posted a portion of my code.
As for truex command......i never used it and don't know how to use it.
 
D

Dann Corbit

[snip]
Am I using the fget() command wrong here: [snip]
139 fgets(fileName);

Yes. You are using it incorrectly. This is actually a bit tricker than many
people would imagine, since the fgets() function is also going to return the
newline at the end of the buffer.

Here is Jack Klein's getsafe() function from
http://home.att.net/~jackklein/ctips01.html#safe_gets

#include <stdio.h>
#include <string.h>
char *getsafe(char *buffer, int count)
{
char *result = buffer, *np;
if ((buffer == NULL) || (count < 1))
result = NULL;
else if (count == 1)
*result = '\0';
else if ((result = fgets(buffer, count, stdin)) != NULL)
if (np = strchr(buffer, '\n'))
*np = '\0';
return result;
}

As you can see, getting a string from the console can be a bit tricky.

To use this function, just call it with the string, followed by the actual
length of the string.
e.g.

char foo[256];
....
if (getsafe(foo, sizeof foo, stdin) != NULL)
{
/* do stuff with the string */
}
 
O

osmium

As for truex command......i never used it and don't know how to use it.

It is not a command, it is the name of a variable.

Remember this?:
int true = 1;

Change it to truex and change anyplace else in the code where you refer to
this variable.

int truex = 1;
 
R

Richard Heathfield

osmium said:
ignore the warning. gets is not bulletproof but this is just a student
program.

Can I just say that's the worst advice I've seen in comp.lang.c for weeks?
This is not *just* a student program. This is a *student program* - a
program written with the specific purpose of learning how to write C
programs. We do *not* write C programs using gets(), and for an excellent
reason, as you well know. So *now* is the time for this student to learn
how to capture input robustly.
 
D

dcorbit

Dann Corbit wrote:
[snip]
DEFECT REPORT:

Jack's getsafe function assumes stdin, so the correct invocation is
not:
if (getsafe(foo, sizeof foo, stdin) != NULL)

but
if (getsafe(foo, sizeof foo) != NULL)
 
R

Richard Heathfield

(e-mail address removed) said:
Hey Keith guess what?!
right now i am too stressed out to even think about your stupid top
posting stuff!

If you can't learn the very simple task of posting a Usenet article in such
a way that it is easy and pleasant to read, it would be a good idea to pick
a discipline less difficult than programming. Marketing springs to mind.
Or, since you seem to prefer heaping abuse on others, perhaps management is
your cup of tea.
i am here for some answers! so if you have them then let it
out.......otherwise try not to post you suggestions, because its waste
of my time to come and read stupid comments that are not relevent to my
question.

Keith is not in the habit of writing irrelevant comments. If you don't see
their relevance, that's your problem, not his. He knows what he's talking
about and he's a very helpful person who is actually trying to help you to
learn C better - and learning to use Usenet more effectively will certainly
help you learn C better. But if you continue to insult those who help you,
you won't continue to get help for very long.
 
K

Keith Thompson

well thanks i'll try finding help some place else because all you are
concerned about is how people are posting there questions. besides i
havent gotten "ne thing" useful from u ne ways!
peace

A student walks into his professor's office for help with a homework
assignment. He shoves some papers aside, sits on the professor's
desk, and starts describing his problem. The professor starts to make
some useful suggestions, then asks you to please get off his desk and
sit in the chair. The student loudly tells the professor he doesn't
want to hear any stupid advice about where he should sit, advice that
has nothing to do with his problem.

It's not a perfect analogy, but it's pretty close.
 
K

Keith Thompson

Dann Corbit said:
[snip]
ok so what do u suggest me using it instead of true = 0 ?
i also made the changes you suggested.....

Include only the content necessary when you quote someone in a follow-up.

If your compiler has a boolean type, use that.

If your compiler lacks a boolean type, you can use screaming macros, like
this:

#undef FALSE
#define FALSE 0
#undef TRUE
#define TRUE !FALSE

Then use TRUE and FALSE everywhere you want a two-state boolean value.
Or something of that nature.

It's simpler just to write

#define TRUE 1

Section 9 of the comp.lang.c FAQ covers this quite well.
 
K

Keith Thompson

I get the fgets() error when i try to run it.......i dont know if i am
using the command wrong but i posted a portion of my code.

What exactly does "I get the fgets() error" mean?
 
O

Old Wolf

Dann said:
Some people are really just not cut out to be programmers.

Or university students, for that matter. I get the feeling that the
guy did not actually write the code that was "his attempt so far".
 
O

osmium

I get the fgets() error when i try to run it.......i dont know if i am
using the command wrong but i posted a portion of my code.
As for truex command......i never used it and don't know how to use it.

You have certainly tried hard enough and I can see you have invested a lot
of time in this. But you ignored my early advice about breaking things down
into pieces. You have one huge function called, roughly, createbinarytree.
That function opens a file, reads from the file, finds the words, and adds
the words to a tree. You need to assign responsibility to a group of
functions that each do one thing and do it well. Eventually, you will need
a menu. Remember I told you to defer that? At this point in the learning
process, a menu is just a time consuming distraction.

I think in terms of plateaus. The first plateau is to construct a tree
containing words, without such a tree none of the other stuff called for can
happen. There are three parts to reaching this first plateau. Opening and
reading a file, isolating the words, and adding the words to a tree. I give
code below that reads the file and isolates the words. I suppose there are
bugs in it, but it passes some cursory checks. Note that isolating the
function like this will make it possible to fairly easily refine the
definition of a word later on, remember that I said this was a first cut at
a definition. The code could be made more compact and cryptic than it is.

Now you need to write a function something like this:
void add_word(struct* Node tree, char* word);

The first parameter identifies the tree and the second identifies the word
to be added. With a modern computer there is no reason to fail so the
return is simply void. The fist entry added will be an "odd-ball" and the
way I would do it would require unique logic for that particular case.

The code below was tested with the file testtree.txt which is attached to a
message in alt.test named "tree tree tree". You will probably recognize the
text of the message. Copy that file to the directory your compiler is using
for the source code you write. My news server does not let me attach files
to posts to this news group.

------------
#include <stdio.h>
#include <ctype.h>

char* fn = "testtree.txt";


// finds next word in line and returns a pointer to it.
// writes '\0' in line, thus destroying original content
// returns NULL if no word could be found.
char* get_word(char* line)
{
char ch;
char* start;
static char* resume;
if(resume)
line = resume; // continue parsing original string
// skip non letters
while(1)
{
if(*line == 0)
{
resume = 0;
return NULL; // end of line
}
ch = *line++;
ch = tolower(ch);
if(islower(ch))
{
start = --line; // undo premature advance
break;
}
}
// have the beginning of a word.
// now find the end of the word
while(1)
{
ch = *line++;
ch = tolower(ch);
if(!islower(ch))
{
// remember where to continue from
resume = line;
// mark this as the end of a C string
line--;
*line = '\0';
// was used for intial testing
//printf("%s\n", start);
return start;
}
} // end while
} // end function
//========================
int main(void)
{
FILE* in;
char buffer[1000];
char* wd;
char* status;
in = fopen(fn, "r");
if(!in)
{
printf("Can't open file\n");
getchar();
return 1;
}
while(1)
{
status = fgets(buffer, 999, in);
if(!status)
{
printf("EOF encountered\n");
getchar();
break;
}
while(wd = get_word(buffer))
printf("%s\n", wd);
}
getchar();
return 0;
}
 
G

genestarwing

osmium said:
I get the fgets() error when i try to run it.......i dont know if i am
using the command wrong but i posted a portion of my code.
As for truex command......i never used it and don't know how to use it.

You have certainly tried hard enough and I can see you have invested a lot
of time in this. But you ignored my early advice about breaking things down
into pieces. You have one huge function called, roughly, createbinarytree.
That function opens a file, reads from the file, finds the words, and adds
the words to a tree. You need to assign responsibility to a group of
functions that each do one thing and do it well. Eventually, you will need
a menu. Remember I told you to defer that? At this point in the learning
process, a menu is just a time consuming distraction.

I think in terms of plateaus. The first plateau is to construct a tree
containing words, without such a tree none of the other stuff called for can
happen. There are three parts to reaching this first plateau. Opening and
reading a file, isolating the words, and adding the words to a tree. I give
code below that reads the file and isolates the words. I suppose there are
bugs in it, but it passes some cursory checks. Note that isolating the
function like this will make it possible to fairly easily refine the
definition of a word later on, remember that I said this was a first cut at
a definition. The code could be made more compact and cryptic than it is.

Now you need to write a function something like this:
void add_word(struct* Node tree, char* word);

The first parameter identifies the tree and the second identifies the word
to be added. With a modern computer there is no reason to fail so the
return is simply void. The fist entry added will be an "odd-ball" and the
way I would do it would require unique logic for that particular case.

The code below was tested with the file testtree.txt which is attached to a
message in alt.test named "tree tree tree". You will probably recognize the
text of the message. Copy that file to the directory your compiler is using
for the source code you write. My news server does not let me attach files
to posts to this news group.

------------
#include <stdio.h>
#include <ctype.h>

char* fn = "testtree.txt";


// finds next word in line and returns a pointer to it.
// writes '\0' in line, thus destroying original content
// returns NULL if no word could be found.
char* get_word(char* line)
{
char ch;
char* start;
static char* resume;
if(resume)
line = resume; // continue parsing original string
// skip non letters
while(1)
{
if(*line == 0)
{
resume = 0;
return NULL; // end of line
}
ch = *line++;
ch = tolower(ch);
if(islower(ch))
{
start = --line; // undo premature advance
break;
}
}
// have the beginning of a word.
// now find the end of the word
while(1)
{
ch = *line++;
ch = tolower(ch);
if(!islower(ch))
{
// remember where to continue from
resume = line;
// mark this as the end of a C string
line--;
*line = '\0';
// was used for intial testing
//printf("%s\n", start);
return start;
}
} // end while
} // end function
//========================
int main(void)
{
FILE* in;
char buffer[1000];
char* wd;
char* status;
in = fopen(fn, "r");
if(!in)
{
printf("Can't open file\n");
getchar();
return 1;
}
while(1)
{
status = fgets(buffer, 999, in);
if(!status)
{
printf("EOF encountered\n");
getchar();
break;
}
while(wd = get_word(buffer))
printf("%s\n", wd);
}
getchar();
return 0;
}

void add_word(struct* Node tree, char* word);
i get syntax error before '*' for this line
 
G

genestarwing

osmium said:
I get the fgets() error when i try to run it.......i dont know if i am
using the command wrong but i posted a portion of my code.
As for truex command......i never used it and don't know how to use it.

You have certainly tried hard enough and I can see you have invested a lot
of time in this. But you ignored my early advice about breaking things down
into pieces. You have one huge function called, roughly, createbinarytree.
That function opens a file, reads from the file, finds the words, and adds
the words to a tree. You need to assign responsibility to a group of
functions that each do one thing and do it well. Eventually, you will need
a menu. Remember I told you to defer that? At this point in the learning
process, a menu is just a time consuming distraction.

I think in terms of plateaus. The first plateau is to construct a tree
containing words, without such a tree none of the other stuff called for can
happen. There are three parts to reaching this first plateau. Opening and
reading a file, isolating the words, and adding the words to a tree. I give
code below that reads the file and isolates the words. I suppose there are
bugs in it, but it passes some cursory checks. Note that isolating the
function like this will make it possible to fairly easily refine the
definition of a word later on, remember that I said this was a first cut at
a definition. The code could be made more compact and cryptic than it is.

Now you need to write a function something like this:
void add_word(struct* Node tree, char* word);

The first parameter identifies the tree and the second identifies the word
to be added. With a modern computer there is no reason to fail so the
return is simply void. The fist entry added will be an "odd-ball" and the
way I would do it would require unique logic for that particular case.

The code below was tested with the file testtree.txt which is attached to a
message in alt.test named "tree tree tree". You will probably recognize the
text of the message. Copy that file to the directory your compiler is using
for the source code you write. My news server does not let me attach files
to posts to this news group.

------------
#include <stdio.h>
#include <ctype.h>

char* fn = "testtree.txt";


// finds next word in line and returns a pointer to it.
// writes '\0' in line, thus destroying original content
// returns NULL if no word could be found.
char* get_word(char* line)
{
char ch;
char* start;
static char* resume;
if(resume)
line = resume; // continue parsing original string
// skip non letters
while(1)
{
if(*line == 0)
{
resume = 0;
return NULL; // end of line
}
ch = *line++;
ch = tolower(ch);
if(islower(ch))
{
start = --line; // undo premature advance
break;
}
}
// have the beginning of a word.
// now find the end of the word
while(1)
{
ch = *line++;
ch = tolower(ch);
if(!islower(ch))
{
// remember where to continue from
resume = line;
// mark this as the end of a C string
line--;
*line = '\0';
// was used for intial testing
//printf("%s\n", start);
return start;
}
} // end while
} // end function
//========================
int main(void)
{
FILE* in;
char buffer[1000];
char* wd;
char* status;
in = fopen(fn, "r");
if(!in)
{
printf("Can't open file\n");
getchar();
return 1;
}
while(1)
{
status = fgets(buffer, 999, in);
if(!status)
{
printf("EOF encountered\n");
getchar();
break;
}
while(wd = get_word(buffer))
printf("%s\n", wd);
}
getchar();
return 0;
}

void add_word(struct* Node tree, char* word);
i get syntax error before '*' for this line

ok so fgets was giving me errors....so i removed it and put scanf
instead. now my program runs without errors. but when i put a file name
to run it.....it gives me segmentation fault.

int createBinaryTree()
{
char fd[1024]; //Textual Data Pointer
char *token;
int check = 0;
int counter = 1;
int true = 1;

printf("\n\t\t---------------------------------------");
printf("\n\t\t------- Frequency of Words ---------");
printf("\n\t\t---------------------------------------");
printf("\n\n\t\tPlease enter the text file name : ");
scanf("%s",&fileName);

printf("\n\t\t---------------------------------------");
printf("\n\n\t\t\tReading file");
// delay(500);
printf(".");
// delay(100);
printf(".");
// delay(100);
printf(".");
// delay(100);
printf(".");
// delay(100);


if ((fp = fopen(fileName, "r")) ==NULL){
printf("\n\n\t\t\tError reading file.");
getchar();
return 1;
}

else {

fgets(fd, fileSize(fp) + 1, fp);
fclose(fp); //Close the file

token = strtok(fd," ");
if (token == NULL) {
printf("\n\n\t\tThe text file is empty.");
true = 0; //So that loop is not entered
getchar();
return 1; //Don't show the menu
}
else {
node = (struct tree *)malloc(sizeof(struct tree));
(*node).word = token;
(*node).count = 1;
node->right = NULL;

sptr=node;
q=node;
 
O

osmium

void add_word(struct* Node tree, char* word);
i get syntax error before '*' for this line

Node is a user defined type which you haven't defined. Remember this?::

struct Node
{
char* wd;
int ct;
struct Node* left_child;
struct Node* right_child;
};

Put that right below the include files, which will make it global scope.
 
O

osmium

ok so fgets was giving me errors....so i removed it and put scanf
instead. now my program runs without errors. but when i put a file name
to run it.....it gives me segmentation fault.

int createBinaryTree()
{
char fd[1024]; //Textual Data Pointer
char *token;
int check = 0;
int counter = 1;
int true = 1;

printf("\n\t\t---------------------------------------");
printf("\n\t\t------- Frequency of Words ---------");
printf("\n\t\t---------------------------------------");
printf("\n\n\t\tPlease enter the text file name : ");
scanf("%s",&fileName);
<snip>

I suggest you give up on this code. There is way too much wrong with it, it
is like putting parches on patches. There was simply no structure there.
Try building on the base I posted earlier today. If you feel uncomfortable
with foreign code (mine) you could start from scratch and build a tree as a
separate effort and meld the two pieces of code later.
 
K

Keith Thompson

osmium said:
void add_word(struct* Node tree, char* word);
i get syntax error before '*' for this line

Node is a user defined type which you haven't defined. Remember this?::

struct Node
{ [snip]
};

Put that right below the include files, which will make it global scope.

That won't help. The name of the type is "struct Node"; you can't
insert a "*" into the middle of the name, whether it's been defined or
not.

struct* Node tree
is a syntax error. To declare "tree" as a pointer to a struct node use:
struct Node *tree
 
D

Default User

osmium wrote:

I suggest you give up on this code. There is way too much wrong with
it, it is like putting parches on patches. There was simply no
structure there. Try building on the base I posted earlier today. If
you feel uncomfortable with foreign code (mine) you could start from
scratch and build a tree as a separate effort and meld the two pieces
of code later.


I think this is sound advice. The program is too big and too flawed.
The OP should start again, and start small. Learn to read in the
filename first. Then open the file. Build up the pieces. Learn how to
stub out functions.




Brian
 
G

genestarwing

osmium said:
ok so fgets was giving me errors....so i removed it and put scanf
instead. now my program runs without errors. but when i put a file name
to run it.....it gives me segmentation fault.

int createBinaryTree()
{
char fd[1024]; //Textual Data Pointer
char *token;
int check = 0;
int counter = 1;
int true = 1;

printf("\n\t\t---------------------------------------");
printf("\n\t\t------- Frequency of Words ---------");
printf("\n\t\t---------------------------------------");
printf("\n\n\t\tPlease enter the text file name : ");
scanf("%s",&fileName);
<snip>

I suggest you give up on this code. There is way too much wrong with it, it
is like putting parches on patches. There was simply no structure there.
Try building on the base I posted earlier today. If you feel uncomfortable
with foreign code (mine) you could start from scratch and build a tree as a
separate effort and meld the two pieces of code later.

well thanks for your help....i appreciate it! you are right, this code
is getting more and more complex everytime i change something to it.
The only reason I came here for help is because my teacher assingned
this question without even teaching us the chapter and now he went on
vacation. I have to do this program in order to pass the class. Also
teachers in Ontario Canada went on strike for three weeks during school
year. That messed up the entire schedule for students and now we have
to cover topics on our own. Anyways thanks for your help! I'll just
hand it in like this.....maybe i'll get partial credit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top