By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

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

P: n/a
QUESTION:
Write a program that opens and read a text file and records how many
times each word occurs in the file. Use a binary search tree modified
to store both a word and the number of times it occurs. After the
program has read the file, it should offer a menu with three choices.
the first is to list all the words along with the number of occurences.
The second is to let you enter a word, with the program reporting how
many times the word occured in the file. The third is to quit.

This quesiton is from the book C Primer Plus 5th edition by Stephen
Prata. Chapter 17 question 7. I read the chapter twice and can't figure
out how to write this program. Is there anyone out there who can give
me the code for this program. I am desperate. Maybe I could pay whoever
can give me the code.

Jun 8 '06 #1
Share this Question
Share on Google+
66 Replies


P: n/a

<ge**********@hotmail.com> wrote
QUESTION:
Write a program that opens and read a text file and records how many
times each word occurs in the file. Use a binary search tree modified
to store both a word and the number of times it occurs. After the
program has read the file, it should offer a menu with three choices.
the first is to list all the words along with the number of occurences.
The second is to let you enter a word, with the program reporting how
many times the word occured in the file. The third is to quit.

This quesiton is from the book C Primer Plus 5th edition by Stephen
Prata. Chapter 17 question 7. I read the chapter twice and can't figure
out how to write this program. Is there anyone out there who can give
me the code for this program. I am desperate. Maybe I could pay whoever
can give me the code.

Forget about the binary search tree.
Write the program so that it goes throught he file, picks out the words, and
stores the count of each word in a flat list. If it helps, set a limit of
10000 distinct words.

Then come back with what you've done and start on the binary tree bit.

--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm
Jun 8 '06 #2

P: n/a
ge**********@hotmail.com wrote:
QUESTION:
Write a program that opens and read a text file and records how many
times each word occurs in the file.
<snip>
Maybe I could pay whoever can give me the code.


how would that help you? You learn to program by writing programs. As
another poster suggested, tackle a slightly simplified version of the
program
and then add to it.

For instance
open file
read lines
close file

the process the lines to extract words. First decide what a word is
(eg. a
sequence of non-whitespace terminated by whitespace, whitespace is
space, newline or tab).

and so on. Learn by doing, take small steps
--
Nick Keighley

Jun 8 '06 #3

P: n/a
ge**********@hotmail.com wrote:

QUESTION:
Write a program that opens and read a text file and records how many
times each word occurs in the file. Use a binary search tree modified
to store both a word and the number of times it occurs. After the
program has read the file, it should offer a menu with three choices.
the first is to list all the words along with the number of occurences.
The second is to let you enter a word, with the program reporting how
many times the word occured in the file. The third is to quit.

This quesiton is from the book C Primer Plus 5th edition by Stephen
Prata. Chapter 17 question 7. I read the chapter twice and can't figure
out how to write this program. Is there anyone out there who can give
me the code for this program. I am desperate. Maybe I could pay whoever
can give me the code.


If this is for homework, the following won't help you at all. If
you just want to learn things, try the wdfreq usage demonstration
in my hashlib package. See:

<http://cbfalconer.home.att.net/download/>

--
"Our enemies are innovative and resourceful, and so are we.
They never stop thinking about new ways to harm our country
and our people, and neither do we." -- G. W. Bush.
"The people can always be brought to the bidding of the
leaders. All you have to do is tell them they are being
attacked and denounce the pacifists for lack of patriotism
and exposing the country to danger. It works the same way
in any country." --Hermann Goering.

Jun 8 '06 #4

P: n/a
<ge**********@hotmail.com> wrote:
QUESTION:
Write a program that opens and read a text file and records how many
times each word occurs in the file. Use a binary search tree modified
to store both a word and the number of times it occurs. After the
program has read the file, it should offer a menu with three choices.
the first is to list all the words along with the number of occurences.
The second is to let you enter a word, with the program reporting how
many times the word occured in the file. The third is to quit.

This quesiton is from the book C Primer Plus 5th edition by Stephen
Prata. Chapter 17 question 7. I read the chapter twice and can't figure
out how to write this program. Is there anyone out there who can give
me the code for this program. I am desperate. Maybe I could pay whoever
can give me the code.


You don't say where your problem is. I don't have that book but based on
other books by that author, I would expect that I would like it. If you
have been going through the book - not attacking it in the center, for
example - the only problem I would expect would be the requirement to use a
tree. Chapter 17 sounds like you are well into the book and doing the file
business and isolating a word should be relatively easy for you by now, or
at least doable. This is the data structure I would use for a single node
in the tree, see if it kick starts your mind. Main has a pointer to the top
node of the tree.

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

Jun 8 '06 #5

P: n/a
Well maybe you could help me with the first 15 lines of the code.....i
dont know how to start the program......and no i havent gone through
the entire book. Our school was out cuz of the teachers strike happened
for 3 weeks during school period. Now in order for me to pass the
class......teacher want us to do this question. Hes on vacation now so
i cant ask him for help. Its due in two weeks and i've emailed other
teachers too but no reply yet!
osmium wrote:
<ge**********@hotmail.com> wrote:
QUESTION:
Write a program that opens and read a text file and records how many
times each word occurs in the file. Use a binary search tree modified
to store both a word and the number of times it occurs. After the
program has read the file, it should offer a menu with three choices.
the first is to list all the words along with the number of occurences.
The second is to let you enter a word, with the program reporting how
many times the word occured in the file. The third is to quit.

This quesiton is from the book C Primer Plus 5th edition by Stephen
Prata. Chapter 17 question 7. I read the chapter twice and can't figure
out how to write this program. Is there anyone out there who can give
me the code for this program. I am desperate. Maybe I could pay whoever
can give me the code.


You don't say where your problem is. I don't have that book but based on
other books by that author, I would expect that I would like it. If you
have been going through the book - not attacking it in the center, for
example - the only problem I would expect would be the requirement to use a
tree. Chapter 17 sounds like you are well into the book and doing the file
business and isolating a word should be relatively easy for you by now, or
at least doable. This is the data structure I would use for a single node
in the tree, see if it kick starts your mind. Main has a pointer to the top
node of the tree.

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


Jun 8 '06 #6

P: n/a
<ge**********@hotmail.com> wrote:
Well maybe you could help me with the first 15 lines of the code.....i
dont know how to start the program......and no i havent gone through
the entire book. Our school was out cuz of the teachers strike happened
for 3 weeks during school period. Now in order for me to pass the
class......teacher want us to do this question. Hes on vacation now so
i cant ask him for help. Its due in two weeks and i've emailed other
teachers too but no reply yet!


Break the problem down into pieces. Defer the menu until much later, write
the program as though the user choice was the first choice listed, list all
the words and the counts. You need a test file, best put into the same
directory your source code will be in. You could cut and paste one of your
messages in this thread, for example, that's a reasonable sample.

#include <stdlio.h>
#include <ctype.h>
#include <string.h> -- don't feel compelled to use this

