Please help me with my code, problem on compiling [closed]
#include <stdio.h>
#include <malloc.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct node
{
char data[20];
char m[40];
int mcount;
struct node* next;
struct node* prev;
struct node* link;
} node;
struct node* dic ;
struct node* allocating ( int n ) ;
void add ( char* ) ;
int *search ( char* null, struct node* head ) ;
void show ( node* ) ;
int sorting ( char *, char * );
void delete_el ( node** head, node* p ) ;
int edit ( char* ) ;
int save ( node* head, char filename ) ;
int load ( node* head, char filename, int n ) ;
int main( void)
{
node *N = NULL ;
node *p ;
node *L ;
node *a ;
node *h ;
int n, nm, k, r, i ;
char filename [ 50 ] ;
char c_name [ 50 ] ;
char word [ 20 ] ;
r=0;
while(1)
{
r++;
system ( " cls " ) ;
puts ( "\t\t Dictionary Menu: \n" ) ;
puts ( "\t\t 1.Add. \n ") ;
puts ( "\t\t 2.Search. \n ") ;
puts ( "\t\t 3.Show. \n") ;
puts ( "\t\t 4.Edit. \n") ;
puts ( "\t\t 5.Delete. \n " ) ;
puts ( "\t\t 6.Compare. \n" ) ;
puts ( "\t\t 7.Save. \n" ) ;
puts ( "\t\t 8.Load. \n" ) ;
puts ( "\t\t 9.Clear. \n" ) ;
puts ( "\t\t 10.Quit. \n" ) ;
do
{
puts("\n Enter number of menu");
scanf("%d",&nm);
if ( nm==11 ) break ;
if ( ( r==1 ) && ( nm!=1 ) )
{
puts ( "\t\tAdd the word first!" ) ;
puts ( "\t\tPress any key to continue..." ) ;
getch ();
}
}
while((r==1)&&(nm!=1));
switch(nm)
{
case 1 ://INPUT
printf ( "\nEnter the word : " ) ;
fflush ( stdin ) ;
gets ( word ) ;
add ( word ) ;
break;
case 2 : //SEARCH
printf ( "\nEnter the word to search : " ) ;
fflush ( stdin ) ;
gets ( word ) ;
i = search ( word ) ;
if ( ! i )
printf ( "Word does not exist." ) ;
getch( ) ;
break ;
case 3 : //OUTPUT
show( word ) ;
getch( ) ;
break;
case 4 ://EDIT
printf ( "\nEnter the word you want to edit: " ) ;
fflush ( stdin ) ;
gets ( word ) ;
edit ( word ) ;
getch ( ) ;
break;
case 5 : //DELETE
printf("Enter name of country you want to delete: ");
fflush(stdin);
gets(c_name);
p = search(h,c_name);
//functia de jos NA
//delete1 (&h,p);
if (!p)
{
puts ("Operation was not performed");
puts ("Press any key to continue...");
//getch();
break;
}
else
puts ("Information about Country was deleted successfuly");
puts ("Press any key to continue...");
getch ();
break;
case 6 ://COMPARING
break;
case 7 ://SORTING
sorting( );
break;
case 8 ://SAVE TO FILE
printf ("Enter name of the file\n");
fflush(stdin);
gets(filename);
save(h,filename);
puts("Information was saved successfuly");
puts("Press any key to continue...");
getch ();
break;
case 9 ://LOAD FROM FILE
printf ("Enter name of the file\n");
fflush(stdin);
gets(filename);
n = length((h));
load(h,filename,n);
puts("Information was load successfuly");
puts("Press any key to continue...");
getch ();
break;
case 10 : //FREE MEMORY
//uncomment cand o creezi
//freememory( N );
printf("\n Press Enter to bring back the Menu");
getch();
break;
case 11 : //EXIT
//uncomment cand o creezi
//deldic( ) ;
exit ( 0 ) ;
}
}
}
node *allocating ( int n ) //ALLOCATING
{
node *head, *C, *p ;
int i;
for( i=0; i<n; i++ )
{
C = ( node* ) malloc ( sizeof ( *C ) ) ;
if ( !C )
{
puts ( "Memory was not allocated" ) ;
exit(1);
}
if( i == 0 )
{
head = C ;
}
else
{
p -> next = C ;
}
C -> next = NULL ;
p = C ;
}
return head ;
}
node *search( node *head, char *c_name ) //SEARCH
{
node *C ;
C = head ;
while ( C )
{
if ( strcmp ( C -> data, c_name ) == 0 )
{
return C;
}
C = C -> next ;
if ( C == head ) break ;
}
return NULL ;
}
void add ( node *head ) //ADDING
{
int i = 0 ;
int mozilla;
node *h;
puts ( "How many words do you want to add?" ) ;
scanf ( "%i", &mozilla ) ;
h = allocating ( mozilla ) ;
puts ( "Enter the words:\n" ) ;
while ( h )
{
printf("\t%d:\n", i+1 ) ;
i++ ;
puts ( "The word itself:\n" ) ;
fflush ( stdin ) ;
gets( h -> data [ i ] ) ;
puts ( "It's definition:\n" ) ;
fflush ( stdin ) ;
gets( h -> m[ i ] ) ;
if( h -> next == head ) break ;
h = h -> next ;
}
}
void show( ) //OUTPUT
{
struct node *n ;
int i, j ;
printf ( "Word\t\tMeaning\n" ) ;
for ( i = 0 ; i <= 30 ; i++ )
printf ( "-" ) ;
for ( i = 0 ; i <= 25 ; i++ )
{
n = dic [ i ] ;
while ( n != NULL )
{
printf ( "\n%s\t\t%s", n -> data, n -> m [ 0 ] ) ;
for ( j = 1 ; j < n -> mcount ; j++ )
printf ( "\n\t\t%s", n -> m [ j ] ) ;
n = n -> link ;
}
}
}
int length ( node *head ) //LENGTH
{
int l = 0 ;
node *C ;
C = head ;
while ( C )
{
C = C -> next ;
l++ ;
if( C == head ) break ;
}
return l ;
}
int save( node *head, char *filename ) //SAVE
{
FILE *fp ;
node *C ;
开发者_Python百科int i;
fp = fopen ( filename," w " ) ;
if ( !fp ) return 0 ;
C = head ;
while ( C )
{
fprintf ( fp, "%s %s \n" , C -> data [ i ] , C -> m [ i ] ) ;
C = C -> next ;
if ( C == head ) break ;
}
fclose ( fp ) ;
return 1 ;
}
int load(node *head, char *filename,int n) //LOAD
{
FILE *fp;
node *C;
int i;
fp = fopen ( filename, " r " ) ;
if ( !fp ) return 0 ;
C = head ;
while ( C )
{
fscanf ( fp, "%s %s \n" , &C -> data [ i ] , &C -> m [ i ] ) ;
if ( C -> next == head ) break ;
{
C = C -> next ;
}
}
fclose ( fp ) ;
return 1 ;
}
void delete_el ( node **head, node *p ) //DELETING
{
node *a ;
if ( *head == p )
{
p = *head ;
*head = p-> next ;
free ( p ) ;
return ;
}
else
{
a = *head ;
while ( a -> next != p )
{
a = a -> next ;
}
a -> next = p -> next ;
free ( p ) ;
return ;
}
}
//int sorting ( char *s1, char *s2 )
//{
// if ( strcmp ( s1, s2 ) == 0 ) return ( 0 ) ; //if they are equal
//
// for ( int i=0;s1[i]!=0;i++)
// {
// if ( data [ i ] > data [ i+1 ] ) return ( 1 ) ;
// else if ( s1 [ i ] < s2 [ i ] )return ( 2 ) ;
// }
//
// return ( 2 ) ; //hey if they are not equal and s1 not greater than s2 then s2 is greater
//}
I get this error messages:
C:\Users\user\Desktop\Untitled1.c||In function 'main':|
C:\Users\user\Desktop\Untitled1.c|96|warning: passing argument 1 of 'show' from incompatible pointer type|
C:\Users\user\Desktop\Untitled1.c|22|note: expected 'struct node *' but argument is of type 'char *'|
C:\Users\user\Desktop\Untitled1.c|115|warning: passing argument 1 of 'search' from incompatible pointer type|
C:\Users\user\Desktop\Untitled1.c|21|note: expected 'char *' but argument is of type 'struct node *'|
C:\Users\user\Desktop\Untitled1.c|115|warning: passing argument 2 of 'search' from incompatible pointer type|
C:\Users\user\Desktop\Untitled1.c|21|note: expected 'struct node *' but argument is of type 'char *'|
C:\Users\user\Desktop\Untitled1.c|115|warning: assignment from incompatible pointer type|
C:\Users\user\Desktop\Untitled1.c|149|warning: passing argument 2 of 'save' makes integer from pointer without a cast|
C:\Users\user\Desktop\Untitled1.c|26|note: expected 'char' but argument is of type 'char *'|
C:\Users\user\Desktop\Untitled1.c|160|warning: passing argument 2 of 'load' makes integer from pointer without a cast|
C:\Users\user\Desktop\Untitled1.c|27|note: expected 'char' but argument is of type 'char *'|
C:\Users\user\Desktop\Untitled1.c|209|error: conflicting types for 'search'|
C:\Users\user\Desktop\Untitled1.c|21|note: previous declaration of 'search' was here|
C:\Users\user\Desktop\Untitled1.c|227|error: conflicting types for 'add'|
C:\Users\user\Desktop\Untitled1.c|20|note: previous declaration of 'add' was here|
C:\Users\user\Desktop\Untitled1.c||In function 'add':|
C:\Users\user\Desktop\Untitled1.c|243|warning: passing argument 1 of 'gets' makes pointer from integer without a cast|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\stdio.h|356|note: expected 'char *' but argument is of type 'char'|
C:\Users\user\Desktop\Untitled1.c|246|warning: passing argument 1 of 'gets' makes pointer from integer without a cast|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\stdio.h|356|note: expected 'char *' but argument is of type 'char'|
C:\Users\user\Desktop\Untitled1.c||In function 'show':|
C:\Users\user\Desktop\Untitled1.c|253|error: number of arguments doesn't match prototype|
C:\Users\user\Desktop\Untitled1.c|22|error: prototype declaration|
C:\Users\user\Desktop\Untitled1.c|264|error: incompatible types when assigning to type 'struct node *' from type 'struct node'|
C:\Users\user\Desktop\Untitled1.c|289|error: conflicting types for 'save'|
C:\Users\user\Desktop\Untitled1.c|26|note: previous declaration of 'save' was here|
C:\Users\user\Desktop\Untitled1.c|307|error: conflicting types for 'load'|
C:\Users\user\Desktop\Untitled1.c|27|note: previous declaration of 'load' was here|
||=== Build finished: 7 errors, 8 warnings ===|
The code contains multiple errors such as passing variables of the wrong type to functions, function definitions that differ from their previous declaration, assignment of variables with an incompatible type etc. It's simply not possible to tell you how to fix the code since this is too much code, too many errors and too few explanation.
I recommend you changing your style of working: Write a few lines of code and compile it. Then fix all the errors the compiler complains about. If possible test the compiled program. Then add a few more lines of code and repeat the above steps.
By working in smaller steps, you'll more likely be able to fix the errors yourself, mainly because it's just one or two errors to fix. Even if you can't fix it, the StackOverflow community will then be able to tell you what the cause and the possible fix for the error is.
But refrain from writing hundreds of lines of code without compiling them. Once you have accumulated so many errors, is almost impossible to isolate and fix them.
Lots of problems in this code, but you knew that.
fflush
is not defined for input streams; fflush(stdin)
does not clear the input stream. Remove those lines from your code altogether.
NEVER NEVER NEVER NEVER NEVER use gets
. It will introduce a point of failure in your code. It has been deprecated in the C99 standard, and is expected to be gone from the C1X standard. Replace those with calls to fgets
instead.
The one problem that stands out immediately to me is that you've declared add
to take a char *
and call it as such, but you later define it to take a node *
:
void add(char *);
...
char word[20];
...
gets(word); // use fgets instead!!!!
...
void add(node *head)
{
...
}
This is undoubtedly where at least one of your errors is coming from. Go back through your code and make sure that your function definitions match up with your function declarations and that you're calling the functions with the right argument types.
Note that you can place your function definitions before main
; the definition also serves as a declaration, so that's one less thing you have to keep straight. In general, I arrange my code so that functions are defined before they are used (at least within the same file); it means my code reads from the bottom up, but it also saves me the hassle of keeping declarations and definitions straight.
精彩评论