请帮我解决我的代码,编译问题

时间:2011-05-01 15:22:51

标签: c compiler-errors

#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 ;
    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
//}

我收到此错误消息:

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

2 个答案:

答案 0 :(得分:3)

代码包含多个错误,例如将错误类型的变量传递给函数,与先前声明不同的函数定义,使用不兼容类型的变量赋值等。根本无法告诉您如何修复代码这是太多的代码,太多的错误和太少的解释。

我建议你改变你的工作方式:编写几行代码并编译它。然后修复编译器抱怨的所有错误。如果可能的话测试编译的程序。然后添加几行代码并重复上述步骤。

通过更小的步骤,你更有可能自己修复错误,主要是因为它只需要修复一两个错误。即使您无法修复它,StackOverflow社区也能告诉您错误的原因和可能的修复方法。

但是不要编写数百行代码而不编译它们。一旦积累了这么多错误,几乎不可能隔离并修复它们。

答案 1 :(得分:1)

此代码中有很多的问题,但您知道这一点。

fflush未定义输入流; fflush(stdin) 清除输入流。完全从代码中删除这些行。

绝不永远不会使用gets在您的代码中引入故障点。它已在C99标准中弃用,预计将从C1X标准中消失。取而代之的是调用fgets

立即向我突出的一个问题是,您已声明add采取char *并将其称为此类,但您稍后将其定义为node *

void add(char *);
...
char word[20];
...
gets(word); // use fgets instead!!!!
...
void add(node *head)
{
  ...
}

毫无疑问,至少有一个错误来自于此。返回您的代码并确保您的函数 definitions 与您的函数声明 匹配,即您正在使用右侧调用函数参数类型。

请注意,您可以在main之前放置您的函数 definitions ;这个定义也可以作为一个声明,所以你必须保持简单。一般来说,我安排我的代码,以便在使用函数之前定义函数(至少在同一个文件中);它意味着我的代码从下往上读取,但它也省去了直接保持声明和定义的麻烦。