struct Node
{
char* wd;
int ct;
struct Node* left_child;
struct Node* right_child;
};
/*=========================*/
int main()
{
struct Node* tree = NULL;
char buff[1024]; /* line 16, a bonus line */

NB: This was not compiled
--------------- pseudo code -------------
open the file

loop
read a line -- perhaps gets()
if done exit the loop
isolate a word -- a function you write perhaps: char* isolate(char*
line);
add the word to the tree -- a function you write. perhaps: void
add(Node* root, char* word);
----
display the tree.
Note: Having created the tree, by now you should have a pretty good idea of
how to display it's content.
Jun 8 '06 #7

P: n/a
I am still confused on how to actually write the whole program.....help
ne one

osmium wrote:
<ge**********@hotmail.com> wrote:
Well maybe you could help me with the first 15 lines of the code.....i
dont know how to start the program......and no i havent gone through
the entire book. Our school was out cuz of the teachers strike happened
for 3 weeks during school period. Now in order for me to pass the
class......teacher want us to do this question. Hes on vacation now so
i cant ask him for help. Its due in two weeks and i've emailed other
teachers too but no reply yet!


Break the problem down into pieces. Defer the menu until much later, write
the program as though the user choice was the first choice listed, list all
the words and the counts. You need a test file, best put into the same
directory your source code will be in. You could cut and paste one of your
messages in this thread, for example, that's a reasonable sample.

#include <stdlio.h>
#include <ctype.h>
#include <string.h> -- don't feel compelled to use this

struct Node
{
char* wd;
int ct;
struct Node* left_child;
struct Node* right_child;
};
/*=========================*/
int main()
{
struct Node* tree = NULL;
char buff[1024]; /* line 16, a bonus line */

NB: This was not compiled
--------------- pseudo code -------------
open the file

loop
read a line -- perhaps gets()
if done exit the loop
isolate a word -- a function you write perhaps: char* isolate(char*
line);
add the word to the tree -- a function you write. perhaps: void
add(Node* root, char* word);
----
display the tree.
Note: Having created the tree, by now you should have a pretty good idea of
how to display it's content.


Jun 9 '06 #8

P: n/a
jjf

ge**********@hotmail.com wrote:
I am still confused on how to actually write the whole program.....help
ne one


Post your attempt and explain which parts of it you're having
difficulty with, and why.

Jun 9 '06 #9

P: n/a
<ge**********@hotmail.com> wrote:
I am still confused on how to actually write the whole program.....help
ne one


Don't worry about writing the whole program. I can see that it might sound
frightening, which is why I gave the help I did. Write the next few lines of
code and post *that*. If you post code you will almost certainly get more
help. If you can't or won't do that, your chances of getting further help
here are about zero.
Jun 9 '06 #10

P: n/a
ok so this is what i got.....but when i try to compile the prg....it
gives me this error
include<conio.h>: no such file or directory
here is my program

#include<string.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void createUp();
void createDown();
void showMenu();
void createRight();
void createLeft();
void search();
void listWords();
void searchWord();
int createBinaryTree();
long fileSize(FILE *);

struct tree
{
char *word; //The word to be stored in a node
// struct tree *left; //Left child of the node
struct tree *right; //Right child of the node
int count; //How many of these words are found by now
};

FILE *fp; //File Pointer
struct tree *node;
struct tree *sptr;
struct tree *q;
char *insdata;
char *fileName;

void main()
{
clrscr();

//Create the Binary Search Tree

if (createBinaryTree() == 0) //Show Menu
{
showMenu();
}
}

void listWords()
{
struct tree *temp;
temp = (struct tree *)malloc(sizeof(struct tree));
temp = node;

while(temp->right!=NULL)
{
printf("\n\tWord:\t%s\t\t\t\tCount:\t%d",temp->word,temp->count);
temp=temp->right;
}
temp = node;
getch();
free(temp);
}

void showMenu()
{
char ch = 'a';
while(ch!='q')
{
clrscr();
printf("\n\t\t---------------------------------");
printf("\n\t\t| Binary Search Tree |");
printf("\n\t\t---------------------------------");
printf("\n\t\t|\tl - List all words\t|");
printf("\n\t\t|\ts - Search a words\t|");
printf("\n\t\t|\tq - Quit\t\t|");
printf("\n\t\t---------------------------------");
printf("\n\t\t| Press your choise (l,s,q)\t|");
printf("\n\t\t---------------------------------\n\t\t\t\t");
ch = getche();
switch(ch)
{
case 'l':
sptr=node;
listWords();
break;
case 's':
sptr=node;
searchWord();
break;
case 'q':
free(node);
free(sptr);
free(q);
default:
break;
}
}
}

void searchWord()
{
int check=1;
char *searchWord;
printf("\n\n\t\tPlease enter the word to search: ");
gets(searchWord);
while(sptr->right !=NULL)
{
if (strcmp(searchWord,sptr->word) == 0) {
printf("\n\n\t\tWord Occurances found:%d\n",sptr->count);
getch();
check = 0;
break;
}
sptr=sptr->right; //Send sptr to its right
}
if (strcmp(searchWord,sptr->word) == 0 && check !=0) {
printf("\n\n\t\tWord Occurances found:%d\n",(*sptr).count);
getch();
check = 0;
}

if(check ==1) {
printf("\n\n\t\tNo occurances of this word found.");
getch();
}
}

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 : ");
gets(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.");
getch();
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
getch();
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;

// loop until finishied
while (true)
{
check = 0;
// extract string from string sequence
token = strtok(NULL, " ");

if (token == NULL)
{
true = 0; //Break the loop
}

insdata = token;
// printf("\nCounter %d\n",counter);
// puts(insdata);
counter++;

sptr= q;

while(sptr->right !=NULL && check==0)
{
if (strcmp(insdata,sptr->word) == 0) {
(*sptr).count++; //Increment
// printf("%d",(*sptr).count);
check=1;
}
sptr=sptr->right; //Send sptr to its right
}

if (strcmp(insdata,sptr->word) == 0) {
(*sptr).count++; //Increment
// printf("%d",(*sptr).count);
check=1;
}

if (check !=1) {
createDown();
}
sptr = node;
printf("%d",true);
}
return 0;
}
}
}

long fileSize(FILE *stream)
{
long curpos, length;
curpos = ftell(stream);
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
fseek(stream, curpos, SEEK_SET);
return length;
}

void createDown()
{

struct tree *temp;
temp = (struct tree *)malloc(sizeof(struct tree));

temp->word = insdata;
(*temp).count = 1;
temp->right= NULL;

sptr->right=temp;
}
/*
void createRight()
{
if(sptr->right==NULL) {
// cout<<" "<<insdata<<" IS THE RIGHT child of "<<q->word<<endl;
sptr->right= malloc(sizeof(q));
sptr=sptr->right;
sptr->word=insdata;
sptr->count = 1;
sptr->left=NULL;
sptr->right=NULL;
q=node;
}
else {
if(strcmp(insdata,sptr->word) > 0)
{
sptr=sptr->right;
q=sptr;
if(strcmp(insdata,sptr->word) > 0)
createRight();
else
createUp();
}
else {
sptr=sptr->left;
q=sptr;
createRight();
}
}
}

void createLeft()
{
if(sptr->left==NULL)
{
// cout<<" "<<insdata<<" IS THE LEFT child of "<<q->word<<endl;
sptr->left=malloc(sizeof(q));
sptr=sptr->left;
sptr->word=insdata;
sptr->count = 1;
sptr->right=NULL;
sptr->left=NULL;
q=node;
}
else
{
if(strcmp(insdata,sptr->word) < 0)
{
sptr=sptr->left;
q=sptr;
if(strcmp(insdata,sptr->word) > 0)
createRight();
else
createLeft();
}
else
{
sptr=sptr->right;
q=sptr;
createRight();
}
}
}

void search()
{
sptr=node;
while(sptr!=NULL)
{
//This commented section will work when we will have to insert equal
data

if(strcmp(insdata,sptr->word) == 0)
{
printf("\nThis is not insertable");
printf("\nInsert child ");
cin>>insdata;
search();
break;
}
else
{

if(strcmp(insdata,sptr->word) > 0)
sptr=sptr->right;
else
sptr=sptr->left;
// }
}
sptr=node;
}

void createUp()
{
struct tree *temp = malloc(sizeof(node));

temp->word = insdata;
temp->count = 1;
temp->right= sptr;

node = temp;
}

*/

can someone look at it and let me know what i am doing wrong here

Jun 12 '06 #11

P: n/a
ge**********@hotmail.com said:
ok so this is what i got.....but when i try to compile the prg....it
gives me this error
include<conio.h>: no such file or directory
That's because there is no such file or directory. C doesn't define such a
header, so there is no guarantee that any implementation will provide it,
nor that it will mean any particular thing if it /is/ provided.
void main()
main returns int. Always.

When you've learned that, and learned not to use gets(), you'll be ready to
ask some more questions.

<snip>
can someone look at it and let me know what i am doing wrong here


I think your biggest mistake is trying to learn C from an inferior book. My
best guess is Herbert Schildt - your program seems to be full of his kind
of nonsense.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 12 '06 #12

P: n/a
<ge**********@hotmail.com> wrote:
ok so this is what i got.....but when i try to compile the prg....it
gives me this error
include<conio.h>: no such file or directory
here is my program

#include<string.h>
#include<conio.h>


conio is not part of standard C. Take this out and a bunch of errors will
crop up where you have used stuff from conio.The ones I saw were probably
nice, but not really needed. clrscr() is a prettifier, eliminate it.
getch() can be replaced by something from stdio, probably by getchar().
Take this stuff out until you see no more errors caused by rewmoval of the
conio header. Then fix what comes next or post a new version of your code.

It would have been better if you had got some of it working before doing all
that typing. But what's done is done.

<big snip>
Jun 12 '06 #13

P: n/a
ge**********@hotmail.com wrote:
ok so this is what i got.....but when i try to compile the prg....it
gives me this error
include<conio.h>: no such file or directory
here is my program
That's an implementation-specific header. If you don't have it, there's
not much you can do. There are some similar things like ncurses that
are more commonly found, but are still non-standard from the standpoint
of this group.
clrscr();
Get rid of this, you don't need to clear the screen.

getch();
Get rid of all instances of this. You probably don't need a "pause
until continue signal", if you really can justify it we can offer some
standard replacements. It's generally the sign of a newbie to have
those.
printf("\n\t\t| Press your choise (l,s,q)\t|"); ch = getche();
switch(ch)


This is trickier. The best idea is to not use a one-key approach to
menus. Read a line of input into a string with fgets() or get the first
char with getchar() and discard the rest.


Brian
Jun 12 '06 #14

P: n/a
ok so i made some changes and i got this
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void createUp();
void createDown();
void showMenu();
void createRight();
void createLeft();
void search();
void listWords();
void searchWord();
int createBinaryTree();
long fileSize(FILE *);

struct tree
{
char *word; //The word to be stored in a node
// struct tree *left; //Left child of the node
struct tree *right; //Right child of the node
int count; //How many of these words are found by now
};

FILE *fp; //File Pointer
struct tree *node;
struct tree *sptr;
struct tree *q;
char *insdata;
char *fileName;

int main(void)
{
//Create the Binary Search Tree

if (createBinaryTree() == 0) //Show Menu
{
showMenu();
}
}

void listWords()
{
struct tree *temp;
temp = (struct tree *)malloc(sizeof(struct tree));
temp = node;

while(temp->right!=NULL)
{
printf("\n\tWord:\t%s\t\t\t\tCount:\t%d",temp->word,temp->count);
temp=temp->right;
}
temp = node;
getchar();
free(temp);
}

void showMenu()
{
char ch = 'a';
while(ch!='q')
{
clrscr();
printf("\n\t\t---------------------------------");
printf("\n\t\t| Binary Search Tree |");
printf("\n\t\t---------------------------------");
printf("\n\t\t|\tl - List all words\t|");
printf("\n\t\t|\ts - Search a words\t|");
printf("\n\t\t|\tq - Quit\t\t|");
printf("\n\t\t---------------------------------");
printf("\n\t\t| Press your choise (l,s,q)\t|");
printf("\n\t\t---------------------------------\n\t\t\t\t");
ch = getche();
switch(ch)
{
case 'l':
sptr=node;
listWords();
break;
case 's':
sptr=node;
searchWord();
break;
case 'q':
free(node);
free(sptr);
free(q);
default:
break;
}
}
}

void searchWord()
{
int check=1;
char *searchWord;
printf("\n\n\t\tPlease enter the word to search: ");
gets(searchWord);
while(sptr->right !=NULL)
{
if (strcmp(searchWord,sptr->word) == 0) {
printf("\n\n\t\tWord Occurances found:%d\n",sptr->count);
getchar();
check = 0;
break;
}
sptr=sptr->right; //Send sptr to its right
}
if (strcmp(searchWord,sptr->word) == 0 && check !=0) {
printf("\n\n\t\tWord Occurances found:%d\n",(*sptr).count);
getchar();
check = 0;
}

if(check ==1) {
printf("\n\n\t\tNo occurances of this word found.");
getchar();
}
}

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 : ");
gets(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;

// loop until finishied
while (true)
{
check = 0;
// extract string from string sequence
token = strtok(NULL, " ");

if (token == NULL)
{
true = 0; //Break the loop
}

insdata = token;
// printf("\nCounter %d\n",counter);
// puts(insdata);
counter++;

sptr= q;

while(sptr->right !=NULL && check==0)
{
if (strcmp(insdata,sptr->word) == 0) {
(*sptr).count++; //Increment
// printf("%d",(*sptr).count);
check=1;
}
sptr=sptr->right; //Send sptr to its right
}

if (strcmp(insdata,sptr->word) == 0) {
(*sptr).count++; //Increment
// printf("%d",(*sptr).count);
check=1;
}

if (check !=1) {
createDown();
}
sptr = node;
printf("%d",true);
}
return 0;
}
}
}

long fileSize(FILE *stream)
{
long curpos, length;
curpos = ftell(stream);
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
fseek(stream, curpos, SEEK_SET);
return length;
}

void createDown()
{

struct tree *temp;
temp = (struct tree *)malloc(sizeof(struct tree));

temp->word = insdata;
(*temp).count = 1;
temp->right= NULL;

sptr->right=temp;
}
/*
void createRight()
{
if(sptr->right==NULL) {
// cout<<" "<<insdata<<" IS THE RIGHT child of "<<q->word<<endl;
sptr->right= malloc(sizeof(q));
sptr=sptr->right;
sptr->word=insdata;
sptr->count = 1;
sptr->left=NULL;
sptr->right=NULL;
q=node;
}
else {
if(strcmp(insdata,sptr->word) > 0)
{
sptr=sptr->right;
q=sptr;
if(strcmp(insdata,sptr->word) > 0)
createRight();
else
createUp();
}
else {
sptr=sptr->left;
q=sptr;
createRight();
}
}
}

void createLeft()
{
if(sptr->left==NULL)
{
// cout<<" "<<insdata<<" IS THE LEFT child of "<<q->word<<endl;
sptr->left=malloc(sizeof(q));
sptr=sptr->left;
sptr->word=insdata;
sptr->count = 1;
sptr->right=NULL;
sptr->left=NULL;
q=node;
}
else
{
if(strcmp(insdata,sptr->word) < 0)
{
sptr=sptr->left;
q=sptr;
if(strcmp(insdata,sptr->word) > 0)
createRight();
else
createLeft();
}
else
{
sptr=sptr->right;
q=sptr;
createRight();
}
}
}

void search()
{
sptr=node;
while(sptr!=NULL)
{
//This commented section will work when we will have to insert equal
data

if(strcmp(insdata,sptr->word) == 0)
{
printf("\nThis is not insertable");
printf("\nInsert child ");
cin>>insdata;
search();
break;
}
else
{

if(strcmp(insdata,sptr->word) > 0)
sptr=sptr->right;
else
sptr=sptr->left;
// }
}
sptr=node;
}

void createUp()
{
struct tree *temp = malloc(sizeof(node));

temp->word = insdata;
temp->count = 1;
temp->right= sptr;

node = temp;
}

*/
now when i compile it i get these errors!
/tmp/ccSZ3XYi.o(.text+0x1ed): In function `searchWord':
: the `gets' function is dangerous and should not be used.
/tmp/ccSZ3XYi.o(.text+0xa2): In function `showMenu':
: undefined reference to `clrscr'
/tmp/ccSZ3XYi.o(.text+0x137): In function `showMenu':
: undefined reference to `getche'
/tmp/ccSZ3XYi.o(.text+0x350): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x36d): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x38a): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x3a7): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x3c4): In function `createBinaryTree':
: undefined reference to `delay'
collect2: ld returned 1 exit status

Default User wrote:
ge**********@hotmail.com wrote:
ok so this is what i got.....but when i try to compile the prg....it
gives me this error
include<conio.h>: no such file or directory
here is my program


That's an implementation-specific header. If you don't have it, there's
not much you can do. There are some similar things like ncurses that
are more commonly found, but are still non-standard from the standpoint
of this group.
clrscr();


Get rid of this, you don't need to clear the screen.

getch();


Get rid of all instances of this. You probably don't need a "pause
until continue signal", if you really can justify it we can offer some
standard replacements. It's generally the sign of a newbie to have
those.
printf("\n\t\t| Press your choise (l,s,q)\t|");

ch = getche();
switch(ch)


This is trickier. The best idea is to not use a one-key approach to
menus. Read a line of input into a string with fgets() or get the first
char with getchar() and discard the rest.


Brian


Jun 12 '06 #15

P: n/a
<ge**********@hotmail.com> wrote:

<snip>
now when i compile it i get these errors!
/tmp/ccSZ3XYi.o(.text+0x1ed): In function `searchWord':
: the `gets' function is dangerous and should not be used.
ignore the warning. gets is not bulletproof but this is just a student
program.
/tmp/ccSZ3XYi.o(.text+0xa2): In function `showMenu':
: undefined reference to `clrscr'
Take it out. You don't need it.
/tmp/ccSZ3XYi.o(.text+0x137): In function `showMenu':
: undefined reference to `getche'
This will be hard to resolve. Put in some way to proceed without solving the
problem. There is a lot of work ahead that is much more important than
getting this right. Replace it with getchar(), for now.
/tmp/ccSZ3XYi.o(.text+0x350): In function `createBinaryTree':
: undefined reference to `delay'
Take it out. Do you *really* need that? If you do someone can suggest a
workaround.
/tmp/ccSZ3XYi.o(.text+0x36d): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x38a): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x3a7): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x3c4): In function `createBinaryTree':
: undefined reference to `delay'
collect2: ld returned 1 exit status


Same thing repeated n times, same cure. Now you may be getting close to
seeing your first *real* error.
Jun 12 '06 #16

P: n/a
ok so u want me to take out 'seachWord' and 'showMenu'?

osmium wrote:
<ge**********@hotmail.com> wrote:

<snip>
now when i compile it i get these errors!
/tmp/ccSZ3XYi.o(.text+0x1ed): In function `searchWord':
: the `gets' function is dangerous and should not be used.


ignore the warning. gets is not bulletproof but this is just a student
program.
/tmp/ccSZ3XYi.o(.text+0xa2): In function `showMenu':
: undefined reference to `clrscr'


Take it out. You don't need it.
/tmp/ccSZ3XYi.o(.text+0x137): In function `showMenu':
: undefined reference to `getche'


This will be hard to resolve. Put in some way to proceed without solving the
problem. There is a lot of work ahead that is much more important than
getting this right. Replace it with getchar(), for now.
/tmp/ccSZ3XYi.o(.text+0x350): In function `createBinaryTree':
: undefined reference to `delay'


Take it out. Do you *really* need that? If you do someone can suggest a
workaround.
/tmp/ccSZ3XYi.o(.text+0x36d): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x38a): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x3a7): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x3c4): In function `createBinaryTree':
: undefined reference to `delay'
collect2: ld returned 1 exit status


Same thing repeated n times, same cure. Now you may be getting close to
seeing your first *real* error.


Jun 12 '06 #17

P: n/a
ge**********@hotmail.com writes:
ok so i made some changes and i got this
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
No implementation-defined headers, good.

