mercredi 30 décembre 2015

How can I double a time value in C++

The task is to double a time from the txt file "time.in" in the format of HH:MM:SS (e.g. 12:22:03) and to output it in the same format (e.g. 24:44:06) in "time.out". I am having trouble skipping the ":" character in the input. Here's my code:

#include <fstream>
#include <cmath>
#include <stdio.h>
using namespace std;

int main() {
FILE *fin = fopen("time.in", "r");
FILE *fout = fopen("time.out", "w");
char c;
double hrs, mins, secs;
double fhrs = 0, fmins = 0, fsecs = 0;

ifstream input;
input.open("time.in");
input>> hrs;
c = fgetc(fin); //what should this be? I can't get it to work properly.
input>> mins;
c = fgetc(fin);
input>> secs;
input.close();

fhrs = hrs * 2;
if (secs < 30) {
    fsecs = secs * 2;
    if (mins < 30)
        fmins = mins * 2;
    else {
        fmins = abs(60 - (mins * 2));
        fhrs++;
    }
} else {
    fsecs = abs(60 - (secs * 2));
    fmins++;
    if (mins < 30)
        fmins += mins * 2;
    else {
        fmins += abs(60 - (mins * 2));
        fhrs++;
    }
}

ofstream output;
output.open("time.out");
output << fhrs <<":"<< fmins <<":"<< fsecs <<endl;
output.close();

return 0;

}

Aucun commentaire:

Enregistrer un commentaire