Author Topic: Programming Exercise - Time Format Conversion  (Read 4322 times)

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
Programming Exercise - Time Format Conversion
« on: December 08, 2005, 06:03:32 PM »
This solution can be simplified when we discuss functions.  For now, we simply apply programming structures already discussed in class.

Code: [Select]

/*
  Name:         Convert Time Format
  Author:       LF Cervantes
  Date:         12/08/05
  Description:  First Programming Exercise (ITE205-IM)
*/

#include <iostream>

using namespace std;

int main()
{
    int hour, min;
    char answer;
    cout << "-- TIME FORMAT CONVERSION PROGRAM --\n";
    cout << "This program will convert time format from 24-hour to 12-hour\n\n";
    do
    {
        bool InvalidInput=false;
       
        // this loop ensures the input hour and min is within valid range
        do
        {
            cout << "\nEnter the time in 24-hour format\n";
            cout << "     using 2 integers (hour minute format e.g. 13 30) :  ";
            cin >> hour >> min;
            if (hour < 0 || hour > 23)
            {
                cout << "\nData Input Error: Invalid Input for hour.\n";
                InvalidInput = true;
            }
            else if (min < 0 || min > 59)
            {
                cout << "\nData Input Error: Invalid Input for minutes.\n";
                InvalidInput = true;
            } else
            {
                InvalidInput = false;  
            }
        } while (InvalidInput);

        cout << endl;
       
        if (hour > 11)
        {
            if (hour > 12) hour -= 12;
           
            cout << "The time in 12-hour format is " << hour << ":";
            if (min < 10)
            {
                cout << "0";
            }
            cout << min << " P.M." << endl;
        }
        else if (hour == 0)
        {
            hour = 12;
            cout << "The time in 12-hour format is " << hour << ":";
            if (min < 10)
            {
                cout << "0";
            }            
            cout << min << " A.M." << endl;            
        }
        else
        {
            cout << "The time in 12-hour format is " << hour << ":";
            if (min < 10)
            {
                cout << "0";
            }            
            cout << min << " A.M." << endl;            
        }        
       
        cout << "Repeat with new input?  y/n  :";
        cin >> answer;    
    } while ( answer=='y' || answer == 'Y');
   
    cout << endl;
    system("PAUSE");
    return 0;
}


Highlights:
Do-While loop
Nested If-decision structures
Analyze. Design. Develop. Debug. Deploy. Maintain.

Gillius

  • Administrator
  • User
  • *****
  • Posts: 147
    • http://www.gillius.org/
Programming Exercise - Time Format Conversion
« Reply #1 on: December 09, 2005, 01:31:37 PM »
There is code repetition in the output.  I would refactor the code to reduce this repetition and do the following:

Code: [Select]

std::string meridian;
if ( hour >= 12 ) {
  if ( hour > 12 )
    hour -= 12;
  meridian = "P.M.";

} else {
  if ( hour == 0 )
    hour = 12;
  meridian = "A.M";
}

cout << "The time in 12-hour format is " << hour << ":";

if (min < 10)
  cout << "0";

cout << min << ' ' << meridian << endl;
Gillius
Gillius's Programming http://www.gillius.org/

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
Programming Exercise - Time Format Conversion
« Reply #2 on: December 11, 2005, 08:23:10 AM »
Thanks Gillius!

I was looking for a way to aviod the redundant coding without having to write functions.  

An important highlight of the refactored code is the use of the standard class string.  It will suffice for now to see how string is used to store data like the standard data types.  Students should read ahead Chapter 11 topic 11.2 of the course textbook Problem Solving with C++ to better understand how the string class can be used to represent values that are strings of characters.
Analyze. Design. Develop. Debug. Deploy. Maintain.