[snip] now when i compile it i get these errors!
/tmp/ccSZ3XYi.o(.text+0x1ed): In function `searchWord':
: the `gets' function is dangerous and should not be used.
/tmp/ccSZ3XYi.o(.text+0xa2): In function `showMenu':
: undefined reference to `clrscr'
/tmp/ccSZ3XYi.o(.text+0x137): In function `showMenu':
: undefined reference to `getche'
/tmp/ccSZ3XYi.o(.text+0x350): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x36d): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x38a): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x3a7): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x3c4): In function `createBinaryTree':
: undefined reference to `delay'
collect2: ld returned 1 exit status


Pay attention to those errors. Don't call gets(); it's standard, but
dangerous. See question 12.23 in the comp.lang.c FAQ
<http://c-faq.com/>.

The clrscr(), getche(), and delay() functions are non-standard, and
you don't really need any of them.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 12 '06 #18

P: n/a
Ok i think i am lost now.....the program is takin up so much of my
energy. whatever you guys are saying here is just going over my head
now!

Keith Thompson wrote:
ge**********@hotmail.com writes:
ok so i made some changes and i got this
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>


No implementation-defined headers, good.

[snip]
now when i compile it i get these errors!
/tmp/ccSZ3XYi.o(.text+0x1ed): In function `searchWord':
: the `gets' function is dangerous and should not be used.
/tmp/ccSZ3XYi.o(.text+0xa2): In function `showMenu':
: undefined reference to `clrscr'
/tmp/ccSZ3XYi.o(.text+0x137): In function `showMenu':
: undefined reference to `getche'
/tmp/ccSZ3XYi.o(.text+0x350): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x36d): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x38a): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x3a7): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x3c4): In function `createBinaryTree':
: undefined reference to `delay'
collect2: ld returned 1 exit status


Pay attention to those errors. Don't call gets(); it's standard, but
dangerous. See question 12.23 in the comp.lang.c FAQ
<http://c-faq.com/>.

The clrscr(), getche(), and delay() functions are non-standard, and
you don't really need any of them.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Jun 12 '06 #19

P: n/a
"osmium" <r1********@comcast.net> writes:
<ge**********@hotmail.com> wrote:

<snip>
now when i compile it i get these errors!
/tmp/ccSZ3XYi.o(.text+0x1ed): In function `searchWord':
: the `gets' function is dangerous and should not be used.


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


Sorry, but that's bad advice. gets(), for all practical purposes,
cannot be used safely, and the best time to learn that is when you're
a student. Use fgets() instead (and deal with the '\n' character).

Also, the code looks like this:

[...]
char *searchWord;
printf("\n\n\t\tPlease enter the word to search: ");
gets(searchWord);
[...]

*No* memory has been allocated; the searchWord pointer is garbage.
Even fgets() would be unsafe if used like this. This is exactly the
situation described in question 7.1 of the comp.lang.c FAQ,
<http://www.c-faq.com/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 12 '06 #20

P: n/a
ge**********@hotmail.com writes:
ok so u want me to take out 'seachWord' and 'showMenu'?


Please don't top-post. See <http://www.caliburn.nl/topposting.html>.

seachWord and showMenu are your functions. You need to remove your
calls to clrscr, getche, and delay.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 12 '06 #21

P: n/a
ge**********@hotmail.com writes:
ok so u want me to take out 'seachWord' and 'showMenu'?


Take the time to spell out words; it's "you" not "u". Silly
abbreviations like that might be appropriate in IRC or SMS, but not
here. They just make it more difficult to read what you write.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 12 '06 #22

P: n/a

Don't top-post, quotes rearranged.

ge**********@hotmail.com wrote:
Default User wrote:
clrscr();


Get rid of this, you don't need to clear the screen.

getch();


Get rid of all instances of this. You probably don't need a "pause
until continue signal", if you really can justify it we can offer
some standard replacements. It's generally the sign of a newbie to
have those.
printf("\n\t\t| Press your choise (l,s,q)\t|");

ch = getche();
switch(ch)

ok so i made some changes and i got this
now when i compile it i get these errors!
/tmp/ccSZ3XYi.o(.text+0x1ed): In function `searchWord':
: the `gets' function is dangerous and should not be used.
/tmp/ccSZ3XYi.o(.text+0xa2): In function `showMenu':
: undefined reference to `clrscr'
/tmp/ccSZ3XYi.o(.text+0x137): In function `showMenu':
: undefined reference to `getche'
/tmp/ccSZ3XYi.o(.text+0x350): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x36d): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x38a): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x3a7): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x3c4): In function `createBinaryTree':
: undefined reference to `delay'
collect2: ld returned 1 exit status

Had you bothered to read what I took the good time and trouble to post,
you'd have seen that things like clrscr are non-standard and
(apparently) unavailable to you. I don't know what delay() is either,
but what do you need it for? Get rid of the non-standard crap.

Brian
Jun 12 '06 #23

P: n/a
ok so i took some stuff out
here is what i got now......

#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void createUp();
void createDown();
void showMenu();
void createRight();
void createLeft();
void search();
void listWords();
void searchWord();
int createBinaryTree();
long fileSize(FILE *);

struct tree
{
char *word; //The word to be stored in a node
// struct tree *left; //Left child of the node
struct tree *right; //Right child of the node
int count; //How many of these words are found by now
};

FILE *fp; //File Pointer
struct tree *node;
struct tree *sptr;
struct tree *q;
char *insdata;
char *fileName;

int main(void)
{
//Create the Binary Search Tree

if (createBinaryTree() == 0) //Show Menu
{
showMenu();
}
}

void listWords()
{
struct tree *temp;
temp = (struct tree *)malloc(sizeof(struct tree));
temp = node;

while(temp->right!=NULL)
{
printf("\n\tWord:\t%s\t\t\t\tCount:\t%d",temp->word,temp->count);
temp=temp->right;
}
temp = node;
getchar();
free(temp);
}

void showMenu()
{
char ch = 'a';
while(ch!='q')
{

printf("\n\t\t---------------------------------");
printf("\n\t\t| Binary Search Tree |");
printf("\n\t\t---------------------------------");
printf("\n\t\t|\tl - List all words\t|");
printf("\n\t\t|\ts - Search a words\t|");
printf("\n\t\t|\tq - Quit\t\t|");
printf("\n\t\t---------------------------------");
printf("\n\t\t| Press your choise (l,s,q)\t|");
printf("\n\t\t---------------------------------\n\t\t\t\t");
ch = getchar();
switch(ch)
{
case 'l':
sptr=node;
listWords();
break;
case 's':
sptr=node;
searchWord();
break;
case 'q':
free(node);
free(sptr);
free(q);
default:
break;
}
}
}

void searchWord()
{
int check=1;
char *searchWord;
printf("\n\n\t\tPlease enter the word to search: ");
// gets(searchWord);
while(sptr->right !=NULL)
{
if (strcmp(searchWord,sptr->word) == 0) {
printf("\n\n\t\tWord Occurances found:%d\n",sptr->count);
getchar();
check = 0;
break;
}
sptr=sptr->right; //Send sptr to its right
}
if (strcmp(searchWord,sptr->word) == 0 && check !=0) {
printf("\n\n\t\tWord Occurances found:%d\n",(*sptr).count);
getchar();
check = 0;
}

if(check ==1) {
printf("\n\n\t\tNo occurances of this word found.");
getchar();
}
}

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 : ");
// gets(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;

// loop until finishied
while (true)
{
check = 0;
// extract string from string sequence
token = strtok(NULL, " ");

if (token == NULL)
{
true = 0; //Break the loop
}

insdata = token;
// printf("\nCounter %d\n",counter);
// puts(insdata);
counter++;

sptr= q;

while(sptr->right !=NULL && check==0)
{
if (strcmp(insdata,sptr->word) == 0) {
(*sptr).count++; //Increment
// printf("%d",(*sptr).count);
check=1;
}
sptr=sptr->right; //Send sptr to its right
}

if (strcmp(insdata,sptr->word) == 0) {
(*sptr).count++; //Increment
// printf("%d",(*sptr).count);
check=1;
}

if (check !=1) {
createDown();
}
sptr = node;
printf("%d",true);
}
return 0;
}
}
}

long fileSize(FILE *stream)
{
long curpos, length;
curpos = ftell(stream);
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
fseek(stream, curpos, SEEK_SET);
return length;
}

