Time issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MSADDOUKI
    New Member
    • Feb 2007
    • 3

    Time issue

    My code is supposed to convert military time to standard time.
    I need to restrict the user from entering hour>25 and less than 0, and minute>59 and less than 0.
    Here's what I got so far.

    #include <iostream>
    using namespace std;

    void get_input_24hou rs ( int & hour24,char & colon, int & minute24);
    void convert_24_to_a mOrPM ( int & hour24, int & houramOrPM, int & minute24, char & amOrPM);
    void output(int & houramOrPM, char & colon, int & minute24, char & amOrPM);
    void main ()
    {
    int hour24, minute24, houramOrPM;
    char amOrPM, colon, ans='y';
    do{

    get_input_24hou rs ( hour24, colon, minute24);

    convert_24_to_a mOrPM ( hour24, houramOrPM, minute24, amOrPM );

    output ( houramOrPM, colon, minute24, amOrPM);

    cout<<"\nWould you like to check it again?\n"
    <<"\nDo you want to go again?(Y/N):\n";
    cin>>ans;

    }while ( ans=='y'|| ans=='Y');

    cout<<"\nGood-bye"<<endl;
    }

    void get_input_24hou rs (int & hour24, char & colon, int & minute24)
    {


    cout<<"\nEnter the time using the following format(hour:min ute24)";
    cin>>hour24>>co lon>>minute24;
    }

    void convert_24_to_a mOrPM( int & hour24, int & houramOrPM, int & minute24, char & amOrPM)
    {

    if ((hour24>=0)&&( hour24<=12))
    {
    houramOrPM= hour24;
    amOrPM='A';
    }

    if ((hour24>=12)&& (hour24<=23)&&( minute24>=0)&&( minute24<=59))
    {
    houramOrPM=hour 24-12;
    amOrPM='P';
    }

    if((hour24>=0)& &(hour24<1)&&(m inute24>=0)&&(m inute24<=59))
    {
    houramOrPM=12;
    amOrPM= 'A';
    }
    if (hour24==12)
    {
    houramOrPM=12;
    amOrPM='P';
    }


    }

    void output(int & houramOrPM, char & colon, int & minute24, char & amOrPM)
    {

    cout<<"\nThe time in twelve hour notation is: " <<houramOrPM<<c olon<<minute24< <amOrPM<<"M"<<e ndl;
    }



    please help
  • AdrianH
    Recognized Expert Top Contributor
    • Feb 2007
    • 1251

    #2
    Not bad, here are some things I would suggest:
    1. Put a loop in your get_input_24hou rs() function (something like you did in your main() function) to ensure that the numbers you get are within bounds. Use istream::ignore () to clear to the end of the line (delimiter '\n') after the read so you don't go and read "34:21 12:21" as "12:21" (unless of course this is what you want).
    2. Comment and (I would really emphasise this) put assertions in the convert_24_to_a mOrPM() and output() functions to restrict the input values for hour, minute, colon and amOrPm to legal values.

      This is called doing a design by contract. Which basically means that these functions are assumed to work if passed the proper values and no other.

      Trust me, this will limit the scope of where problems can occur, as you don't have to go back far to determine what range of values a variable could have been.

      To use assert, include the <assert.h> file. It takes one parameter which must be 0 (false) or non-zero (true), if 0, the file and line and the parameter (as you have written it) will be spit out to the console and the programme would exit. So if I were to say assert(1==0); the call would fail and the programme would bail. If assert(1==1) ever failed, you got a BIG problem :).


    Hope this helps,


    Adrian

    P.S. Please use [code] [/code] markers around your code. It makes it easier to read.

    Comment

    • AdrianH
      Recognized Expert Top Contributor
      • Feb 2007
      • 1251

      #3
      Oh, BTW, Design by Contract is also used for return values and out parameters too.


      Adrian

      Comment

      • MSADDOUKI
        New Member
        • Feb 2007
        • 3

        #4
        Adrian,
        I'm unable to follow you. Forgive me but I'm new to C++.
        Can u please provide more details. I tried adding a while loop to (void get_input_24hou rs) function, but it didn't work.
        thnx for responding

        Comment

        • AdrianH
          Recognized Expert Top Contributor
          • Feb 2007
          • 1251

          #5
          Originally posted by MSADDOUKI
          Adrian,
          I'm unable to follow you. Forgive me but I'm new to C++.
          Can u please provide more details. I tried adding a while loop to (void get_input_24hou rs) function, but it didn't work.
          thnx for responding
          For a new programmer, you got fairly good style. Here are some examples of what I was saying:
          Code:
          void get_input_24hours (int & hour24, char & colon, int  & minute24)
          {
            bool loop=false;
            do {
               if (loop) {
                  if (hour24 <= 0) {
                     cout << "ERROR: hour cannot be negitive!" << endl;
                  }
                  else if (hour24 >= 24) {
                     cout << "ERROR: hour cannot be greater than 23!" << endl;
                  }
                  else if ... /* add more error statements here */
               }
               cout<<"\nEnter the time using the following format(hour:minute24)";
               cin>>hour24>>colon>>minute24;
               cin.ignore(10000, '\n');  // ignore a lot of chars till reached newline
                                         // should work but may not, you should test.
               loop=true;
             /* note the (! (...)).  I am just stating the valid ranges and to loop if not valid. */
            } while (! (0 <= hour24 && hour24 <= 23 && /* add other tests here */));
          }
          Code:
          void convert_24_to_amOrPM( int & hour24, int & houramOrPM, int & minute24, char & amOrPM)
          {
               assert(0 <= hour24 && hour24 <= 23); // hour24 must be within this range!
               /* add other assert statements here to state input ranges */
          
               /* your code here has not been modified */
               if ((hour24>=0)&&(hour24<=12))
               {
                    houramOrPM= hour24;
                    amOrPM='A';                                                                                                                     
               }
                    
               if ((hour24>=12)&&(hour24<=23)&&(minute24>=0)&&(minute24<=59))
               {
                    houramOrPM=hour24-12;
                    amOrPM='P';
               }
           
               if((hour24>=0)&&(hour24<1)&&(minute24>=0)&&(minute24<=59))
               {
                    houramOrPM=12;
                    amOrPM= 'A';
               }
               if (hour24==12)
               {
                    houramOrPM=12;
                    amOrPM='P';
               } 
          
               assert(1 <= houramOrPM && houramOrPM <= 12); /* houramOrPM must be in this range!
               /* add other assert statements here to state result ranges */
          }
          Hope this helps.


          Adrian

          Comment

          • MSADDOUKI
            New Member
            • Feb 2007
            • 3

            #6
            Adrian,
            Thank you very much for you help.
            I didn't have a chance to finish the code, but I will keep u posted.
            Thanks

            Comment

            • AdrianH
              Recognized Expert Top Contributor
              • Feb 2007
              • 1251

              #7
              Originally posted by MSADDOUKI
              Adrian,
              Thank you very much for you help.
              I didn't have a chance to finish the code, but I will keep u posted.
              Thanks
              Your welcome. Any time.


              Adrian

              Comment

              Working...