counter.cpp
// Counter examples
#include <iostream>
using namespace std;
int main()
{
// Count up. This prints `x` number of stars.
// Consider other combinations, like start
// at 1 but loop as long as i <= x.
int x = 10;
int i = 0; // This is my counter variable.
while(i < x)
{
cout << i << '*';
i++;
}
cout << endl;
// Or here is a variation that counts down.
// In that case, we don't need an extra
// variable, unless you need to save the
// value of `x` for future calculations.
int y = 10;
while( y > 0 )
{
cout << y << '*';
y--;
}
cout << endl;
return 0;
}
// Input validation loops
#include <iostream>
using namespace std;
int main()
{
int hour;
cout << "Enter hour (0-23): ";
cin >> hour;
while(hour < 0 || hour > 23) {
cout << "Error, out of range.";
cout << "Enter hour (0-23): "; // Unfortunate duplication
cin >> hour;
}
// To get to this point, hour must be valid.
cout << hour-12 << endl;
// Here's an alternative, using the "empty for"
// style of infinite loop.
for(;;)
{
cout << "Enter hour (0-23): ";
cin >> hour;
if(hour >= 0 && hour <= 23) break;
cout << "Error, out of range." << endl;
}
// To get to this point, hour must be valid.
cout << hour-12 << endl;
return 0;
}
p05sol.cpp
// My solution to project 5, on 12- and 24-hour time
// C League, 2016-10-26
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int hour = 0;
int minute = 0;
cout << "Enter hour (0-23): ";
cin >> hour;
if(hour < 0 || hour > 23)
{
cout << "ERROR: invalid hour\n";
return 0;
}
cout << "Enter minute (0-59): ";
cin >> minute;
if(minute < 0 || minute > 59)
{
cout << "ERROR: invalid minute\n";
return 0;
}
// Print 24-hour time, with leading zeroes
if(hour < 10) cout << '0'; // Technique #1
cout << hour << ":";
// Technique #2
cout.fill('0'); // Set fill character
cout << setw(2) << minute << " is ";
// Print 12-hour time
int hour12;
if(hour == 0) hour12 = 12;
else if(hour < 13) hour12 = hour;
else hour12 = hour - 12;
cout << setw(2) << hour12 << ':'
<< setw(2) << minute << ' ';
if(hour < 12) cout << "AM";
else cout << "PM";
cout << endl;
// English approximate time
if(minute >= 8 && minute <= 22) {
cout << "quarter past";
}
else if(minute >= 23 && minute <= 37) {
cout << "half past";
}
else if(minute >= 38 && minute <= 52) {
cout << "quarter til";
hour++; // increment hour
}
else if(minute >= 53) {
hour++; // increment hour
}
cout << ' ';
switch(hour)
{
case 0: case 24: cout << "midnight"; break;
case 1: case 13: cout << "one"; break;
case 2: case 14: cout << "two"; break;
case 3: case 15: cout << "three"; break;
case 4: case 16: cout << "four"; break;
case 5: case 17: cout << "five"; break;
case 6: case 18: cout << "six"; break;
case 7: case 19: cout << "seven"; break;
case 8: case 20: cout << "eight"; break;
case 9: case 21: cout << "nine"; break;
case 10: case 22: cout << "ten"; break;
case 11: case 23: cout << "eleven"; break;
case 12: cout << "noon"; break;
}
if(hour >= 1 && hour <= 11) {
cout << " in the morning";
}
else if(hour > 12 && hour <= 17) {
cout << " in the afternoon";
}
else if(hour >= 18 && hour <= 23) {
cout << " in the evening";
}
cout << endl;
return 0;
}