void createDown()
{

struct tree *temp;
temp = (struct tree *)malloc(sizeof(struct tree));

temp->word = insdata;
(*temp).count = 1;
temp->right= NULL;

sptr->right=temp;
}
/*
void createRight()
{
if(sptr->right==NULL) {
// cout<<" "<<insdata<<" IS THE RIGHT child of "<<q->word<<endl;
sptr->right= malloc(sizeof(q));
sptr=sptr->right;
sptr->word=insdata;
sptr->count = 1;
sptr->left=NULL;
sptr->right=NULL;
q=node;
}
else {
if(strcmp(insdata,sptr->word) > 0)
{
sptr=sptr->right;
q=sptr;
if(strcmp(insdata,sptr->word) > 0)
createRight();
else
createUp();
}
else {
sptr=sptr->left;
q=sptr;
createRight();
}
}
}

void createLeft()
{
if(sptr->left==NULL)
{
// cout<<" "<<insdata<<" IS THE LEFT child of "<<q->word<<endl;
sptr->left=malloc(sizeof(q));
sptr=sptr->left;
sptr->word=insdata;
sptr->count = 1;
sptr->right=NULL;
sptr->left=NULL;
q=node;
}
else
{
if(strcmp(insdata,sptr->word) < 0)
{
sptr=sptr->left;
q=sptr;
if(strcmp(insdata,sptr->word) > 0)
createRight();
else
createLeft();
}
else
{
sptr=sptr->right;
q=sptr;
createRight();
}
}
}

void search()
{
sptr=node;
while(sptr!=NULL)
{
//This commented section will work when we will have to insert equal
data

if(strcmp(insdata,sptr->word) == 0)
{
printf("\nThis is not insertable");
printf("\nInsert child ");
cin>>insdata;
search();
break;
}
else
{

if(strcmp(insdata,sptr->word) > 0)
sptr=sptr->right;
else
sptr=sptr->left;
// }
}
sptr=node;
}

void createUp()
{
struct tree *temp = malloc(sizeof(node));

temp->word = insdata;
temp->count = 1;
temp->right= sptr;

node = temp;
}

*/
now when i compile it.....it doesn't give me any errors.....and it runs
too
but i get this
[bmhm0008@munro ctec202]$ tuo

---------------------------------------
------- Frequency of Words ---------
---------------------------------------

Please enter the text file name :
---------------------------------------

Reading file....

Error reading file.

thats it.......it doesnt do anything after that
Default User wrote:
Don't top-post, quotes rearranged.

ge**********@hotmail.com wrote:
Default User wrote:

> clrscr();

Get rid of this, you don't need to clear the screen.
> getch();

Get rid of all instances of this. You probably don't need a "pause
until continue signal", if you really can justify it we can offer
some standard replacements. It's generally the sign of a newbie to
have those.

> printf("\n\t\t| Press your choise (l,s,q)\t|");

> ch = getche();
> switch(ch)

ok so i made some changes and i got this


now when i compile it i get these errors!
/tmp/ccSZ3XYi.o(.text+0x1ed): In function `searchWord':
: the `gets' function is dangerous and should not be used.
/tmp/ccSZ3XYi.o(.text+0xa2): In function `showMenu':
: undefined reference to `clrscr'
/tmp/ccSZ3XYi.o(.text+0x137): In function `showMenu':
: undefined reference to `getche'
/tmp/ccSZ3XYi.o(.text+0x350): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x36d): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x38a): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x3a7): In function `createBinaryTree':
: undefined reference to `delay'
/tmp/ccSZ3XYi.o(.text+0x3c4): In function `createBinaryTree':
: undefined reference to `delay'
collect2: ld returned 1 exit status

Had you bothered to read what I took the good time and trouble to post,
you'd have seen that things like clrscr are non-standard and
(apparently) unavailable to you. I don't know what delay() is either,
but what do you need it for? Get rid of the non-standard crap.

Brian


Jun 12 '06 #24

P: n/a
<ge**********@hotmail.com> wrote:

ok so i took some stuff out
here is what i got now......

#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void createUp();
void createDown();
void showMenu();
void createRight();
void createLeft();
void search();
void listWords();
void searchWord();
int createBinaryTree();
long fileSize(FILE *);

struct tree
{
char *word; //The word to be stored in a node
// struct tree *left; //Left child of the node
struct tree *right; //Right child of the node
int count; //How many of these words are found by now
};

FILE *fp; //File Pointer
struct tree *node;
struct tree *sptr;
struct tree *q;
char *insdata;
//> char *fileName;
char fileName[1000];


int main(void)
{
//Create the Binary Search Tree

if (createBinaryTree() == 0) //Show Menu
{
showMenu();
}
}

void listWords()
{
struct tree *temp;
temp = (struct tree *)malloc(sizeof(struct tree));
temp = node;

while(temp->right!=NULL)
{
printf("\n\tWord:\t%s\t\t\t\tCount:\t%d",temp->word,temp->count);
temp=temp->right;
}
temp = node;
getchar();
free(temp);
}

void showMenu()
{
char ch = 'a';
while(ch!='q')
{

printf("\n\t\t---------------------------------");
printf("\n\t\t| Binary Search Tree |");
printf("\n\t\t---------------------------------");
printf("\n\t\t|\tl - List all words\t|");
printf("\n\t\t|\ts - Search a words\t|");
printf("\n\t\t|\tq - Quit\t\t|");
printf("\n\t\t---------------------------------");
printf("\n\t\t| Press your choise (l,s,q)\t|");
printf("\n\t\t---------------------------------\n\t\t\t\t");
ch = getchar();
switch(ch)
{
case 'l':
sptr=node;
listWords();
break;
case 's':
sptr=node;
searchWord();
break;
case 'q':
free(node);
free(sptr);
free(q);
default:
break;
}
}
}

void searchWord()
{
int check=1;
char *searchWord;
printf("\n\n\t\tPlease enter the word to search: ");
// gets(searchWord);
while(sptr->right !=NULL)
{
if (strcmp(searchWord,sptr->word) == 0) {
printf("\n\n\t\tWord Occurances found:%d\n",sptr->count);
getchar();
check = 0;
break;
}
sptr=sptr->right; //Send sptr to its right
}
if (strcmp(searchWord,sptr->word) == 0 && check !=0) {
printf("\n\n\t\tWord Occurances found:%d\n",(*sptr).count);
getchar();
check = 0;
}

if(check ==1) {
printf("\n\n\t\tNo occurances of this word found.");
getchar();
}
}

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 : ");


gets(fileName);

Try those two changes. I didn't try them.
Jun 12 '06 #25

P: n/a
ge**********@hotmail.com writes:
ok so i took some stuff out
here is what i got now......
[snip] now when i compile it.....it doesn't give me any errors.....and it runs
too
but i get this
[bmhm0008@munro ctec202]$ tuo

---------------------------------------
------- Frequency of Words ---------
---------------------------------------

Please enter the text file name :
---------------------------------------

Reading file....

Error reading file.

thats it.......it doesnt do anything after that


You're still top-posting. You need to place your new text *below*, or
mixed with, any quoted text, and you should snip any quoted text that
isn't relevant to your response. I'll ask you once again to read
<http://www.caliburn.nl/topposting.html>. We don't offer this kind of
advice to be annoying; if you can post properly, we're more likely to
be able (and willing) to help you.

The last 100 lines or so of the code you posted were commented out.
It would have been much better to leave it out entirely, so we don't
waste our time reading it.

You're using "//" comments. They're perfectly legal in C99, and
apparently are supported by your compiler, but they're not legal in
strict C90, and they're not recommended for code you post to Usenet.
It's very easy for line-wrapping to cause part of your comment to
appear on a separate line, causing syntax errors. Old style /* ... */
comments are less likely to cause this problem.

You've commented out the calls to gets(), but you haven't replaced
them with anything. If you don't do anything to read the text file
name, you're not going to be able to do anything with it. Try using
fgets(); see the FAQ for some examples of how to use it. Remember
that you can't use a pointer until you've assigned a value to it
(usually that means allocating memory for it to point to).

I was unable to compile the program until I deleted the delay() calls.
Apparently your system has a non-standard delay() function; mine
doesn't. You're not doing anything useful with it anyway. Here's
the code in question:

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

This is apparently intended to print a series of '.' characters while
giving the *illusion* that it's taking some time to read the file. I
have no idea why you'd want to do that. In fact, the dots are printed
before you even attempt to open the file. Just delete that entire
block of code.

I think you're trying to do too much at once. Try writing a smaller
program that prompts for a file name, reads name from stdin, opens the
named file, and prints the contents to stdout. Once you've got that
working, extend the program to do whatever else you're trying to do.

And if you're given advice on this newsgroup, *pay attention to it*.
I've seen you repeat errors for which people have already given you
solutions. There's always a chance someone may give you bad advice,
but if that happens someone else will correct it very quickly.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 12 '06 #26

P: n/a
ok so i made those changes.........

#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void createUp();
void createDown();
void showMenu();
void createRight();
void createLeft();
void search();
void listWords();
void searchWord();
int createBinaryTree();
long fileSize(FILE *);

struct tree
{
char *word; //The word to be stored in a node
// struct tree *left; //Left child of the node
struct tree *right; //Right child of the node
int count; //How many of these words are found by now
};

FILE *fp; //File Pointer
struct tree *node;
struct tree *sptr;
struct tree *q;
char *insdata;
char fileName[1000];
int main(void)
{
//Create the Binary Search Tree

if (createBinaryTree() == 0) //Show Menu
{
showMenu();
}
}

void listWords()
{
struct tree *temp;
temp = (struct tree *)malloc(sizeof(struct tree));
temp = node;

while(temp->right!=NULL)
{
printf("\n\tWord:\t%s\t\t\t\tCount:\t%d",temp->word,temp->count);
temp=temp->right;
}
temp = node;
getchar();
free(temp);
}

