Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
this c code have the error at this line "while((fscanf(fin,"%c",&letter)=EOF))" when i complier it and that put up a error msg. If anyone could Debug this code, i am very thank you very much.
#include <stdio.h>
#include <stdlib.h>
/* Binary Tree Structure Template */
typedef struct binary_tree
{
char letter;
struct binary_tree *left;
struct binary_tree *right;
} TREE;/* Function declarations */
TREE *fillTree(TREE *);
void insert(char, TREE **);
void menu(TREE *);
void displayInfo();
void inorder(TREE *);
void preorder(TREE *);
void postorder(TREE *);
int search(TREE *, char, int);
void freeTree(TREE *);
/* Begin main function */
void main()
{
TREE *root=NULL;/* Create the root pointer */
root=fillTree(root);/* Fill the tree */
menu(root);/* Pass menu root, and enjoy */
}
/* Begin fillTree function */
TREE *fillTree(TREE *root)
{
FILE *fin=fopen("btree.dat","r"); /* Open data file & create FILE ptr */
char letter;
while((fscanf(fin,"%c",&letter)=EOF))/* Fill tree letter by letter */
insert(letter, &root);
return root;
}
/* Begin insert function */
void insert(char newLetter, TREE **root)
{
TREE *process;
if(*root == NULL){
process = (TREE *)malloc(sizeof(TREE));
if(process!= NULL){
process->letter = newLetter;
process->left = NULL;
process->right = NULL;
*root = process;
}
else
printf("Out of memory, can't insert letter.\n");
}
else{
if(newLetter < (*root)->letter) insert(newLetter, &((*root)->left));
else insert(newLetter, &((*root)->right));
}
}
/* Begin menu function */
void menu(TREE *root)
{
int choice, result, count;
char target, process;
displayInfo();
while((scanf("%d",&choice)!=8)){
switch(choice){
case 1: /* Traverse BST inorder */
puts("");
inorder(root);
displayInfo();
break;
case 2: /* Traverse BST in preorder */
puts("");
preorder(root);
displayInfo();
break;
case 3: /* Traverse BST in postorder */
puts("");
postorder(root);
displayInfo();
break;
case 4: /* Search BST for a node */
count=0;
puts("");
printf("\nEnter target to search for: ");
flushall(); /* Clear input buffer */
scanf("%c",&target);
result=search(root, target, count);
if(result==-1) printf("\nTarget does not exist.");
else
printf("\nTarget %c found in %2d searches.\n", target, result);
displayInfo();
break;
case 5: /* Insert node into BST */
puts("");
printf("\nEnter character to insert into binary search tree: ");
flushall(); /* Clear input buffer */
scanf("%c",&process);
insert(process,&root);
printf("\nThe character %c was inserted.", process);
displayInfo();
break;
case 6: /* Au Revoir! */
printf("\nHave a nice day. Goodbye.");
freeTree(root);
break;
default:/* Let user know they made an invalid choice */
puts("");
printf("Invalid selection\n\n");
displayInfo();
break;
} /* End switch */
}/* End while */
}
/* Begin displayInfo function */
void displayInfo()
{
printf("\n\n");
p
puts(" Binary Search Tree Menu Options ");
p
printf("\n");
printf("\t 1 Display inorder traversal\n");
printf("\t 2 Display preorder traversal\n");
printf("\t 3 Display postorder traversal\n");
printf("\t 4 Search for a given node\n");
printf("\t 5 Insert a node onto the tree\n");
printf("\t 6 Quit program\n");
printf("\n");
printf("Enter your selection: ");
}
/* Begin inorder function */
void inorder(TREE *root)
{
if(root->left!=NULL) inorder(root->left);
printf("%c",root->letter);
if(root->right!=NULL) inorder(root->right);
}
/* Begin preorder function */
void preorder(TREE *root)
{
printf("%c",root->letter);
if(root->left!=NULL) preorder(root->left);
if(root->right!=NULL) preorder(root->right);
}
/* Begin postorder function */
void postorder(TREE *root)
{
if(root->left!=NULL) postorder(root->left);
if(root->right!=NULL) postorder(root->right);
printf("%c",root->letter);
}
/* Begin search function */
int search(TREE *root, char target, int count)
{
if(root==NULL) return -1; /* Target doesn't exist */
count++;
if(root->letter==target) return count;/* Target found */
if(target > root->letter)
return search(root->right, target, count);
if(target < root->letter)
return search(root->left, target, count);
return 007;/* Bond, James Bond */
}
/* Begin freeTempTree function */
void freeTree(TREE *root)
{
if(root!=NULL){/* As long as root isn't null, recursively */
freeTree(root->left); /* travel tree in postorder freeing the */
freeTree(root->right); /* nodes as you go. */
free(root);
}
}

I don't know C very well, but I'm pretty sure that = is an assignment operator, so, for example, while(x=3) doesn't work because there is no comparison in the while condition. What you would mean is while(x==3).
-SN

I believe SN has the correct answer, but let me clarify a little bit.
First, he's correct that the problem is the single equal (=) should be a double equal (==) in the while statement
while (fscanf(...) == EOF)
Normally, putting a single equal in place of a double equal causes runtime problems, not compile problems.
In this case tho, I suspect that you are getting an error (why not tell us the error when posting, by the way?) like:
illegal lvalue
This error means that the value on the left side of the assigment is not a variable that can be assigned a value.
For example:
int x;
x = 3;All is OK.
const int x = 2;
x = 3;You'd get an "illegal lvalue" error because x is defined as a constant, thus you can't change the value.
In your case, fscanf() is not a legal 'lvalue'.
To clarify SN's example:while (x = 3)
is perfectly legal C/C++ code!!
You won't get a compiler error. You will get an infinite loop tho, which is probably not what you want.
The 'value' of an assignment statement, is the value assigned.
So in "while (x = 3)", the value 3 will be assigned to x, then the value assigned (3), will be tested to see if it is true or false. Since anything non-zero is true, the while loop will continue.
So, you'd probably never use "while (x = 3)", but there are times when I use an assigment statement inside the test part of a while, for example:while ((ch = getchar()) != EOF)
The value returned from getchar() is
assigned to ch, then that assigned value
is compared to EOF. Note that the extra
parens are critical here. Because of the
order that operators are evaluated, leaving
out the inner parens changes the way it works.while (ch = getchar() != EOF) is something completely different (the != is evaluated before the =)
or when testing a return code for True/False but also wanting to store it:
while (returnCode = someFunc())
the return value from someFunc() is
stored in 'returnCode' and then tested
for true/false and the while loop
responds accordingly.The above can be confusing, so you might code it differently if you are seeking 'easily readable' code.

![]() |
reading data from smart c...
|
Public AV Signature Defin...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |