1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
#include <iostream>
#include <limits>
//convert seconds in to HH/MM/SS format
double SecToH(double sec, double &hours, double &minutes, double &seconds){
int temp;
hours = sec/3600;
temp= hours;
minutes = (hours-temp)*60;
temp = minutes;
seconds = ((minutes-temp)*60)+0.5;
}
int main(){
double h, m, s;
const int daySec = 86400;
int startNumber, winner=daySec, raceTime, winNr;
bool inputCorrect;
//Program starts
std::cout<<"Welcome.\nEnter the Start number of each participant and their Start and End Times."<<std::endl;
std::cout<<"To END program ENTER 0 as Start number."<<std::endl;
do{
//ENTER start number
std::cout<< "\n"<< "ENTER Start number: "<<std::endl;
std::cin>>startNumber;
//End program at "0" input
if(startNumber != 0){
//ENTER start time
long startTime;
long endTime = endTime + startTime;
std::cout<<"ENTER Start time and End time "<<std::endl;
std::cin>>startTime;
//check if Start time is within realistic values
if(startTime < daySec ){
//Print start Time
SecToH(startTime,h,m,s);
std::cout<< "Start time: " << int(h) << ":" << int(m) << ":" << int(s) <<std::endl;
//Input end time
std::cin>>endTime;
//check for wrong input
if(endTime > daySec*2 || endTime < 1 || endTime < startTime){
inputCorrect = false;
}
//length of race, store fastest time
raceTime = endTime-startTime;
if(raceTime<winner){
winner=raceTime;
}
//save Start number of fastest time if 0 has not been input
if(raceTime<winner || startNumber != 0){
winNr=startNumber;
}
// check if lap passes 24:00 hours
if(endTime > daySec){
endTime = endTime-daySec;
}
//if input correct
if(inputCorrect != false ){
//Print end time
SecToH(endTime,h,m,s);
std::cout<< "Finnish time: " << int(h) << ":" << int(m) << ":" << int(s) << std::endl;
//if End time input is wrong
} else{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout<< "Invalid Input try again"<< std::endl;
}
} else{
//if start input is wrong
std::cout<< "ENTER a realistic Start time"<<std::endl;
}
} else{
//winners time and Start number
SecToH(winner,h,m,s);
std::cout<< "Winner is! "<<winNr<<std::endl;
std::cout<< "Time: "<< int(h) << ":" << int(m) << ":" << int(s) <<std::endl;
}
}while(startNumber!=0);
//keep program from closing once 0 is input
std::cin.get();
return 0;
}
|