Thread: error:expected declaration or statement at end of input

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    63

    linked list and read strings-problem

    GO TO POST 6(is my new problem)
    {
    this solved
    I am getting this message when i am trying to compile the code
    series1.c:44: σφάλμα: expected declaration or statement at end of input
    series1.c:44: σφάλμα: expected declaration or statement at end of input
    where is the mistake?(maybe any missing { or })


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node{
      char data[64];
      struct node *next;
     };
    struct node *firsta, *currenta, *newa;
    
    int main(void)
    {
     int k;
     scanf("%d", &k);
    
     struct node *head=NULL;
    
     do{
      switch(k){
       case 1: addnew();
       break;
    
      }while(k!=0);
    
     void addnew(void)
    {
     struct node *newnode=malloc(sizeof(struct node));
      
     if (head=NULL) 
      firsta=newa=currenta;
     else
     {
      currenta=firsta;
       while(currenta->next!=NULL)
        currenta=currenta->next;
    
       currenta->next=newa;
       currenta=newa;
    }
    
    gets(currenta->data);
    
    currenta->next=NULL;
    }
    }SOLVED
    Last edited by alzar; 09-20-2007 at 12:23 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you indent your code correctly, you'll notice that you are missing two curly braces...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Count the number of closing braces which your main() seems to have.

    Read the FAQ on why you shouldn't use gets()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    Quote Originally Posted by alzar View Post
    I am getting this message when i am trying to compile the code
    series1.c:44: σφάλμα: expected declaration or statement at end of input
    series1.c:44: σφάλμα: expected declaration or statement at end of input
    where is the mistake?(maybe any missing { or })


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node{
      char data[64];
      struct node *next;
     };
    struct node *firsta, *currenta, *newa;
    
    int main(void)
    {
     int k;
     scanf("%d", &k);
    
     struct node *head=NULL;
    
     do{
      switch(k){
       case 1: addnew();
       break;
        }
      }while(k!=0);
      
    return 0;
    }
     void addnew(void)
    {
     struct node *newnode=malloc(sizeof(struct node));
      
     if (head=NULL) 
      firsta=newa=currenta;
     else
     {
      currenta=firsta;
       while(currenta->next!=NULL)
        currenta=currenta->next;
    
       currenta->next=newa;
       currenta=newa;
    }
    
    gets(currenta->data);
    
    currenta->next=NULL;
    }
    This code was a part of an exercise that i have, so i forget to close switch and main.Sorry

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    int main(void)
    {
        int k;
        scanf("%d", &k);
    
        struct node *head=NULL;
    
        do {
            switch(k) {
            case 1:
                addnew();
                break;
            }
        } while(k!=0);
    This is an infinite loop if k is anything other than 0. There is no modification of k beyond the initial scanf call.



    Code:
    void addnew(void)
    {
        struct node *newnode=malloc(sizeof(struct node));
      
        if (head=NULL) 
            firsta=newa=currenta;
    = is for assignment, == is to test for equality. Second, head is not in scope here. Third, currenta hasn't yet been initialized the first time the function is called, head is equal to NULL and in fact does not change it's value so it will always be NULL and the rest of the code (the else part) will never get executed.


    Code:
        else
        {
            currenta=firsta;
            while(currenta->next!=NULL)
                currenta=currenta->next;
    
            currenta->next=newa;
            currenta=newa;
        }
    
        gets(currenta->data);
    
        currenta->next=NULL;
    }
    newa never gets initialized to anything valid (in the above if test it is effectively set to the same random address as currenta). Same thing with firsta and since currenta points to this random memory address I certainly wouldn't want to try and access its next pointer member. I think newa is really supposed to be newnode here in this function and firsta is maybe supposed to be set to head which gets passed in as a parameter?


    The whole function should either be returning a pointer to a new node, or setting an argument passed into the function to the newly allocated memory, or something like that. Bottom line is there's a lot wrong with this code that needs fixing.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    @hk_mp5kpdw
    i make some changes. What's wrong?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node{
      char data[64];
      struct node *next;
     };
    struct node *firsta, *currenta, *newa;
     void addnew(void);
    
    int main(void)
    {
     int k;
    
    firsta=NULL;
    
     do{
     fflush(stdin);  
     printf("enter a choice\n");
     scanf("&#37;d", &k);
      switch(k){
       case 1: 
       printf("enter string");
       fflush(stdin); 
       addnew();
       break;}
    
      }while(k!=0);
    return 0;
    }
     void addnew(void)
    {
     newa=(struct node *)malloc(sizeof(struct node));
      
     if (firsta==NULL) 
      firsta=currenta=newa;
     else
     {
      currenta=firsta;
       while(currenta->next!=NULL)
        currenta=currenta->next;
    
       currenta->next=newa;
       currenta=newa;
     }
    
    gets(currenta->data);
    
    currenta->next=NULL;
    }
    I am getting something like this
    enter a choice
    1
    enter stringenter a choice
    1
    enter stringenter a choice
    asd
    enter stringenter a choice
    0
    owner@owner-laptop
    Last edited by alzar; 09-20-2007 at 12:18 PM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    fflush(stdin);
    Not what you wnat to do. Check the FAQ for "How do I clear the input buffer" or some such.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    i found only this
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    should i replace fflush with something?if yes with what?
    or just remove it from the code?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by alzar View Post
    i found only this
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    should i replace fflush with something?if yes with what?
    or just remove it from the code?
    No, another bit further down:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. you should not cast malloc (see FAQ)
    2. you HAVE TO check the return value of malloc
    3. you SHOULD NOT use gets (see FAQ)
    4. Why do you need currenta and newa as globals? Make the local for addnew
    5. better to make *firsta local for main and pass it as a parameter to addnew (pointer to it actually to be able to modify it)
    6. do not forget to free the list before you exit the program
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    @matsp
    Thank you so much.I didn't know that fflush is so dangerous.I replace the fflush with
    while ((ch = getchar()) != '\n' && ch != EOF);
    but now i have another problem look:
    PHP Code:
    owner@owner-laptop:~$ ./series1
                            
    /*the first time i must enter in order to print me the"enter choice"*/
    enter a choice
    1
    enter string
    hello there

    enter a choice

    Is there a way not to have this problem?(look in my edit down here in this post)

    ps:in the link you gave me the BUFSIZ where is? In stdio.h?

    @vart
    thank you.I know all of them but this not the finally code.I will make some of the changes you tell me in the end and also the addnew function must have no arguments.




    edit:i delete the first while and i haven't this problem,but maybe is necessary ?i don't know.In the book where i saw the code it cleans the input also before enter choice

    note:there is also other cases after, such as delete an element of the list or print an element
    Last edited by alzar; 09-21-2007 at 01:29 AM.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can't just "flush" the input buffer if there's nothing there. You'll need to try to get something first, then flush if you think the user input more than you wanted. [But better make SURE that this is the case, otherwise it will be the same problem].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    i update my post and still have some problems with what it prints me
    case 1 is to add a newnode
    case 2 to print the list
    case 3 to print one element(asks to give an index) of the list and delete it
    case 4 to find the length of a string that i gave his index


    it prints me
    enter a choice
    1
    enter string
    hello
    enter a choice
    1
    enter string
    there
    enter a choice
    you
    enter string

    so in enter choice i write "you" and then it didn't tell me again enter choice but enter string.why?
    the hole code(a little big)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    struct node{
      char data[64];
      struct node *next;
     };
    struct node *firsta, *currenta, *newa;
     
    void addnew(void);
    void printall(struct node *head);
    void printanddelete( int i);
    int findlen(int j);
    int slen(char *p);
    
    int main(void)
    {
     int k,i,j,m;
     char ch;
    firsta=NULL;
    
     do{
     printf("enter a choice\n");
     scanf("&#37;d", &k);
      switch(k){
       case 1: 
       printf("enter string\n");
       while ((ch = getchar()) != '\n' && ch != EOF);
       addnew();
       break;
       
       case 2:
       printall(firsta);
       break;
    
       case 3:
        printf("enter a number");
        scanf("%d", &i);
        printanddelete(i);
        break;
       case 4:
        printf("enter index to  find length");
        scanf("%d", &j);
        m=findlen(j);
        printf("%d", m);}
      }while(k!=0);
    return 0;
    }
     void addnew(void)
    {
     newa=(struct node *)malloc(sizeof(struct node));
      
     if (firsta==NULL) 
      firsta=currenta=newa;
     else
     {
      currenta=firsta;
       while(currenta->next!=NULL)
        currenta=currenta->next;
    
       currenta->next=newa;
       currenta=newa;
     }
    
    gets(currenta->data);
    
    currenta->next=NULL;
    }
    
    void printall(struct node *head)
    {
     struct node *current=head;
    
     if(head==NULL)
      return;
    
      while(current!=NULL)
       {printf(current->data);
        printf("\n");
        current=current->next;
    
        }
        printf("~");
    }
    
    
    void printanddelete( int i)
    {
     int index=1;
     struct node *current=firsta;
     struct node *prev;
    
     while (current!=NULL){
      if(i==index)
       {printf(current->data);
        if(current==firsta) 
          firsta=current->next;
         else
          prev=current->next;
    
        free(current);
        current=prev;
        break;}
      else{
      index++;
      current=current->next;}
      }
    }
    
    int findlen(int j)
    {
      struct node *current=firsta;
      int index=1;
      int l;
      
      while (current!=NULL){
        if (j==index)
          return(slen(current->data));
        else
          index++;
          current=current->next;  
       }
    }
    
    
    int slen(char *p)
    {
     if (*p=='\0')
       return 0;
     else
       return(slen(++p) +1);
    }
    Last edited by alzar; 09-22-2007 at 03:21 AM.

  14. #14
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    anyone please?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parse error at end of input??
    By erikcn in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2002, 09:41 AM
  2. Parse Error At End Of Input?!?!?!?!?!
    By kas2002 in forum C++ Programming
    Replies: 3
    Last Post: 06-02-2002, 09:22 AM
  3. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  4. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM
  5. Reading integers until end of input
    By nivo in forum C Programming
    Replies: 7
    Last Post: 10-20-2001, 04:18 PM