Greenhand want help!

This is a discussion on Greenhand want help! within the C Programming forums, part of the General Programming Boards category; Hi Experts, I'm greenhand to C, please lend me a hand why I can not get following Source compiled correctly? ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    2

    Unhappy Greenhand want help!

    Hi Experts,

    I'm greenhand to C, please lend me a hand why I can not get following Source compiled correctly?


    Thanks in advance!

    Rick


    #include <stdio.h>

    #define FALSE 0
    #define NULL 0

    typedef struct {
    int dataitem;
    struct listelement *link;
    } listelement;

    void Menu (int *choice);
    listelement * AddItem (listelement * listpointer, int data);
    listelement * RemoveItem (listelement * listpointer);
    void PrintQueue (listelement * listpointer);
    void ClearQueue (listelement * listpointer);

    main () {
    listelement listmember, *listpointer;
    int data,
    choice;

    listpointer = NULL;
    do {
    Menu (&choice);
    switch (choice) {
    case 1:
    printf ("Enter data item value to add ");
    scanf ("%d", &data);
    listpointer = AddItem (listpointer, data);
    break;
    case 2:
    if (listpointer == NULL)
    printf ("Queue empty!\n");
    else
    listpointer = RemoveItem (listpointer);
    break;
    case 3:
    PrintQueue (listpointer);
    break;

    case 4:
    break;

    default:
    printf ("Invalid menu choice - try again\n");
    break;
    }
    } while (choice != 4);
    ClearQueue (listpointer);
    } /* main */

    void Menu (int *choice) {

    char local;

    printf ("\nEnter\t1 to add item,\n\t2 to remove item\n\
    \t3 to print queue\n\t4 to quit\n");
    do {
    local = getchar ();
    if ((isdigit (local) == FALSE) && (local != '\n')) {
    printf ("\nyou must enter an integer.\n");
    printf ("Enter 1 to add, 2 to remove, 3 to print, 4 to quit\n");
    }
    } while (isdigit ((unsigned char) local) == FALSE);
    *choice = (int) local - '0';
    }

    listelement * AddItem (listelement * listpointer, int data) {

    listelement * lp = listpointer;

    if (listpointer != NULL) {
    while (listpointer -> link != NULL)
    listpointer = listpointer -> link;
    listpointer -> link = (struct listelement *) malloc (sizeof (listelement));
    listpointer = listpointer -> link;
    listpointer -> link = NULL;
    listpointer -> dataitem = data;
    return lp;
    }
    else {
    listpointer = (struct listelement *) malloc (sizeof (listelement));
    listpointer -> link = NULL;
    listpointer -> dataitem = data;
    return listpointer;
    }
    }

    listelement * RemoveItem (listelement * listpointer) {

    listelement * tempp;
    printf ("Element removed is %d\n", listpointer -> dataitem);
    tempp = listpointer -> link;
    free (listpointer);
    return tempp;
    }

    void PrintQueue (listelement * listpointer) {

    if (listpointer == NULL)
    printf ("queue is empty!\n");
    else
    while (listpointer != NULL) {
    printf ("%d\t", listpointer -> dataitem);
    listpointer = listpointer -> link;
    }
    printf ("\n");
    }

    void ClearQueue (listelement * listpointer) {

    while (listpointer != NULL) {
    listpointer = RemoveItem (listpointer);
    }
    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    reply

    Well, what errors do you get? This is one thing that I found:
    Code:
    
    typedef struct
    { 
      int dataitem; 
      struct listelement *link;
    }listelement;
    
    I believe this one should be:
    
    typedef struct listelement
    { 
      int dataitem; 
      listelement *link;
    }listelement;
    
    main() should be int main(). I don't know if your compiler automatically makes this if there are no void in front of it...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    - You need to add some include files:

    #include <ctype.h> /* needed for isdigit */
    #include <stdlib.h> /* needed for malloc, free */
    #include <malloc.h> /* needed ? for malloc, free */

    - Also remove the listmember in main()

    listelement *listpointer;

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    2

    More Info!

    Now the code is:

    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <malloc.h>

    #define FALSE 0
    #define NULL 0

    typedef struct listelement
    {
    int dataitem;
    listelement *link;
    }listelement;

    void Menu(int *choice);
    listelement *AddItem(listelement *listpointer,int data);
    listelement *RemoveItem(listelement *listpointer);
    void PrintQueue(listelement *listpointer);
    void ClearQueue(listelement *listpointer);
    void main(){
    listelement listmember,*listpointer;
    int data;
    int choice;

    listpointer = NULL;
    do{
    Menu(&choice);
    switch(choice){
    case 1:
    printf("Enter data item value to add ");
    scanf("%d",&data);
    listpointer = AddItem(listpointer,data);
    break;
    case 2:
    if(listpointer == NULL)
    printf("Queue empty!\n");
    else
    listpointer = RemoveItem(listpointer);
    break;
    case 3:
    PrintQueue(listpointer);
    break;
    case 4:
    break;
    default:
    printf("Invalid menu choice-try again\n");
    break;
    }
    }while(choice! = 4);
    ClearQueue(listpointer);
    } /*main*/
    void Menu(int *choice){

    char local;

    printf("\nEnter\t1 to add item,\n\t2 to remove item\n\t3 to print queue\
    n\t4 to quit\n");
    do{
    local = getchar();
    if((isdigit(local) == FALSE)&&(local != '\n')){
    printf("\nyou must enter an integer.\n");
    printf("Enter 1 to add, 2 to remove, 3 to print,
    4 to quit\n");
    }
    }while(isdigit((unsigned char)local) == FALSE);
    *choice = (int)local-'0';
    }

    listelement *AddItem(listelement *listpointer,int data){
    listelement *lp = listpointer;

    if(listpointer!=NULL){
    while(listpointer -> link != NULL)
    listpointer = listpointer -> link;
    listpointer -> link = (struct listelement *)malloc(sizeof(listel
    ement));
    listpointer = listpointer -> link;
    listpointer -> link = NULL;
    listpointer -> dataitem = data;
    return lp;
    }
    else {
    listpointer = (struct listelement *)malloc(sizeof(listelement));
    listpointer -> link = NULL;
    listpointer -> dataitem = data;
    return listpointer;
    }
    }

    listelement *RemoveItem(listelement *listpointer){

    listelement *tempp;
    printf("Element removed is %d\n",listpointer -> dataitem);
    tempp = listpointer -> link;
    free(listpointer);
    return termpp;
    }

    void PrintQueue(listpointer *listpointer){

    if(listpointer == NULL)
    printf("queue is empty!\n")'
    else
    while(listpointer != NULL){
    printf("%d\t",listpointer -> dataitem);
    listpointer = listpointer -> link;
    }
    printf("\n");
    }
    }

    void ClearQueue(listelement *listpointer){

    while(listpointer != NULL){
    listpointer = RemoveItem(listpointer);
    }
    }

    --------------- CUT HERE ---------------------------------------

    And I use "lint" to the file, the output is:


    lint: "queue.c", line 16: error 1000: Unexpected symbol: "listelement".
    lint: "queue.c", line 20: error 1000: Unexpected symbol: "*".
    lint: "queue.c", line 21: error 1000: Unexpected symbol: "*".
    lint: "queue.c", line 21: error 1000: Unexpected symbol: ";".
    lint: "queue.c", line 22: error 1000: Unexpected symbol: ";".
    lint: "queue.c", line 23: error 1000: Unexpected symbol: ";".
    lint: "queue.c", line 19: error 1705: Function prototypes are an ANSI feature.
    lint: "queue.c", line 20: error 1705: Function prototypes are an ANSI feature.
    lint: "queue.c", line 20: error 1573: Type of "listpointer" is undefined due to
    an illegal declaration.
    lint: "queue.c", line 25: warning 845: errors seen skipping the code including f
    unction.
    lint: "queue.c", line 26: error 1000: Unexpected symbol: "listmember".
    lint: "queue.c", line 54: error 1000: Unexpected symbol: "!".
    lint: "queue.c", line 23: warning 558: Empty declaration.
    lint: "queue.c", line 26: error 1573: Type of "listmember" is undefined due to a
    n illegal declaration.
    lint: "queue.c", line 26: error 1573: Type of "listpointer" is undefined due to
    an illegal declaration.
    lint: "queue.c", line 30: error 1549: Modifiable lvalue required for assignment
    operator.
    lint: "queue.c", line 37: warning 563: Argument #1 is not the correct type.
    lint: "queue.c", line 37: warning 563: Argument #2 is not the correct type.
    lint: "queue.c", line 37: error 1549: Modifiable lvalue required for assignment
    operator.
    lint: "queue.c", line 40: error 1563: Expression in if must be scalar.
    lint: "queue.c", line 43: error 1549: Modifiable lvalue required for assignment
    operator.
    lint: "queue.c", line 25: warning 845: errors seen skipping the code including f
    unction.
    lint: "queue.c", line 57: error 1705: Function prototypes are an ANSI feature.
    lint: "queue.c", line 57: warning 845: errors seen skipping the code including f
    unction.
    lint: "queue.c", line 72: error 1000: Unexpected symbol: "*".
    lint: "queue.c", line 72: error 1000: Unexpected symbol: "int".
    lint: "queue.c", line 72: error 1000: Unexpected symbol: "{".
    lint: "queue.c", line 72: warning 845: errors seen skipping the code including f
    unction.
    lint: "queue.c", line 75: error 1000: Unexpected symbol: "if".
    lint: "queue.c", line 75: error 1000: Unexpected symbol: "!=".
    lint: "queue.c", line 75: error 1000: Unexpected symbol: "{".
    lint: "queue.c", line 76: error 1000: Unexpected symbol: "->".
    lint: "queue.c", line 76: error 1000: Unexpected symbol: "!=".
    lint: "queue.c", line 77: error 1000: Unexpected symbol: "listpointer".
    lint: "queue.c", line 78: error 1000: Unexpected symbol: "->".
    lint: "queue.c", line 80: error 1000: Unexpected symbol: "->".
    lint: "queue.c", line 81: error 1000: Unexpected symbol: "->".
    lint: "queue.c", line 82: error 1000: Unexpected symbol: "return".
    lint: "queue.c", line 83: error 1000: Unexpected symbol: "}".
    lint: "queue.c", line 86: error 1000: Unexpected symbol: "->".
    lint: "queue.c", line 87: error 1000: Unexpected symbol: "->".
    lint: "queue.c", line 88: error 1000: Unexpected symbol: "return".
    lint: "queue.c", line 89: error 1000: Unexpected symbol: "}".
    lint: "queue.c", line 92: error 1000: Unexpected symbol: "{".
    lint: "queue.c", line 73: warning 557: Missing declaration specifiers, "int" ass
    umed.
    lint: "queue.c", line 73: error 1588: "listpointer" undefined.
    lint: "queue.c", line 73: error 1521: Incorrect initialization.
    lint: "queue.c", line 75: error 1506: Parameters allowed in function definition
    only.
    lint: "queue.c", line 75: error 1584: Inconsistent type declaration: "listpointe
    r".
    lint: error 2017: Cannot recover from earlier errors, terminating.


    ------------------------------------------- CUT HERE ---------------------------


    You see, too many errors!!!

  5. #5
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Most of them are simply syntax errors which can easily be fixed.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    reply

    The code is a little bit messy. Plz use the code tag ( [ C O D E ] ... [ / C O D E ] ) to make it look better next time, like this:
    Code:
    int main()
    {
      printf("Hello");
      getch();
      return 0;
    }
    "Unexpected symbol"? Hmm, I have never got that error before... Must be some syntax error somewhere!
    Last edited by Magos; 01-29-2002 at 05:46 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    This looks a bit dubious...

    >>>
    typedef struct listelement
    {
    int dataitem;
    listelement *link;
    }listelement;
    <<<

    ... you are declaring a type, and an object of that type, with the same name. Is that really what you want to do? Later, you are creating objects and pointers to them, I think you may be better off with this...

    >>>
    typedef struct listelement
    {
    int dataitem;
    listelement *link;
    };
    <<<
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21