Need Help For A Program!

This is a discussion on Need Help For A Program! within the C Programming forums, part of the General Programming Boards category; Hi i need help for my assignment If anyone got any ideas.... Cheers! A simple log book contains records (lines ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    4

    Exclamation Program structure

    Hi i need help for my assignment
    If anyone got any ideas....
    Cheers!

    A simple log book contains records (lines of text) that have the following format:

    Name Start km End km Date Destination

    Functionality of your Program
    Need tp write a program that takes such a log book as input. Each line in the log book contains one record, with each field separated by a \t (tab) character.

    The program should read the log book from stdin (Standard Input). After reading all input records, the program should print a short summary to stdout (Standard Output), including the number of trips, the total number of kilometres traveled, and the average trip length.

    The input is terminated by an EOF (end of file, can be simulated on gucis (unix command) by pressing Ctrl-D) or an error condition.

    Here is an example input file (spaces between the fields denote tab characters):

    Peter 23000 23100 17/01/2003 Southport
    John 23100 23250 18/01/2003 Noosa
    Peter 23250 23280 19/01/2003 CBD
    Peter 23500 23550 25/01/2003 Logan

    The output for the above log data should be:

    Number of trips: 4
    Total kilometres: 330
    Average trip length: 82.5
    Error and Consistency Checking
    All errors need to be caught and an appropriate error message should be printed on stderr. The program should then exit with an error code of EXIT_FAILURE as defined in <stdlib.h>.

    In addition to checking for system, input, and other errors, I need to add the following consistency checks:

    check that Start km and End km are both decimal numbers,
    check that in a record, Start km always less than End km; and
    check that Start km in one record is always greater than or equal to End km of the previous record.
    Make sure to handle inconsistencies in the same manner as errors (see above).

    The maximum input line length (excluding trailing carriage return or new line characters) will be 80 characters. However, you need to make sure that your program detects longer lines and exits gracefully with a corresponding error message.


    Thats what I did:
    This would be for stdin:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
    
    char * line;
    int int1;
    int int2;
    line = fgets(line, 81, stdin);
    
    sscanf(line,"%*[^\t]%d%*[^\t]%d%*[^\t]%*[^\n]",&int1,&int2);
    
    while (fgets(line,sizeof line,stdin) != NULL){ /*read the input line*/
    {
    		
    int i = 0;
    for (i=0; line[i] != '\n' && i <= sizeof line; i++){ /*makes sure the line is < 80 characters*/
    
    if (i == sizeof line){ /*this will be true only when the line exceed 80 characters*/
    
    fprintf(stderr, "The input line exceeds 80 characters.\n");
    						return EXIT_FAILURE;
    							
    		line = fgets(line, 81, stdin);
         }
    }
    
    /*In stdout I need to calculate number of trips, total & average kilometre:*/
    void Trips()
    {
    	int numT = 0;
    	int i;
    	for( i=0; i<number; i++)
    	numT++;
    	printf("\nNumber of trips: %d", numT);
    	printf("\nTotal kilometres: %d", sekm);
    	printf("\nThe average trips length is %d", av);
    }
    Im not shure for total km and average trips length...Im Sorry for the inconvenience by posting the entire assignment but i just started learning this programming language and we already need to implement such concepts...If anyone can help, Ill be really greatfull...TQ.


    Thx hammer, Ill try to follow what you said earlier and post the results afterwards... Cheerz
    ;-)
    Last edited by Tibo; 03-21-2003 at 09:17 AM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>If anyone got any ideas....
    Yes, get started on your own, and post your code here when you have troubles. Please don't post complete assignments...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Also looks on Hammer's signature about code-tags, and edit your post.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    As mentioned, please use code tags.

    >>char * line;
    This is only a pointer to a char, it does not give you any memory to actually store chars. Either malloc() memory, or use an array. For your level, I'd suggest the array:
    >>char line[81];

    >>line = fgets(line, 81, stdin);
    You don't use fgets() in this manner (with the use of "line")
    I'd suggest you read this and any other entries in here that you feel might be relevant.

    That sscanf() call is horrid, too
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21