This is basically my school homework and I want to know what to improve, and what not to do. The homework basically is this: You will get a input in seconds, which is supposed to be shown as H:MM:SS where H is hours M is minutes and S is seconds. The formatting is supposed to look like in my example. ( note: In output, 0s from the left will be ignored and replaced with spaces )
Input:
3000
730
308
3967
15
1
Output:
The count of entires: 6
50:00
12:10
5:08
1:06:07
15
1
So basically, I did a code based on the example. I haven't got more informations, just that but it is enough and fun. Here goes my code:
#include <iostream>
#include <fstream>
inline int CountSpaces ( int hod, int min, int sec ){
int spaces = -1;
// 1:06:07
if ( hod )
return 0;
if ( min >= 10 )
return 2;
if ( min < 10 && min > 0 )
return 3;
if ( sec >= 10 )
return 5;
if ( sec < 10 )
return 6;
// in the case something goes wrong..
return spaces;
}
// convert time to hour,min,seconds
void CalcTime ( int value, int& hour, int& min, int& sec ){
hour = value / 3600;
min = value % 3600 / 60;
sec = value % 60;
}
// output spaces
void Space ( int* hour, int* min, int* sec ){
int spaces = CountSpaces ( *hour , *min , *sec );
if ( !spaces ) return;
for ( int i = 0 ; i < spaces ; i++ ){
std::cout << ' ';
}
}
void Write ( int time ){
if ( time <= 0 )
return;
int hour;
int min;
int sec;
CalcTime ( time , hour , min , sec );
Space ( &hour , &min , &sec );
bool bHour = false;
bool bMin = false;
if ( hour > 0 ){
bHour = true;
std::cout << hour << ':';
}
if ( min < 60 && min > 0 ){
bMin = true;
if ( bHour ){
if ( min < 10 ){
std::cout << '0';
}
}
if ( min > 0 )
std::cout << min << ':';
}
if ( sec < 60 && sec >= 0 ){
if ( sec < 10 && bMin ){
std::cout << '0';
}
std::cout << sec << std::endl;
}
}
int main ( ){
std::ifstream fin ( "in.txt" );
if ( !fin ) return 1;
int num;
int count = 0;
int arr[100];
while ( fin >> num ){
arr [ count++ ] = num;
}
std::cout << "The count of entires: " << count << std::endl;
for ( int i = 0 ; i < count ; i++ ){
Write ( arr [ i ] );
}
return 0;
}
stringstream
,string
, and STL. This is good information for answerers to know. Would you mind editing those constraints into the question? \$\endgroup\$