Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Hello again, I had to do four programming assignments and this is my forth one. I was given the code and I have checked the code over twice and I ...
  1. #1
    Just Joined!
    Join Date
    Sep 2008
    Location
    Buffalo, NY
    Posts
    25

    Help with Program - Label Sorting Program

    Hello again, I had to do four programming assignments and this is my forth one. I was given the code and I have checked the code over twice and I continue to receive errors when I try to compile the program. I have attached the code. I am just trying to find out what is wrong since this was the code the professor provided me with. Thanks for the assistance! Sorry for the long code.

    *edit - can someone please tell me how to attach the code in a window like I have seen on this site a few times? Thanks

    Errors:
    [dmank@localhost ~]$ gcc -o lab3c lab3c.c
    lab3c.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    lab3c.c:29: warning: data definition has no type or storage class
    lab3c.c:31: error: expected declaration specifiers or ‘...’ before ‘LABEL’
    lab3c.c:33: error: expected declaration specifiers or ‘...’ before ‘LABEL’
    lab3c.c:35: error: expected ‘)’ before ‘*’ token
    lab3c.c: In function ‘main’:
    lab3c.c:46: error: ‘names’ undeclared (first use in this function)
    lab3c.c:46: error: (Each undeclared identifier is reported only once
    lab3c.c:46: error: for each function it appears in.)
    lab3c.c:87: error: expected expression before ‘)’ token
    lab3c.c:90: error: too many arguments to function ‘getlabel’
    lab3c.c:117: error: too many arguments to function ‘putlabel’
    lab3c.c: At top level:
    lab3c.c:123: error: expected declaration specifiers or ‘...’ before ‘LABEL’
    lab3c.c: In function ‘getlabel’:
    lab3c.c:126: error: ‘customer’ undeclared (first use in this function)
    lab3c.c: At top level:
    lab3c.c:137: error: expected declaration specifiers or ‘...’ before ‘LABEL’
    lab3c.c: In function ‘putlabel’:
    lab3c.c:140: error: ‘customer’ undeclared (first use in this function)
    lab3c.c: At top level:
    lab3c.c:148: error: expected ‘)’ before ‘*’ token

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <malloc.h>

    #define MAX 100

    typedef struct {
    char last[15],
    first[15];
    } NAME;

    typedef struct {
    char street[25],
    city[15],
    state[3];
    long zip;
    } ADDRESS;

    typedef strcut {
    NAME name;
    ADDRESS address;
    } LABEL;

    int getlabel(FILE *fp, LABEL *customer);

    void putlabel(FILE *fp, LABEL *customer);

    void sort(LABEL *names[], int number);

    main()
    {
    int i,
    number = 0;

    char more = 'Y',
    in_name[15],
    out_name[15];

    LABEL *names[MAX];

    FILE *in_file,
    *out_file;

    puts("*** Label Sorting Program ***");
    puts("press the enter key to map files to terminal\n");

    printf("Input file containing unsorted labels :");
    gets(in_name);

    printf("Output file to contain sorted labels :");
    gets(out_name);

    if (strlen(in_name) == 0)
    in_file = stdin;
    else
    {
    in_file = fopen( in_name, "r");
    if (in_file == NULL)
    {
    printf("Can't opent input: &#37;s", in_name);
    exit(0);
    }
    }

    if (strlen(out_name) == 0)
    out_file = stdout;
    else
    {
    out_file = fopen(out_name, "w");
    if (out_file == NULL)

    {
    printf("Can't open output: %s", out_name);
    exit(0);
    }
    }
    while (more == 'Y' || more == 'y')
    {
    if (number < MAX)
    {
    names[number] = (LABEL *)calloc(1, sizeof(LABEL));
    if (names[number] != NULL)
    {
    if (getlabel(in_file, names[number]) == EOF)
    {
    free(names[number]);
    break;
    }
    ++number;
    if (in_file == stdin)
    {
    printf("More labels? (Y/N): ");
    more = getchar();
    }
    }
    else
    {
    printf("<<< Out of Memory >>>\n");
    break;
    }
    }
    else
    {
    printf(" <<< Maximum No. of Labels is %d.>>>\n", MAX);
    break;
    }
    }
    sort(names, number);
    for(i=0; i<number; i++)
    putlabel(out_file, names[i]);
    flcose(in_file);
    fclose(out_file);

    }

    int getlabel(FILE *fp, LABEL *customer)
    {int num;
    if (fp == stdin) printf("Enter Name :");
    fscanf(fp, "%s %s%*c ", customer->name.first,
    customer->name.last);
    if (fp == stdin) printf("Enter street :");
    fgets(customer->address.street, 25, fp);

    if (fp == stdin) printf("Enter city, state & zip :");
    return fscanf(fp, "%s %s %ld%*c", customer->address.city,
    customer->address.state,
    &customer->address.zip);
    }

    void putlabel(FILE *fp, LABEL *customer)
    {
    fprintf(fp, "\n%s, %s\n%s%s %s %ld\n",
    customer->name.last,
    customer->name.first,
    customer->address.street,
    customer->address.city,
    customer->address.state,
    customer->address.zip);
    }

    void sort (LABEL *names[], int number)
    {

    #define TRUE 1
    #define FASLE 0

    int notsorted = TRUE,
    i;

    LABEL *ptr;

    --number;

    while (notsorted)
    {
    nosorted = FALSE;
    for (i=0; i<number; i++)
    {
    if (strcmp(names[i]->name.last, names[i+1]->name.last)>0)
    {
    notsorted=TRUE;
    ptr = names[i];
    names[i] = names[i+1];
    names[i+1] = ptr;
    }
    }
    }
    }
    Last edited by Mankthetank19; 09-28-2008 at 03:45 PM. Reason: The attachment up-load didn't work.

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    could you try re-attaching your code...Thanks Gerard4143

  3. #3
    Just Joined!
    Join Date
    Sep 2008
    Location
    Buffalo, NY
    Posts
    25
    Quote Originally Posted by gerard4143 View Post
    could you try re-attaching your code...Thanks Gerard4143
    Sorry about that - I was trying to put the code in a window instead of copying and pasting. I saw your post about AMD and was wondering how to put the code in a separate window. Thanks for the quick response.

  4. #4
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    use the # quick button in the editor to wrap code tags around selected text...Hope this helps...Gerard4143

  5. #5
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    I compiled/ran you code...its just some typing errors and omissions...that is to say it ran, I not sure it will produce the proper outcome because I don't know the programs purpose...Gerard4143

  6. #6
    Just Joined!
    Join Date
    Sep 2008
    Location
    Buffalo, NY
    Posts
    25
    Quote Originally Posted by gerard4143 View Post
    use the # quick button in the editor to wrap code tags around selected text...Hope this helps...Gerard4143

    Do you mean in the terminal or in the edit my post section

  7. #7
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Here's the corrected code...it will compile and run but like I said I'm not sure if it will produce the proper results...Gerard4143

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    #define FALSE 0 //probably not the best solution for FALSE
    
    #define MAX 100
    
    typedef struct {
    	char last[15],
    	first[15];
    	} NAME;
    
    typedef struct {
    	char street[25],
    	city[15],
    	state[3];
    	long zip;
    	} ADDRESS;
    
    typedef struct {
    	NAME name;
    	ADDRESS address;
    	} LABEL;
    
    int getlabel(FILE *fp, LABEL *customer);
    
    void putlabel(FILE *fp, LABEL *customer);
    
    void sort(LABEL *names[], int number);
    
    main()
    {
    int i,
    number = 0;
    
    char more = 'Y',
    in_name[15],
    out_name[15];
    
    LABEL *names[MAX];
    
    FILE *in_file,
    *out_file;
    
    puts("*** Label Sorting Program ***");
    puts("press the enter key to map files to terminal\n");
    
    printf("Input file containing unsorted labels :");
    gets(in_name);
    
    printf("Output file to contain sorted labels :");
    gets(out_name);
    
    if (strlen(in_name) == 0)
    in_file = stdin;
    else
    {
    in_file = fopen( in_name, "r");
    if (in_file == NULL)
    {
    printf("Can't opent input: %s", in_name);
    exit(0);
    }
    }
    
    if (strlen(out_name) == 0)
    out_file = stdout;
    else
    {
    out_file = fopen(out_name, "w");
    if (out_file == NULL)
    
    {
    printf("Can't open output: %s", out_name);
    exit(0);
    }
    }
    while (more == 'Y' || more == 'y')
    {
    if (number < MAX)
    {
    names[number] = (LABEL *)calloc(1, sizeof(LABEL));
    if (names[number] != NULL)
    {
    if (getlabel(in_file, names[number]) == EOF)
    {
    free(names[number]);
    break;
    }
    ++number;
    if (in_file == stdin)
    {
    printf("More labels? (Y/N): ");
    more = getchar();
    }
    }
    else
    {
    printf("<<< Out of Memory >>>\n");
    break;
    }
    }
    else
    {
    printf(" <<< Maximum No. of Labels is %d.>>>\n", MAX);
    break;
    }
    }
    sort(names, number);
    for(i=0; i<number; i++)
    putlabel(out_file, names[i]);
    fclose(in_file);
    fclose(out_file);
    
    }
    
    int getlabel(FILE *fp, LABEL *customer)
    {int num;
    if (fp == stdin) printf("Enter Name :");
    fscanf(fp, "%s %s%*c ", customer->name.first,
    customer->name.last);
    if (fp == stdin) printf("Enter street :");
    fgets(customer->address.street, 25, fp);
    
    if (fp == stdin) printf("Enter city, state & zip :");
    return fscanf(fp, "%s %s %ld%*c", customer->address.city,
    customer->address.state,
    &customer->address.zip);
    }
    
    void putlabel(FILE *fp, LABEL *customer)
    {
    fprintf(fp, "\n%s, %s\n%s%s %s %ld\n",
    customer->name.last,
    customer->name.first,
    customer->address.street,
    customer->address.city,
    customer->address.state,
    customer->address.zip);
    }
    
    void sort (LABEL *names[], int number)
    {
    
    #define TRUE 1
    #define FASLE 0
    
    int notsorted = TRUE,
    i;
    
    LABEL *ptr;
    
    --number;
    
    while (notsorted)
    {
    notsorted = FALSE;
    for (i=0; i<number; i++)
    {
    if (strcmp(names[i]->name.last, names[i+1]->name.last)>0)
    {
    notsorted=TRUE;
    ptr = names[i];
    names[i] = names[i+1];
    names[i+1] = ptr;
    }
    }
    }
    }

  8. #8
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    when you click add new thread or reply to post in linux forums you will be presented with a small editor, one of the quick buttons on the editor is the # button(note if you put the mouse pointer over it, it will display "wrap [code] tags around selected text")
    Just click this and enter your code between the tags...Hope this helps Gerard4143


    P.S Welcome to the Linux Forums

  9. #9
    Just Joined!
    Join Date
    Sep 2008
    Location
    Buffalo, NY
    Posts
    25
    Thanks for the assistance and informing me of the # icon - I got it to work now lol

    After looking over your code - I was able to see my mistakes but I am still not able to compile. I get the error below.

    I decided to not use the #define False 0 in the first part of the code.


    Code:
    [dmank@localhost ~]$ gcc -o lab3c lab3c.c
    lab3c.c: In function ‘sort’:
    lab3c.c:162: error: ‘FALSE’ undeclared (first use in this function)
    lab3c.c:162: error: (Each undeclared identifier is reported only once
    lab3c.c:162: error: for each function it appears in.)

  10. #10
    Just Joined!
    Join Date
    Sep 2008
    Location
    Buffalo, NY
    Posts
    25
    Nevermind - I got it working

    Thanks for the assitance and the welcome to the forums gerard.

    Can you explain the reason to define FALSE???

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...