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 ...
- 09-28-2008 #1Just 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: %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.
- 09-28-2008 #2
could you try re-attaching your code...Thanks Gerard4143
- 09-28-2008 #3Just Joined!
- Join Date
- Sep 2008
- Location
- Buffalo, NY
- Posts
- 25
- 09-28-2008 #4
use the # quick button in the editor to wrap code tags around selected text...Hope this helps...Gerard4143
- 09-28-2008 #5
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
- 09-28-2008 #6Just Joined!
- Join Date
- Sep 2008
- Location
- Buffalo, NY
- Posts
- 25
- 09-28-2008 #7
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; } } } }
- 09-28-2008 #8
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
- 09-28-2008 #9Just 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.)
- 09-28-2008 #10Just 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???


Reply With Quote