void showMenu()
{
char ch = 'a';
while(ch!='q')
{

printf("\n\t\t---------------------------------");
printf("\n\t\t| Binary Search Tree |");
printf("\n\t\t---------------------------------");
printf("\n\t\t|\tl - List all words\t|");
printf("\n\t\t|\ts - Search a words\t|");
printf("\n\t\t|\tq - Quit\t\t|");
printf("\n\t\t---------------------------------");
printf("\n\t\t| Press your choise (l,s,q)\t|");
printf("\n\t\t---------------------------------\n\t\t\t\t");
ch = getchar();
switch(ch)
{
case 'l':
sptr=node;
listWords();
break;
case 's':
sptr=node;
searchWord();
break;
case 'q':
free(node);
free(sptr);
free(q);
default:
break;
}
}
}

void searchWord()
{
int check=1;
char *searchWord;
printf("\n\n\t\tPlease enter the word to search: ");
// gets(searchWord);
while(sptr->right !=NULL)
{
if (strcmp(searchWord,sptr->word) == 0) {
printf("\n\n\t\tWord Occurances found:%d\n",sptr->count);
getchar();
check = 0;
break;
}
sptr=sptr->right; //Send sptr to its right
}
if (strcmp(searchWord,sptr->word) == 0 && check !=0) {
printf("\n\n\t\tWord Occurances found:%d\n",(*sptr).count);
getchar();
check = 0;
}

if(check ==1) {
printf("\n\n\t\tNo occurances of this word found.");
getchar();
}
}

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 : ");
gets(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;

// loop until finishied
while (true)
{
check = 0;
// extract string from string sequence
token = strtok(NULL, " ");

if (token == NULL)
{
true = 0; //Break the loop
}

insdata = token;
// printf("\nCounter %d\n",counter);
// puts(insdata);
counter++;

sptr= q;

while(sptr->right !=NULL && check==0)
{
if (strcmp(insdata,sptr->word) == 0) {
(*sptr).count++; //Increment
// printf("%d",(*sptr).count);
check=1;
}
sptr=sptr->right; //Send sptr to its right
}

if (strcmp(insdata,sptr->word) == 0) {
(*sptr).count++; //Increment
// printf("%d",(*sptr).count);
check=1;
}

if (check !=1) {
createDown();
}
sptr = node;
printf("%d",true);
}
return 0;
}
}
}

long fileSize(FILE *stream)
{
long curpos, length;
curpos = ftell(stream);
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
fseek(stream, curpos, SEEK_SET);
return length;
}

void createDown()
{

struct tree *temp;
temp = (struct tree *)malloc(sizeof(struct tree));

temp->word = insdata;
(*temp).count = 1;
temp->right= NULL;

sptr->right=temp;
}
/*
void createRight()
{
if(sptr->right==NULL) {
// cout<<" "<<insdata<<" IS THE RIGHT child of "<<q->word<<endl;
sptr->right= malloc(sizeof(q));
sptr=sptr->right;
sptr->word=insdata;
sptr->count = 1;
sptr->left=NULL;
sptr->right=NULL;
q=node;
}
else {
if(strcmp(insdata,sptr->word) > 0)
{
sptr=sptr->right;
q=sptr;
if(strcmp(insdata,sptr->word) > 0)
createRight();
else
createUp();
}
else {
sptr=sptr->left;
q=sptr;
createRight();
}
}
}

void createLeft()
{
if(sptr->left==NULL)
{
// cout<<" "<<insdata<<" IS THE LEFT child of "<<q->word<<endl;
sptr->left=malloc(sizeof(q));
sptr=sptr->left;
sptr->word=insdata;
sptr->count = 1;
sptr->right=NULL;
sptr->left=NULL;
q=node;
}
else
{
if(strcmp(insdata,sptr->word) < 0)
{
sptr=sptr->left;
q=sptr;
if(strcmp(insdata,sptr->word) > 0)
createRight();
else
createLeft();
}
else
{
sptr=sptr->right;
q=sptr;
createRight();
}
}
}

void search()
{
sptr=node;
while(sptr!=NULL)
{
//This commented section will work when we will have to insert equal
data

if(strcmp(insdata,sptr->word) == 0)
{
printf("\nThis is not insertable");
printf("\nInsert child ");
cin>>insdata;
search();
break;
}
else
{

if(strcmp(insdata,sptr->word) > 0)
sptr=sptr->right;
else
sptr=sptr->left;
// }
}
sptr=node;
}

void createUp()
{
struct tree *temp = malloc(sizeof(node));

temp->word = insdata;
temp->count = 1;
temp->right= sptr;

node = temp;
}

*/
and when i complied it......i got this error:

[bmhm0008@munro ctec202]$ gcc tutorial.c -o tuo
/tmp/cc6enaBa.o(.text+0x30c): In function `createBinaryTree':
: the `gets' function is dangerous and should not be used.

but i went ahead and ran the program......and i got the menu and all
and i typed the file name i wanted to search

[bmhm0008@munro ctec202]$ tuo

---------------------------------------
------- Frequency of Words ---------
---------------------------------------

Please enter the text file name : Hello.txt

---------------------------------------

Segmentation fault
osmium wrote:
<ge**********@hotmail.com> wrote:

ok so i took some stuff out
here is what i got now......

#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void createUp();
void createDown();
void showMenu();
void createRight();
void createLeft();
void search();
void listWords();
void searchWord();
int createBinaryTree();
long fileSize(FILE *);

struct tree
{
char *word; //The word to be stored in a node
// struct tree *left; //Left child of the node
struct tree *right; //Right child of the node
int count; //How many of these words are found by now
};

FILE *fp; //File Pointer
struct tree *node;
struct tree *sptr;
struct tree *q;
char *insdata;


//> char *fileName;
char fileName[1000];


int main(void)
{
//Create the Binary Search Tree

if (createBinaryTree() == 0) //Show Menu
{
showMenu();
}
}

void listWords()
{
struct tree *temp;
temp = (struct tree *)malloc(sizeof(struct tree));
temp = node;

while(temp->right!=NULL)
{
printf("\n\tWord:\t%s\t\t\t\tCount:\t%d",temp->word,temp->count);
temp=temp->right;
}
temp = node;
getchar();
free(temp);
}

void showMenu()
{
char ch = 'a';
while(ch!='q')
{

printf("\n\t\t---------------------------------");
printf("\n\t\t| Binary Search Tree |");
printf("\n\t\t---------------------------------");
printf("\n\t\t|\tl - List all words\t|");
printf("\n\t\t|\ts - Search a words\t|");
printf("\n\t\t|\tq - Quit\t\t|");
printf("\n\t\t---------------------------------");
printf("\n\t\t| Press your choise (l,s,q)\t|");
printf("\n\t\t---------------------------------\n\t\t\t\t");
ch = getchar();
switch(ch)
{
case 'l':
sptr=node;
listWords();
break;
case 's':
sptr=node;
searchWord();
break;
case 'q':
free(node);
free(sptr);
free(q);
default:
break;
}
}
}

void searchWord()
{
int check=1;
char *searchWord;
printf("\n\n\t\tPlease enter the word to search: ");
// gets(searchWord);
while(sptr->right !=NULL)
{
if (strcmp(searchWord,sptr->word) == 0) {
printf("\n\n\t\tWord Occurances found:%d\n",sptr->count);
getchar();
check = 0;
break;
}
sptr=sptr->right; //Send sptr to its right
}
if (strcmp(searchWord,sptr->word) == 0 && check !=0) {
printf("\n\n\t\tWord Occurances found:%d\n",(*sptr).count);
getchar();
check = 0;
}

if(check ==1) {
printf("\n\n\t\tNo occurances of this word found.");
getchar();
}
}

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 : ");


gets(fileName);

Try those two changes. I didn't try them.


Jun 13 '06 #27

P: n/a
ge**********@hotmail.com writes:
ok so i made those changes.........

[snip]

And you're still top-posting, and you've still commented out your
calls to gets() without replacing them with anything else, and a big
chunk of code at the end of your program is still commented out.

I understand that you may not have seen my previous response before
you wrote this. Please read what I wrote and try again.

<http://groups.google.com/group/comp.lang.c/msg/dd79217c161e63a9?hl=en&>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 13 '06 #28

P: n/a

Keith Thompson wrote:
ge**********@hotmail.com writes:
ok so i took some stuff out
here is what i got now......

[snip]
now when i compile it.....it doesn't give me any errors.....and it runs
too
but i get this
[bmhm0008@munro ctec202]$ tuo

---------------------------------------
------- Frequency of Words ---------
---------------------------------------

Please enter the text file name :
---------------------------------------

Reading file....

Error reading file.

thats it.......it doesnt do anything after that


You're still top-posting. You need to place your new text *below*, or
mixed with, any quoted text, and you should snip any quoted text that
isn't relevant to your response. I'll ask you once again to read
<http://www.caliburn.nl/topposting.html>. We don't offer this kind of
advice to be annoying; if you can post properly, we're more likely to
be able (and willing) to help you.

