Thread: Computing divetimes based on time logged

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    3

    Question 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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,086
    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
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2018
    Posts
    3
    Quote Originally Posted by laserlight View Post
    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. #4
    Registered User
    Join Date
    Sep 2018
    Posts
    3

    Question

    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[1], "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 subscribe to a feed

Similar Threads

  1. time computing
    By Fibre Optic in forum C++ Programming
    Replies: 3
    Last Post: 09-17-2005, 04:58 PM
  2. Tick-based time ?
    By Korhedron in forum C++ Programming
    Replies: 2
    Last Post: 07-11-2003, 05:36 AM
  3. time-based movement?
    By pode in forum Game Programming
    Replies: 16
    Last Post: 12-21-2002, 06:58 PM
  4. time based movement
    By werdy666 in forum Game Programming
    Replies: 5
    Last Post: 11-04-2002, 01:52 PM
  5. computing time elapsed?
    By redemption in forum C Programming
    Replies: 2
    Last Post: 09-09-2002, 06:55 AM

Tags for this Thread