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

K

Keith Thompson

osmium said:
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/>.
 
K

Keith Thompson

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.
 
G

genestarwing

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
 
O

osmium

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.
 
K

Keith Thompson

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.
 
G

genestarwing

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

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.
 
K

Keith Thompson

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&>
 
G

genestarwing

Keith said:
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.
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.
 
K

Keith Thompson

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.
 
G

genestarwing

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
 
O

osmium

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>
 
D

Dann Corbit

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.
 
G

genestarwing

osmium said:
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.....
 
D

Dann Corbit

[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.
 
D

Dann Corbit

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.

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'.
 
G

genestarwing

Dann 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.

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 }
 

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
474,184
Messages
2,570,973
Members
47,529
Latest member
JaclynShum

Latest Threads

Top