help with this

This is a discussion on help with this within the C Programming forums, part of the General Programming Boards category; i have an assignment that has to show and sort a student record system from a txt file, i have ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    5

    Question 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
    Last edited by tyrantil; 01-30-2005 at 12:46 PM. Reason: missed stuff out

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    What's wrong with it? What do you need help with? Use code tags.

  3. #3
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    Gotos! Ahh!!!! If you could edit it and add code tags we might take a look.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    5
    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

  5. #5
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Are you two guys in the same class? Maybe you should help eachother out.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    5
    ye we r from the same class an were mates its how we came up with this coding we have will appreicate any help!!
    Last edited by tyrantil; 01-30-2005 at 01:45 PM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    5
    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

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    5
    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

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    Perhaps storing your years as 4-digit years and not 2-digit years would help.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Perhaps they should take a class on common sense while they're at it.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    6
    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.

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    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.
    When no one helps you out. Call google();

Page 1 of 2 12 LastLast
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