The last 100 lines or so of the code you posted were commented out.
It would have been much better to leave it out entirely, so we don't
waste our time reading it.

You're using "//" comments. They're perfectly legal in C99, and
apparently are supported by your compiler, but they're not legal in
strict C90, and they're not recommended for code you post to Usenet.
It's very easy for line-wrapping to cause part of your comment to
appear on a separate line, causing syntax errors. Old style /* ... */
comments are less likely to cause this problem.

You've commented out the calls to gets(), but you haven't replaced
them with anything. If you don't do anything to read the text file
name, you're not going to be able to do anything with it. Try using
fgets(); see the FAQ for some examples of how to use it. Remember
that you can't use a pointer until you've assigned a value to it
(usually that means allocating memory for it to point to).

I was unable to compile the program until I deleted the delay() calls.
Apparently your system has a non-standard delay() function; mine
doesn't. You're not doing anything useful with it anyway. Here's
the code in question:

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

This is apparently intended to print a series of '.' characters while
giving the *illusion* that it's taking some time to read the file. I
have no idea why you'd want to do that. In fact, the dots are printed
before you even attempt to open the file. Just delete that entire
block of code.

I think you're trying to do too much at once. Try writing a smaller
program that prompts for a file name, reads name from stdin, opens the
named file, and prints the contents to stdout. Once you've got that
working, extend the program to do whatever else you're trying to do.

And if you're given advice on this newsgroup, *pay attention to it*.
I've seen you repeat errors for which people have already given you
solutions. There's always a chance someone may give you bad advice,
but if that happens someone else will correct it very quickly.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Hey Keith guess what?!
right now i am too stressed out to even think about your stupid top
posting stuff!
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.

Jun 13 '06 #29

P: n/a
ge**********@hotmail.com writes:
[...]
Hey Keith guess what?!
right now i am too stressed out to even think about your stupid top
posting stuff!
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.


I'm sincerely sorry that you're stressed out.

I have been trying to help you. If you don't want help, don't post
here. If you do want help pay attention to the experts. If you're
going to insult people who are trying to help you, get used to being
ignored.

Take a break and think things over for a while before responding.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 13 '06 #30

P: n/a
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

Keith Thompson wrote:
ge**********@hotmail.com writes:
[...]
Hey Keith guess what?!
right now i am too stressed out to even think about your stupid top
posting stuff!
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.


I'm sincerely sorry that you're stressed out.

I have been trying to help you. If you don't want help, don't post
here. If you do want help pay attention to the experts. If you're
going to insult people who are trying to help you, get used to being
ignored.

Take a break and think things over for a while before responding.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Jun 13 '06 #31

P: n/a
Some people are really just not cut out to be programmers.

BTW:
http://www.dtcc.edu/cs/rfc1855.html
http://oakroadsystems.com/genl/unice.htm
http://www.albion.com/netiquette/
Jun 13 '06 #32

P: n/a

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

BTW:
http://www.dtcc.edu/cs/rfc1855.html
http://oakroadsystems.com/genl/unice.htm
http://www.albion.com/netiquette/


programming is not my major ne ways!...its just a course stuck in my
major and i had to take it!

Jun 13 '06 #33

P: n/a
<ge**********@hotmail.com> wrote:
ok so i made those changes.........

#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void createUp();
void createDown();
void showMenu();
void createRight();
void createLeft();
void search();
void listWords();
void searchWord();
int createBinaryTree();
long fileSize(FILE *);

struct tree
I understand about your stress, but this is *not* a tree.
It is a single node and several such nodes can be used to make a tree.
{
char *word; //The word to be stored in a node
// struct tree *left; //Left child of the node
change to
struct tree* left;
struct tree *right; //Right child of the node
int count; //How many of these words are found by now
};

FILE *fp; //File Pointer
struct tree *node = NULL;
struct tree *sptr = NULL;
struct tree *q = NULL;

char *insdata;
char fileName[1000];
int main(void)
{
//Create the Binary Search Tree

if (createBinaryTree() == 0) //Show Menu
{
showMenu();
}
}

void listWords()
{
struct tree *temp;
temp = (struct tree *)malloc(sizeof(struct tree));
temp = node;

while(temp->right!=NULL)
{
printf("\n\tWord:\t%s\t\t\t\tCount:\t%d",temp->word,temp->count);
temp=temp->right;
}
temp = node;
getchar();
free(temp);
}

void showMenu()
{
char ch = 'a';
while(ch!='q')
{

printf("\n\t\t---------------------------------");
printf("\n\t\t| Binary Search Tree |");
printf("\n\t\t---------------------------------");
printf("\n\t\t|\tl - List all words\t|");
printf("\n\t\t|\ts - Search a words\t|");
printf("\n\t\t|\tq - Quit\t\t|");
printf("\n\t\t---------------------------------");
printf("\n\t\t| Press your choise (l,s,q)\t|");
printf("\n\t\t---------------------------------\n\t\t\t\t");
ch = getchar();
switch(ch)
{
case 'l':
sptr=node;
listWords();
break;
case 's':
sptr=node;
searchWord();
break;
case 'q':
free(node);
free(sptr);
free(q);
default:
break;
}
}
}

void searchWord()
{
int check=1;
char *searchWord;
printf("\n\n\t\tPlease enter the word to search: ");
// gets(searchWord);
while(sptr->right !=NULL)
{
if (strcmp(searchWord,sptr->word) == 0) {
printf("\n\n\t\tWord Occurances found:%d\n",sptr->count);
getchar();
check = 0;
break;
}
sptr=sptr->right; //Send sptr to its right
}
if (strcmp(searchWord,sptr->word) == 0 && check !=0) {
printf("\n\n\t\tWord Occurances found:%d\n",(*sptr).count);
getchar();
check = 0;
}

if(check ==1) {
printf("\n\n\t\tNo occurances of this word found.");
getchar();
}
}

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 : ");
gets(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
That seems designed to read one line and then stop. Text files are made up
of many lines. In any event, print a message here to see if you got this
far. Liberally sprinkle "progress" messages in the path where you think the
damned thing should go. They don't have to actually *mean* anything, e.g.,

printf("aaa");
....
printf("bbb");

and so on, are fine.
I guess the next bit of code is to isolate a word. For a first cut use
this definition of a word: A word is a group of 1 or more contiguous
letters. The other stuff is space, '\n', punctuation, tabs, digits, ....
You can refine this later. (For example, the hyphen should perhaps not
break the sequence) I have the feeling that you are only looking for spaces
to terminate a word. The functions tolower() and islower() in <ctype.h>
that I encouraged you to use in a prior post would be helpful.

token = strtok(fd," ");
if (token == NULL) {
printf("\n\n\t\tThe text file is empty.");
true = 0; //So that loop is not entered
true is an unfortunate name. So is false. These have established meanings
in C++ and, I think, C99.

getchar();
return 1; //Don't show the menu
See how nasty this interaction stuff is? Here we are, in a function called
create_tree and we are concerned with some damned menu thingy!
}
else {
node = (struct tree *)malloc(sizeof(struct tree));
(*node).word = token;
(*node).count = 1;
node->right = NULL;

sptr=node;
q=node;

// loop until finishied
while (true)
{
check = 0;
// extract string from string sequence
token = strtok(NULL, " ");

if (token == NULL)
{
true = 0; //Break the loop
}

insdata = token;
// printf("\nCounter %d\n",counter);
// puts(insdata);
counter++;

sptr= q;

while(sptr->right !=NULL && check==0)
{
if (strcmp(insdata,sptr->word) == 0) {
(*sptr).count++; //Increment
// printf("%d",(*sptr).count);
check=1;
}
sptr=sptr->right; //Send sptr to its right
}

if (strcmp(insdata,sptr->word) == 0) {
(*sptr).count++; //Increment
// printf("%d",(*sptr).count);
check=1;
}

if (check !=1) {
createDown();
}
sptr = node;
printf("%d",true);
}
return 0;
}
}
}

long fileSize(FILE *stream)
{
long curpos, length;
curpos = ftell(stream);
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
fseek(stream, curpos, SEEK_SET);
return length;
}


<snip, arbitrary point>
Jun 13 '06 #34

P: n/a
<ge**********@hotmail.com> wrote in message
news:11*********************@y43g2000cwc.googlegro ups.com...

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

BTW:
http://www.dtcc.edu/cs/rfc1855.html
http://oakroadsystems.com/genl/unice.htm
http://www.albion.com/netiquette/


programming is not my major ne ways!...its just a course stuck in my
major and i had to take it!


The HTML links provided will be of great service to you if you should choose
to read the contents.

It appears that you have been put into a difficult position. You are taking
a course you did not want to take and you refuse to accept expert help when
offered. As I see it, there is no solution for you.

One of the things I enjoyed most about college was learning how to solve
problems that were initially beyond my reach. I did discover that when
offered a ladder, if I at least attempted to climb it, sometimes I made my
way out of the hole.
Jun 13 '06 #35

P: n/a

osmium wrote:
<ge**********@hotmail.com> wrote:
ok so i made those changes.........

