Thread: C Moron requires Mucho helpo!

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    4

    C Moron requires Mucho helpo!

    Hi,

    Have just started trying to learn how to program using C. Am trying to write a program that reads a text file containing a list of 10 prisoners eg..

    doe, john, 00222555
    doe, jane, 01254896

    I need to be able to use c to go thru these one at a time and then sort them in the order in which they were sent to prison. This is denoted by the first two numbers on their reg number. e.g.

    John Doe's year is 2000
    Jane Doe's year is 2001

    If anyone has any pointers I would be eternally greatful! as at present I am having problems just being able to read a text file!

    Sorry to ask what I guess is a very straight forward question but these are sometimes the hardest for me!!!!!!!!!!


    Thanks

    TOM0987654321

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    (Insert explitive here). We will not do your home work for you. Post your code. Post your problems and error messages. Then, and only then will you get help from me.

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

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    First off.... 2 digit years?? argh, its Y2K all over again

    Now down to business.

    Code:
        FILE *fp;
        char buffer[80];
    
        fp = fopen( "file", "r" );
        if( fp != NULL )
        {
          while( fgets( buffer, 80, fp ) != NULL )
              fputs( buffer, stdout );
          fclose( fp );
        }
    This code simply opens the file, and writes it contents to the screen (stdout). It's a start for you.

    You can try here for a reasonably good reference site (in my opinion).

    Once you have the data loaded (if you're going to sort it, you'll need to store it in some form of array/list), you can then sort it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Another good place is the FAQ and my C Refrence Card
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    4
    My Code is now loking like this!.......... I am quite amazed with myself really..... not up to much but on the way to working! Can't work out sorting for the life of me!!!!!!!!!!!

    Any help as always greatfully appreciated!
    Code:
    #include <stdio.h> 
    #include <string.h> 
    #include <stdlib.h> 
    #include <conio.h> 
    
    
    /* {char surname [20]; 
    char first [20]; 
    long reg [9] 
    char dob [9]; }RECORD; 
    
    int comp(const void *a,const void *b);*/ 
    
    void main () 
    
    { 
    char prisoner[10][60]; 
    int count2=0; 
    int count1=0; 
    
    FILE*pfile; 
    
    int choice; 
    puts(" PRISONERS RECORD VIEWER\n\n"); 
    puts("Fname Sname Reg DOB\n"); 
    
    pfile=fopen("prisoner.txt","r"); 
    
    if (pfile==NULL) 
    {printf("Cannot find prisoner file\n");} 
    
    for(;count1<10;count1++) 
    {fgets(prisoner[count1],60, pfile);} 
    printf("\n\n\n\n\n\n\n\n\n\n\n"); 
    
    
    menu: 
    
    printf("\n\n\n\n\nChoose from the option list below"); 
    printf("or quit\n\n"); 
    printf("OPTIONS :- First(1) Last(2) Previous(3) Next(4) Sort(5) "); 
    printf("Quit(6)\n\n\n\n"); 
    
    scanf("%d",&choice); 
    
    switch (choice) 
    { case 1: 
    count2=0; 
    printf("\t\t\tPrisoner Record Viewer\n\n"); 
    printf("Fname Sname Reg DOB\n\n"); 
    printf("%s",prisoner[count2]); 
    printf("\n\n\n\n\n\n\n\n\n\n\n"); 
    
    goto menu; 
    
    case 2: 
    count2=9; 
    printf("\t\t\tPrisoner Record Viewer\n\n"); 
    printf("Fname Sname Reg DOB\n\n"); 
    printf("%s",prisoner[count2]); 
    printf("\n\n\n\n\n\n\n\n\n\n\n"); 
    
    goto menu; 
    
    case 3: 
    count2=count2-1; 
    if (count2<0) 
    count2=0; 
    printf("\t\t\tPrisoner Record Viewer\n\n"); 
    printf("Fname Sname Reg DOB\n\n"); 
    printf("%s",prisoner[count2]); 
    printf("\n\n\n\n\n\n\n\n\n\n\n"); 
    
    goto menu; 
    
    case 4: 
    count2=count2+1; 
    if (count2>9) 
    count2=9; 
    printf("\t\t\tPrisoner Record Viewer\n\n"); 
    printf("Fname Sname Reg DOB\n\n"); 
    printf("%s",prisoner[count2]); 
    printf("\n\n\n\n\n\n\n\n\n\n\n"); 
    
    goto menu; 
    
    /* case 5 : SORTING!!!!!!!!! */ 
    
    case 6: 
    exit(0); 
    
    default: 
    printf("\n\n\n\n\n\n\n\n\n\n\n !NOT RECOGNISED!"); 
    printf("please re-enter using keys 1-6 only"); 
    goto menu; 
    }
    prisoner.txt ==========

    Nudd, Bob, 00266989, 30-07-80,
    Ward, Laura, 88998563, 25-09-64,
    Best, George, 01005645, 10-10-54,
    Moffat, Bobby 00254657, 25-06-48,
    Braun, Ross, 02025984, 12-05-49,
    O'Sullivan, Ron, 89215658, 12-03-62,
    Brown, Wesley, 86456856, 10-12-58,
    Chan, Jackie, 76856854, 01-10-50,
    Garbo, Greta, 64856213, 19-06-66,
    Charlton, Bobby, 66845123, 18-03-48,

    Anyone know of anywhere on the web that gives the do's and donts of indentation? Thanks as ever Tom!
    Last edited by tom0987654321; 04-30-2002 at 01:44 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void main ()
    Try int main

    No, wait, you must use int main

    > Can't work out sorting for the life of me
    Your code isn't ready for that - you need to incorporate the RECORD structure first

    > goto menu;
    And read a book about the use of the break; keyword within switch statements

    > Anyone know of anywhere on the web that gives the do's and donts of indentation
    Being consistent is the most important step - after that, it's just style
    To preserve what you have, read this before posting more code

    http://www.cprogramming.com/cboard/m...bbcode#buttons

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM