Thread: ADT synthax errors

  1. #1
    University Student
    Join Date
    Sep 2015
    Location
    United States
    Posts
    16

    ADT synthax errors

    Spent last three hours before taking a mini break fixing all the errors in my ADT project...
    I still am getting some errors that I seem to not be able to fix or see how to.
    The file: C code - 99 lines - codepad
    (That is the implementation. Where I am getting errors... If needed, I can link the header and client files...)
    Errors: te.c: In function ‘removeLeft’: te.c:63:49: error: expected ‘(’ before ‘{’ token { ^ te.c: In function ‘removeRight’: te.c:78:77: error: expected ‘(’ before ‘{’ token { ^ te.c:79:77: error: expected declaration or statement at end of input q-> items[q->tail+size-1%q->size] = input; ^ te.c:79:77: error: expected declaration or statement at end of input
    I know it means to put another { or a ; to end it but I cannot find where the syntax error is to solve it... Thanks

  2. #2
    Guest
    Guest
    else if requires a condition, if you don't need a condition, just use else.
    In the future, post your code directly here in [code][/code] tags, 100 lines is not too much for that.

  3. #3
    University Student
    Join Date
    Sep 2015
    Location
    United States
    Posts
    16
    Thank you. Fixed the errors..
    I'm now occurring errors:
    Code:
     warning: initialization makes pointer from integer without a cast [enabled by default]
         Deque q = newDeque(*xi);
                   ^
    /tmp/cc5TR5ii.o: In function `main':
    weis_client.c:(.text+0x4b): undefined reference to `newDeque'
    collect2: error: ld returned 1 exit status
    Code in my client that I believe is getting the error from...:
    Code:
     int x; // Choice is what you choose for switch statement and x is the number to be set as max of the queue
        xi = &x;
        printf("Please enter the number to be set as max of the queue"); 
        scanf("%d", &x);
        
        Deque q = newDeque(*xi);
    Then in my implementation the creation of it..
    Code:
    struct deque { 
        int head;
        int tail; 
        int size; 
        char *items; //Purpose as an array 
    };
    
    Deque createDeque(int max_size)
    {//Will take in a maximum size and create a new deque. Again, you will want to create your struct and your array using dynamic allocation. 
        Deque q = malloc(sizeof *q);
        q->items = malloc(sizeof(char) * max_size);
        q->head = 0;// Both should start at zero
        q->tail = 0;
        q->size = max_size;
        return q;
    }
    Only thing I can guess is that I needed to make it x to be a pointer. so I did *xi...
    instead of just plain X... Still got the errors.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by marchmadness
    Only thing I can guess is that I needed to make it x to be a pointer. so I did *xi...
    instead of just plain X
    Why do you think that you need to pass a pointer? Look:
    Code:
    Deque createDeque(int max_size)
    The parameter is an int. x is an int. No pointer! xi is completely unnecessary.

    Anyway, what and where is the declaration of Deque? I would have expected it to be a typedef for struct deque*, but as we do not have access to "deque.h", maybe this is not so.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    University Student
    Join Date
    Sep 2015
    Location
    United States
    Posts
    16
    Header file part:
    Code:
     typedef struct deque* Deque; //creating a handle for struct queue

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah. Did you #include "deque.h" in the client code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    University Student
    Join Date
    Sep 2015
    Location
    United States
    Posts
    16
    Yes ma'am.
    Replaced it with x...
    I get:
    Code:
     In function ‘main’:
      warning: implicit declaration of function ‘newDeque’ [-Wimplicit-function-declaration]
         Deque q = newDeque(x);
         ^
    warning: initialization makes pointer from integer without a cast [enabled by default]
         Deque q = newDeque(x);
                   ^
    /tmp/ccTmyqfA.o: In function `main':
    : undefined reference to `newDeque'
    collect2: error: ld returned 1 exit status

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you post your entire code: not only the implementation file, but the header and client code too. Something just does not add up: it looks like you have missing declarations, but then you apparently do have them.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Actually it looks like the OP is using a compiler that accepts default return types and default parameter types and is not declaring or defining the function.


    Jim

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jimblumberg
    Actually it looks like the OP is using a compiler that accepts default return types and default parameter types and is not declaring or defining the function.
    That's true: I though that because the header was said to be included and contained at least the typedef, that it would also contain the correct function forward declaration. But of course, if the header were to be posted, we could then check.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    University Student
    Join Date
    Sep 2015
    Location
    United States
    Posts
    16
    I use my school's SSH gcc compiler. I edit the code on JGRASP then compile on the SSH server. (Reason: That is how professor grades it)
    Header:
    Code:
    #ifndef DEQUE_H
    #define DEQUE_H
    //Used as a true or false statement...
    typedef int bool; 
     
    typedef struct deque* Deque; //creating a handle for struct queue
    
    // Creates a new array that has a max defined size based on input of client 
    Deque createDeque(int max_size);
    
    //Inserts a value in the front of the queue
    int insertLeft(Deque q, char input);
    
    //Inserts a value to the rear of the queue
    int insertRight(Deque q, char input);
    
    //Removes a value from the head of the queue/ the front of it
    char removeLeft(Deque q, char input);
    
    //Removes a value from the tail of the queue/ the rear of the queue
    char removeRight(Deque q, char input);
    /* Returns whether a queue is empty
    * @param q the queue to check
    * @return whether the queue is empty
    */
    bool isEmpty(Deque q);
    /* Returns whether a queue is full
     * * @param q the queue to check
     * * @return whether the queue is full
     * */
    bool isFull(Deque q);
    
    void freeDeque(Deque q);
    
    #endif
    Implementation:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "deque.h"
    
    struct deque { 
        int head;
        int tail; 
        int size; 
        char *items; //Purpose as an array 
    };
    
    Deque createDeque(int max_size)
    {//Will take in a maximum size and create a new deque. Again, you will want to create your struct and your array using dynamic allocation. 
        Deque q = malloc(sizeof *q);
        q->items = malloc(sizeof(char) * max_size);
        q->head = 0;// Both should start at zero
        q->tail = 0;
        q->size = max_size;
        return q;
    }
    int insertLeft(Deque q, char input) 
    {
    //Will accept your deque and insert the character on the left side of the deque. 
    if(isFull(q)) // check to see if it is full 
    {
    printf("Reached the max size of queue.");
             exit(1);
    }
    
    else
    {
    q->items[q->head++%q->size] = input;
    }
    return input;
    }
    int insertRight(Deque q, char input) 
    {
    // Will accept your deque and insert the character on the right side of the deque. 
    
    if(isFull(q)) // check to see if it is full 
    {
    printf("Reached the max size of queue.");
             exit(1);
    }
    
    else
    { 
    q->items[q->tail++%q->size] = input;
    }
    return input;
    }
    
    
    char removeLeft(Deque q, char input)
    {
    // Will accept your deque and remove the character on the left side of the deque. 
    
       if(isEmpty(q))
        {
            printf("Queue is empty.");
            exit(1);
        }  
       else 
    {
    q->items[q->head+1%q->size] = input;
    }
    return input;
    }
    char removeRight(Deque q, char input)
    {
    //Will accept your deque and remove the character on the right side of the deque. 
    
    if(isEmpty(q))// We need to check if it is empty rather than if it is full...
        {
            printf("Queue is empty.");
            exit(1);
        }
        else 
    {
    q->items[q->tail+q->size-1%q->size] = input;
    }
    
       return input;
    }
    bool isEmpty(Deque q) 
    { 
    return q->tail == q->head; 
    }
    bool isFull(Deque q)
    {
    return q->tail - q->head == q->size; // Easiest way to check if full
    }
    void freeDeque(Deque q)
    {//Will free the memory used by your deque. 
    free(q->items);
        free(q);
    }
    Client:
    Code:
    /**
     * Client of Deque.
     *
     */
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    #include "deque.h"
    
    
    int main()
    {
    char c,d;
    int choice = 0;
    int *xi;
    int x; // Choice is what you choose for switch statement and x is the number to be set as max of the queue
        
        printf("Please enter the number to be set as max of the queue"); 
        scanf("%d", &x);
        
        Deque q = newDeque(x);
          do {
        printf("Please select one of the options \n");
        printf("1: Enter a character to the front\n");
        printf("2: Enter a character to the head\n");
        printf("3: Remove a character from the left\n");
        printf("4: Remove a character from the right\n");
        printf("5: Exit");
        switch (choice)
        {
        case '1': 
        printf("Please enter the character you want to add to the front");
        scanf(" %c", &c);
        insertLeft(q, c);
        break;
        case '2':
        printf("Please enter the character you want to add to the rear");
        scanf(" %c", &c);
        insertRight(q, c);
        break;
        case '3':
        printf("Removing character from the left");
        scanf(" %c", &d);
        removeLeft(q, d); 
        printf("Character removed is: %c\n", d); 
        printf("Answer so far: %c \n %c\n", removeLeft(q, d), removeRight(q, d));
        break;
        case '4':
        printf("Removing character from the right");
        scanf(" %c", &d);
        removeRight(q, d);
        printf("Character removed is %c\n", d);
        printf("Answer so far is: %c \n %c\n", removeLeft(q, d), removeRight(q, d));
        break;
        case '5': 
        return 0; 
        break; 
        }
        }
        while ( choice != 5 );
    
            return 0;
    }

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Great. In your header, you declared a function named createDeque:
    Code:
    Deque createDeque(int max_size);
    You then implemented createDeque in the corresponding source file. But in your client code, you attempted to use a non-existent function named newDeque:
    Code:
    Deque q = newDeque(x);
    By the way, your code is badly in need of proper indentation. For example, here is your current client code with the newDeque -> createDeque fix applied, with proper indentation:
    Code:
    /**
     * Client of Deque.
     *
     */
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    #include "deque.h"
    
    
    int main()
    {
        char c,d;
        int choice = 0;
        int *xi;
        int x; // Choice is what you choose for switch statement and x is the number to be set as max of the queue
    
        printf("Please enter the number to be set as max of the queue");
        scanf("%d", &x);
    
        Deque q = createDeque(x);
        do {
            printf("Please select one of the options \n");
            printf("1: Enter a character to the front\n");
            printf("2: Enter a character to the head\n");
            printf("3: Remove a character from the left\n");
            printf("4: Remove a character from the right\n");
            printf("5: Exit");
            switch (choice)
            {
            case '1':
                printf("Please enter the character you want to add to the front");
                scanf(" %c", &c);
                insertLeft(q, c);
                break;
            case '2':
                printf("Please enter the character you want to add to the rear");
                scanf(" %c", &c);
                insertRight(q, c);
                break;
            case '3':
                printf("Removing character from the left");
                scanf(" %c", &d);
                removeLeft(q, d);
                printf("Character removed is: %c\n", d);
                printf("Answer so far: %c \n %c\n", removeLeft(q, d), removeRight(q, d));
                break;
            case '4':
                printf("Removing character from the right");
                scanf(" %c", &d);
                removeRight(q, d);
                printf("Character removed is %c\n", d);
                printf("Answer so far is: %c \n %c\n", removeLeft(q, d), removeRight(q, d));
                break;
            case '5':
                return 0;
                break;
            }
        }
        while ( choice != 5 );
    
        return 0;
    }
    As you can see, the logical flow of control through the code is now easier to see. For example, you can more easily tell at a glance where the do while loop begins and ends.

    Also, you should avoid making multiple nested calls like these when they modify the deque:
    Code:
    printf("Answer so far: %c \n %c\n", removeLeft(q, d), removeRight(q, d));
    The reason is that the order of evaluation of function arguments is unspecified. This means that removeLeft could be called first, or removeRight could be called first, you simply cannot tell in advance. In this case, it probably doesn't matter, but say if you were calling removeLeft twice instead, it could make an observable difference.

    A few other things to improve:
    • Check the return value of malloc and scanf: malloc could return a null pointer; scanf might not return the expected number of assignments.
    • Instead of the typedef of int to bool, if you are compiling with respect to C99 or later, #include <stdbool.h> instead. If not, it would be better to conditionally define:
      Code:
      #ifndef __bool_true_false_are_defined
      typedef int bool;
      #endif
    • It would be consistent with free for freeDeque to handle a null pointer argument as a no-op.


    Oh, and "synthax" is actually "syntax".
    Last edited by laserlight; 02-10-2016 at 10:51 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    University Student
    Join Date
    Sep 2015
    Location
    United States
    Posts
    16
    Thank you so much.. Feel like an idiot that I missed that. Thank you so much for the advice too

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can anyone help me with these errors?
    By nullifyed in forum C Programming
    Replies: 7
    Last Post: 05-30-2010, 07:52 AM
  2. Replies: 3
    Last Post: 08-13-2008, 01:34 PM
  3. private struct synthax
    By carlorfeo in forum C++ Programming
    Replies: 18
    Last Post: 02-26-2008, 03:54 PM
  4. Errors!
    By Moony in forum C Programming
    Replies: 16
    Last Post: 06-26-2006, 04:55 PM
  5. errors.. errrors.. more errors
    By Klinerr1 in forum C++ Programming
    Replies: 17
    Last Post: 07-23-2002, 08:43 PM

Tags for this Thread