Thread: Computing divetimes based on time logged

1. Computing divetimes based on time logged

Objective: Write a program in C that reads in dive times in the form of hh:mm (hours and minutesseparated by a colon) and calculates the total dive time (i.e. the sum of all the log times), theaverage dive time and selects the longest dive time. The calculated outputs are all output in theform of hhh:mm or hh:mm (i.e. hours and minutes)

Reading the input from the supplied data file:

Once you create your divelog executable, you can get it to read data from an input file by doing the following in the command shell:

cat logtimes.txt | ./divelog

The code I have so far is pasted below. I didn't realize that we have to get user input and that the file has to be taken in AS user input..I'm having trouble with the syntax for this. Any suggestions? Thank you

Code:
#include<stdio.h>

int main(){

FILE *fp;

fp = fopen("logtimes.txt", "r");

if(fp==NULL){

printf("Unable to open file");

return 0;

}

int hh, mm, min;
int total_min = 0;
int c=0;
int longest = 0;

while(!feof(fp)){

fscanf(fp, "%d:%d", &hh, &mm);

min = hh*60+mm;

total_min += min;

if(min>longest) {

longest = min;

c++;
}

}

int avg = total_min/c;

printf("Total dive time is %02d:%02d\n", (total_min/60), (total_min%60));

printf("Average dive time is %02d:%02d\n", (avg/60), (avg%60));

printf("Longest dive time is %02d:%02d\n", (longest/60), (longest%60));

return 0;

} 2. You first need to understand this command:
Code:
cat logtimes.txt | ./divelog
What cat does is concatenate the contents of files named in its arguments and print the result to standard output. There is only one argument, i.e., logtimes.txt, so the content of logtimes.txt is printed to standard output. This output is then piped to ./divelog as standard input. Therefore, you should write your divelog program to read from standard input, not from logtimes.txt 3. Originally Posted by laserlight You first need to understand this command:
Code:
cat logtimes.txt | ./divelog
What cat does is concatenate the contents of files named in its arguments and print the result to standard output. There is only one argument, i.e., logtimes.txt, so the content of logtimes.txt is printed to standard output. This output is then piped to ./divelog as standard input. Therefore, you should write your divelog program to read from standard input, not from logtimes.txt
Right, I understand that. I'm jsut having trouble with the syntax to do that since I'm new to C. Would it be like this?

Code:
printf("Enter dive start time in format (HH:MM): ");
scanf("%s %s", hh,mm); 4. I've accounted for the user input. This code runs but it won't let the user input more than once.

Code:
#include<stdio.h>

int main(int argc, char ** argv){

int hh, mm, min;
int total_min = 0;
int c=0;
int longest = 0;
int amt;

float avg;

if(argc == 2){
//File mode
FILE *fp;

fp = fopen(argv, "r");

if(fp==NULL){
printf("Unable to open file");
return 0;
}

while(!feof(fp)){
fscanf(fp, "%d:%d", &hh, &mm);

min = hh*60+mm;

total_min += min;

if(min>longest) {
longest = min;
c++;
}
}
}else{
printf("How many dive times would you like to enter?");
scanf(%s, &amt);

while(amt){
printf("Enter dive time in format(HH:MM):");
scanf(%s %s, &hh, &mm);

min = hh*60+mm;

total_min += min;

if(min>longest) {
longest = min;
c++;
}

amt--;
}
}

avg = total_min/c;

printf("Total dive time is %02d:%02d\n", (total_min/60), (total_min%60));
printf("Average dive time is %02d:%02d\n", (avg/60), (avg%60));
printf("Longest dive time is %02d:%02d\n", (longest/60), (longest%60));

return 0;
} Popular pages Recent additions Tags for this Thread

dive, file, input, int, time 