-
help with this
i have an assignment that has to show and sort a student record system from a txt file, i have done this, but it needs to be more efficent an also i need the sort to work properly and be in more depth e.g. alphabetically or some other form
here it is
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
void goto_first();
void goto_prev();
void goto_next();
void goto_last();
void goto_sort();
void menu();
struct stype
{
char s1[100];
char s2[100];
char s3[100];
char s4[100];
};
struct stype studs[10];
int compare(const void* p1, const void* p2)
{
const struct stype* s1 = (const struct stype*)p1;
const struct stype* s2 = (const struct stype*)p2;
return strcmp(s1->s3, s2->s3);
}
FILE *f;
char *t;
char seps[] = ",";
char line[80];
char page[100][100];
int len;
int CurrentLine = 0;
int LastLine = 0;
int k;
int size;
void breakup(int c)
{
// fill up studs[c]
}
void main()
{
FILE *f = fopen("studentl.txt", "r");
if (f != NULL)
{
printf("\n\t\t\t***Student Record System***\n");
printf("\nFILE STATUS: File Opened OK\n\n");
}
else
{
printf("\n\t\t\t***STUDENT DATABASE***\n");
printf("\nFILE STATUS: ERROR - File not loaded\n");
printf("\nMake sure the file exe and the student record system are saved together in the same place.\n\n");
exit(0);
}
while(!feof(f))
{
for (k=0; k<1; k++)
{
fgets(line, 80, f);
}
if (feof(f))
{
break;
}
len = strlen(line);
if (len > 0)
{
line[len-1] =0;
}
strcpy(page[LastLine++], line);
}
menu();
}
void menu()
{
char input;
char finish = 0;
do
{
printf("Press, N for Next, P for Previous, F for First, or L for Last record.\nPress S,
to Sort records, by Student #. X to Exit:\n\n");
input = getch();
switch(input)
{
case 'f':
case 'F':
goto_first();
break;
case 'l':
case 'L':
goto_last();
break;
case 'n':
case 'N':
goto_next();
break;
case 'p':
case 'P':
goto_prev();
break;
case 's':
case 'S':
goto_sort();
break;
case 'x':
case 'X':
finish = 1;
break;
default:
printf("\n\n<WRONG CHAR>\n");
}
} while (finish!=1);
}
void goto_first()
{
CurrentLine = 0;
f = fopen("Students.txt", "r");
if (f !=NULL)
for (k=0;k<1;k++)
{
fgets(line,80,f);
}
t=strtok(line,seps); strcpy (studs->s1, t);
t=strtok(NULL,seps); strcpy (studs->s2, t);
t=strtok(NULL,seps); strcpy (studs->s3, t);
t=strtok(NULL,seps); strcpy (studs->s4, t);
printf("\n\t ***FIRST RECORD*** ");
printf("\n\nRECORD # %d", k);
printf("\n\n\tSURNAME: \t%s \n\n\tFIRSTNAME: \t%s \n\n\tSTUDENT #: \t%s\n\n\tD.O.B: \t\t%s\n",
studs->s1, studs->s2, studs->s3, studs->s4);
printf("\n\n");;
printf( "\t***BEGINNING OF RECORDS***\n\n");
CurrentLine = 1;
}
void goto_last()
{
CurrentLine = 9;
f = fopen("student.txt", "r");
if (f !=NULL)
for (k=0;k<10;k++)
{
fgets(line,80,f);
}
t=strtok(line,seps); strcpy (studs->s1, t);
t=strtok(NULL,seps); strcpy (studs->s2, t);
t=strtok(NULL,seps); strcpy (studs->s3, t);
t=strtok(NULL,seps); strcpy (studs->s4, t);
printf("\n\t ***LAST RECORD*** ");
printf("\n\nRECORD # %d", k);
printf("\n\n\tSURNAME: \t%s \n\n\tFIRSTNAME: \t%s \n\n\tSTUDENT #: \t%s\n\n\tD.O.B: \t\t%s\n",
studs->s1, studs->s2, studs->s3, studs->s4);
printf("\n\n");;
printf( "\t ***END OF RECORDS*** \n\n");
}
void goto_next()
{
while (CurrentLine != 10)
{
CurrentLine = CurrentLine + 1;
f = fopen("Students.txt", "r");
if (f !=NULL)
for (k=0;k<CurrentLine;k++)
{
fgets(line,80,f);
}
t=strtok(line,seps); strcpy (studs->s1, t);
t=strtok(NULL,seps); strcpy (studs->s2, t);
t=strtok(NULL,seps); strcpy (studs->s3, t);
t=strtok(NULL,seps); strcpy (studs->s4, t);
printf("\n\t ***NEXT RECORD*** ");
printf("\n\nRECORD # %d", k);
printf("\n\n\tSURNAME: \t%s \n\n\tFIRSTNAME: \t%s \n\n\tSTUDENT #: \t%s\n\n\tD.O.B: \t\t%s\n",
studs->s1, studs->s2, studs->s3, studs->s4);
printf("\n\n");;
if (CurrentLine >= 10) { printf( "\t ***END OF RECORDS*** \n\n");}
return;
}
}
void goto_prev()
{
while (CurrentLine != 1)
{
CurrentLine = CurrentLine - 1;
f = fopen("Students.txt", "r");
if (f !=NULL)
for (k=0;k<CurrentLine;k++)
{
fgets(line,80,f);
}
t=strtok(line,seps); strcpy (studs->s1, t);
t=strtok(NULL,seps); strcpy (studs->s2, t);
t=strtok(NULL,seps); strcpy (studs->s3, t);
t=strtok(NULL,seps); strcpy (studs->s4, t);
printf("\n\t ***PREVIOUS RECORD*** ");
printf("\n\nRECORD # %d", k);
printf("\n\n\tSURNAME: \t%s \n\n\tFIRSTNAME: \t%s \n\n\tSTUDENT #: \t%s\n\n\tD.O.B: \t\t%s\n",
studs->s1, studs->s2, studs->s3, studs->s4);
printf("\n\n");;
if (CurrentLine <= 1) { printf("\t ***BEGINNING OF RECORDS***\n\n");}
return;
}
}
void goto_sort()
{
int CurrentLine = 0;
FILE* f = fopen("Students.txt", "r");
if(f == NULL)
{
printf("Can't open file\n");
return;
}
while(fgets(line,sizeof(line),f) != NULL)
{
size = strlen(line);
if(line[size-1] == '\n')
line[size-1] = 0;
t=strtok(line,seps); strcpy (studs[CurrentLine].s1, t);
t=strtok(NULL,seps); strcpy (studs[CurrentLine].s2, t);
t=strtok(NULL,seps); strcpy (studs[CurrentLine].s3, t);
t=strtok(NULL,seps); strcpy (studs[CurrentLine].s4, t);
CurrentLine++;
if(CurrentLine == 10)
break;
}
printf("BEFORE SORTING: \n\n");
printf("\t RECORD | STUDENT # | D.O.B\t | SURNAME\t | FIRSTNAME\n");
for (k=0;k< CurrentLine;k++)
{
printf("\n\t%4d\t| %s | %s\t | %s\t | %s ", k,
studs[k].s3,
studs[k].s4,
studs[k].s1,
studs[k].s2); }
qsort((void *) studs, CurrentLine, sizeof( struct stype), compare);
printf("\n\nAFTER SORTING: \n\n");
printf("\t RECORD | STUDENT # | D.O.B\t | SURNAME\t | FIRSTNAME\n");
for (k=0;k< CurrentLine;k++)
{
printf("\n\t%4d\t| %s | %s\t | %s\t | %s ", k,
studs[k].s3,
studs[k].s4,
studs[k].s1,
studs[k].s2);
}
if (CurrentLine >= 10)
{
printf("\n\n\t\t\t ***END OF RECORDS*** \n\n");
}
}
any help is much appreciated thanks guys
-
What's wrong with it? What do you need help with? Use code tags.
-
Gotos! Ahh!!!! If you could edit it and add code tags we might take a look.
-
sry about that first time here, well i am told its not efficent enough meaning its long winded, an also i am missing some sort of more detailed sorting, and also when i run this i need 2 know how to get the year 99 at top, and 00 below that not the other way!! sry if this is 2 much, thanks for any help tho
-
Are you two guys in the same class? Maybe you should help eachother out.
-
ye we r from the same class an were mates its how we came up with this coding we have will appreicate any help!!
-
While you're at school, consider taking a course in basic English, and possibly one in typing. Because you don't seem to be able to express yourself in full, readable sentences.
Quzah.
-
i came here for help in programming not english lessong i have been polite but u know what if thats the mannor ppl show here forget it!! sorry for wasted your time
-
ok in english u can understand, I can't get the compare to work, for the records to be sorted by id #. I would like the 1995 numbers to be at the top of the list, and as the list goes down, the year increases to around 2005. I would like to have them in the order
95, 96, 97, 98, 99, 00, 01, 02, 03, 04
i believe the problem is somewhere in the sortcode but i am probably wrong
-
Is 90 greater than 01? Yes, it is. If that's not what you want, you'll have to convert them to four digits and sort that.
Quzah.
-
Perhaps storing your years as 4-digit years and not 2-digit years would help.
-
Perhaps they should take a class on common sense while they're at it. :rolleyes:
Quzah.
-
No need to take a class on common sense. Just can't understand C that much, too cryptic.
The problem is using the set century command. Need to add 19 to numbers say 95 - 00, giving 1995, and 20 for those numbers 00 - 05, giving 2000, etc.
This would then sort the records by 95 first, has the code would take it has 1995, which is lower than 2005.
The specifications state that it is 2 digit year, so changing it to 4 isn't allowed.
Seen some info in a book on the set century, but doesn't work with the code. I have a feeling maybe something is missing, or it could be due to the wording/names used for the variables.
That is as clear as we (myself & tyrantil) can state.
-
> The specifications state that it is 2 digit year, so changing it to 4 isn't allowed.
Then you need to add all that logic to your compare function
Like
1. convert both strings to integer
2. if int value is > 50, add 1900 to it, else add 2000 to it
3. compare both integer results.
-
Y2k bug eheheh, this is why many companies especially the ones that deal with mortgages, rebuild their databases or patched them. because the computer didnt know that 01 is 2001. so you have to use 4 digits.