#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void createUp();
void createDown();
void showMenu();
void createRight();
void createLeft();
void search();
void listWords();
void searchWord();
int createBinaryTree();
long fileSize(FILE *);

struct tree


I understand about your stress, but this is *not* a tree.
It is a single node and several such nodes can be used to make a tree.
{
char *word; //The word to be stored in a node
// struct tree *left; //Left child of the node


change to
struct tree* left;
struct tree *right; //Right child of the node
int count; //How many of these words are found by now
};

FILE *fp; //File Pointer


struct tree *node = NULL;
struct tree *sptr = NULL;
struct tree *q = NULL;

char *insdata;
char fileName[1000];
int main(void)
{
//Create the Binary Search Tree

if (createBinaryTree() == 0) //Show Menu
{
showMenu();
}
}

void listWords()
{
struct tree *temp;
temp = (struct tree *)malloc(sizeof(struct tree));
temp = node;

while(temp->right!=NULL)
{
printf("\n\tWord:\t%s\t\t\t\tCount:\t%d",temp->word,temp->count);
temp=temp->right;
}
temp = node;
getchar();
free(temp);
}

void showMenu()
{
char ch = 'a';
while(ch!='q')
{

printf("\n\t\t---------------------------------");
printf("\n\t\t| Binary Search Tree |");
printf("\n\t\t---------------------------------");
printf("\n\t\t|\tl - List all words\t|");
printf("\n\t\t|\ts - Search a words\t|");
printf("\n\t\t|\tq - Quit\t\t|");
printf("\n\t\t---------------------------------");
printf("\n\t\t| Press your choise (l,s,q)\t|");
printf("\n\t\t---------------------------------\n\t\t\t\t");
ch = getchar();
switch(ch)
{
case 'l':
sptr=node;
listWords();
break;
case 's':
sptr=node;
searchWord();
break;
case 'q':
free(node);
free(sptr);
free(q);
default:
break;
}
}
}

void searchWord()
{
int check=1;
char *searchWord;
printf("\n\n\t\tPlease enter the word to search: ");
// gets(searchWord);
while(sptr->right !=NULL)
{
if (strcmp(searchWord,sptr->word) == 0) {
printf("\n\n\t\tWord Occurances found:%d\n",sptr->count);
getchar();
check = 0;
break;
}
sptr=sptr->right; //Send sptr to its right
}
if (strcmp(searchWord,sptr->word) == 0 && check !=0) {
printf("\n\n\t\tWord Occurances found:%d\n",(*sptr).count);
getchar();
check = 0;
}

if(check ==1) {
printf("\n\n\t\tNo occurances of this word found.");
getchar();
}
}

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 : ");
gets(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


That seems designed to read one line and then stop. Text files are made up
of many lines. In any event, print a message here to see if you got this
far. Liberally sprinkle "progress" messages in the path where you think the
damned thing should go. They don't have to actually *mean* anything, e.g.,

printf("aaa");
...
printf("bbb");

and so on, are fine.
I guess the next bit of code is to isolate a word. For a first cut use
this definition of a word: A word is a group of 1 or more contiguous
letters. The other stuff is space, '\n', punctuation, tabs, digits, ....
You can refine this later. (For example, the hyphen should perhaps not
break the sequence) I have the feeling that you are only looking for spaces
to terminate a word. The functions tolower() and islower() in <ctype.h>
that I encouraged you to use in a prior post would be helpful.

token = strtok(fd," ");
if (token == NULL) {
printf("\n\n\t\tThe text file is empty.");
true = 0; //So that loop is not entered


true is an unfortunate name. So is false. These have established meanings
in C++ and, I think, C99.

getchar();
return 1; //Don't show the menu


See how nasty this interaction stuff is? Here we are, in a function called
create_tree and we are concerned with some damned menu thingy!
}
else {
node = (struct tree *)malloc(sizeof(struct tree));
(*node).word = token;
(*node).count = 1;
node->right = NULL;

sptr=node;
q=node;

// loop until finishied
while (true)
{
check = 0;
// extract string from string sequence
token = strtok(NULL, " ");

if (token == NULL)
{
true = 0; //Break the loop
}

insdata = token;
// printf("\nCounter %d\n",counter);
// puts(insdata);
counter++;

sptr= q;

while(sptr->right !=NULL && check==0)
{
if (strcmp(insdata,sptr->word) == 0) {
(*sptr).count++; //Increment
// printf("%d",(*sptr).count);
check=1;
}
sptr=sptr->right; //Send sptr to its right
}

if (strcmp(insdata,sptr->word) == 0) {
(*sptr).count++; //Increment
// printf("%d",(*sptr).count);
check=1;
}

if (check !=1) {
createDown();
}
sptr = node;
printf("%d",true);
}
return 0;
}
}
}

long fileSize(FILE *stream)
{
long curpos, length;
curpos = ftell(stream);
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
fseek(stream, curpos, SEEK_SET);
return length;
}


<snip, arbitrary point>

ok so what do u suggest me using it instead of true = 0 ?
i also made the changes you suggested.....

Jun 13 '06 #36

P: n/a
<ge**********@hotmail.com> wrote in message
news:11*********************@c74g2000cwc.googlegro ups.com...
[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.
Jun 13 '06 #37

P: n/a
"Dann Corbit" <dc*****@connx.com> wrote in message
news:e6**********@nntp.aioe.org...
<ge**********@hotmail.com> wrote in message
news:11*********************@c74g2000cwc.googlegro ups.com...
[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.


Another very popular method is to use enums:

typdef enum tag_Boolean_type {False=0, True=1} Boolean_type;

Then you can just declare one and use it.

It is also very counter-intuitive to set true to 0.
Think about what this would do:

if (true) {
/* Do something */
}
else
{
/* Do something else */
}

In a case like that, it is 'something else' that happens instead of
'something'.
Jun 13 '06 #38

P: n/a

Dann Corbit wrote:
<ge**********@hotmail.com> wrote in message
news:11*********************@c74g2000cwc.googlegro ups.com...
[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.


Am I using the fget() command wrong here:

int createBinaryTree()
128 {
129 char fd[1024]; //Textual Data Pointer
130 char *token;
131 int check = 0;
132 int counter = 1;
133 int true = 1;
134
135
printf("\n\t\t---------------------------------------");
136 printf("\n\t\t------- Frequency of Words
---------");
137
printf("\n\t\t---------------------------------------");
138 printf("\n\n\t\tPlease enter the text file name : ");
139 fgets(fileName);
140
141
printf("\n\t\t---------------------------------------");
142 printf("\n\n\t\t\tReading file....");
143
144
145 if ((fp = fopen(fileName, "r")) ==NULL){
146 printf("\n\n\t\t\tError reading file.");
147 getchar();
148 return 1;
149 }
150
151 else {
152
153 fgets(fd, fileSize(fp) + 1, fp);
154 fclose(fp); //Close the file
155
156 token = strtok(fd," ");
157 if (token == NULL) {
158 printf("\n\n\t\tThe text file is
empty.");
159 true = 0; //So that loop is not
entered
160 getchar();
161 return 1; //Don't show the menu
162 }

Jun 13 '06 #39

P: n/a
<ge**********@hotmail.com> wrote:
ok so what do u suggest me using it instead of true = 0 ?
I suggest you use truex
i also made the changes you suggested.....


So how far did it get, what test messages did you see?
Jun 13 '06 #40

P: n/a

osmium wrote:
<ge**********@hotmail.com> wrote:
ok so what do u suggest me using it instead of true = 0 ?


I suggest you use truex
i also made the changes you suggested.....


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.

Jun 13 '06 #41

P: n/a

osmium wrote:
<ge**********@hotmail.com> wrote:
ok so what do u suggest me using it instead of true = 0 ?


I suggest you use truex
i also made the changes you suggested.....


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.

Jun 13 '06 #42

P: n/a
<ge**********@hotmail.com> wrote in message
news:11**********************@f6g2000cwb.googlegro ups.com...
[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 */
}
Jun 13 '06 #43

P: n/a
<ge**********@hotmail.com> wrote:
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;
Jun 13 '06 #44

P: n/a
osmium said:
<ge**********@hotmail.com> wrote:

<snip>
now when i compile it i get these errors!
/tmp/ccSZ3XYi.o(.text+0x1ed): In function `searchWord':
: the `gets' function is dangerous and should not be used.


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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 13 '06 #45

P: n/a
Dann Corbit wrote:
"Dann Corbit" <dc*****@connx.com> wrote in message
news:e6**********@nntp.aioe.org...
[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)

Jun 13 '06 #46

P: n/a
osmium said:
// struct tree *left; //Left child of the node


change to
struct tree* left;


Why?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 13 '06 #47

P: n/a
ge**********@hotmail.com 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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 13 '06 #48

P: n/a
ge**********@hotmail.com writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 13 '06 #49

P: n/a
"Dann Corbit" <dc*****@connx.com> writes:
<ge**********@hotmail.com> wrote in message
news:11*********************@c74g2000cwc.googlegro ups.com...
[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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 13 '06 #50

66 Replies

This discussion thread is closed

Replies have been disabled for this discussion.