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
;-)