Hey guys,
Just joined today, and since It's my first post. I'm not sure where to post this either in Beginner or here. anyway, I can't figure out how to turn 00:30am/pm to show as 12:30 am/pm . here's what I mean:
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
usingnamespace std
int main () {
int count = 0, // number of valid times entered
newTime, // latest time entered
lastTime = 0, // previous time entered
gap, // current gap
shortestGap, // the shortest gap
longestGap, // the longest gap
hours, // hours input
minutes, // minutes input
gapTotal = 0, // total of all gaps
badCount = 0; // number of invalid times entered
// get input from user
cout << "Enter time in hours and minutes (99 99 to stop): ";
cin >> hours >> minutes;
// loop until user enters 99 99
for (;;){
if (hours==99&&minutes==99){
break;
}else{
// if time is crazy, output an error message
if ((hours < 0) || (hours > 23) || (minutes < 0) || (minutes > 59)) {
cout << "** Time ignored - hours and/or minutes invalid. **" << endl;
badCount++;
} else {
newTime = (60 * hours) + minutes;
// if time is before the previous one, output an error message
if ( (newTime < lastTime) &&
!((lastTime > 23 * 60) && (newTime < 60)) ) {
cout << "** Time ignored - not >= previous time. **" << endl;
badCount = badCount + 1;
} else {
//converting 24hrs to 12hrs (12:30am//0:30am does not work)
if (hours == 00 && minutes == 00) {
cout <<"valid time"<< " midnight"<<endl;
} elseif (hours == 12 && minutes == 00) {
cout <<"valid time"<< " noon"<<endl;
} elseif (hours < 12) {
cout <<"valid time"<<hours<<":"<<minutes<< "AM"<<endl;
} else {
hours=hours-12;
cout <<"valid time"<<hours<<":"<<minutes<<"PM"<<endl;
}
// we have a good time
if (count == 0) {
// our very first time
lastTime = newTime;
count = 1; // we now have one time
} else {
// not the first time, so there's a gap to calculate
if (newTime < lastTime) {
gap = (newTime + (24 * 60)) - lastTime;
} else {
gap = newTime - lastTime;
} // end if
// gap statistics: total; min and max
gapTotal = + gap;
if ((count == 1) || (gap < shortestGap)) {
shortestGap = gap;
}
if ((count == 1) || (gap > longestGap)) {
longestGap = gap;
}
lastTime = newTime;
count++;
} // end if
} // end if
} // end if}
// get input from user
cout << "Enter time in hours and minutes (99 99 to stop): ";
cin >> hours >> minutes;
}
} //end for
// output results
cout << endl << count << " valid times and " << badCount << " invalid times were entered." << endl;
if (count < 2) {
cout << "Fewer than 2 times were entered - no gaps to analayze.\n";
} else {
cout << "The shortest gap was " << shortestGap << " minutes long.\n"
<< "The longest gap was " << longestGap << " minutes long.\n"
<< "The average gap length was " << gapTotal / (count - 1.0)
<< " minutes." << endl;
} // end if
cout << endl;
system ("PAUSE");
return 0;
Your code is nearly unreadable. Please use the button marked '<>' at the right of the editor field to format it. Then try a preview to see whether other people like to read it.