473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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_24hours ( int & hour24,char & colon, int & minute24);
void convert_24_to_amOrPM ( 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_24hours ( hour24, colon, minute24);

convert_24_to_amOrPM ( 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_24hours (int & hour24, char & colon, int & minute24)
{


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

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

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

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

if((hour24>=0)&&(hour24<1)&&(minute24>=0)&&(minute 24<=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<<colon<<minute24<<amOrPM<<"M"<<endl ;
}



please help
Feb 11 '07 #1
6 1327
AdrianH
1,251 Expert 1GB
Not bad, here are some things I would suggest:
  1. Put a loop in your get_input_24hours() 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_amOrPM() 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.
Feb 11 '07 #2
AdrianH
1,251 Expert 1GB
Oh, BTW, Design by Contract is also used for return values and out parameters too.


Adrian
Feb 11 '07 #3
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_24hours) function, but it didn't work.
thnx for responding
Feb 11 '07 #4
AdrianH
1,251 Expert 1GB
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_24hours) 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:
Expand|Select|Wrap|Line Numbers
  1. void get_input_24hours (int & hour24, char & colon, int  & minute24)
  2. {
  3.   bool loop=false;
  4.   do {
  5.      if (loop) {
  6.         if (hour24 <= 0) {
  7.            cout << "ERROR: hour cannot be negitive!" << endl;
  8.         }
  9.         else if (hour24 >= 24) {
  10.            cout << "ERROR: hour cannot be greater than 23!" << endl;
  11.         }
  12.         else if ... /* add more error statements here */
  13.      }
  14.      cout<<"\nEnter the time using the following format(hour:minute24)";
  15.      cin>>hour24>>colon>>minute24;
  16.      cin.ignore(10000, '\n');  // ignore a lot of chars till reached newline
  17.                                // should work but may not, you should test.
  18.      loop=true;
  19.    /* note the (! (...)).  I am just stating the valid ranges and to loop if not valid. */
  20.   } while (! (0 <= hour24 && hour24 <= 23 && /* add other tests here */));
  21. }
  22.  
Expand|Select|Wrap|Line Numbers
  1. void convert_24_to_amOrPM( int & hour24, int & houramOrPM, int & minute24, char & amOrPM)
  2. {
  3.      assert(0 <= hour24 && hour24 <= 23); // hour24 must be within this range!
  4.      /* add other assert statements here to state input ranges */
  5.  
  6.      /* your code here has not been modified */
  7.      if ((hour24>=0)&&(hour24<=12))
  8.      {
  9.           houramOrPM= hour24;
  10.           amOrPM='A';                                                                                                                     
  11.      }
  12.  
  13.      if ((hour24>=12)&&(hour24<=23)&&(minute24>=0)&&(minute24<=59))
  14.      {
  15.           houramOrPM=hour24-12;
  16.           amOrPM='P';
  17.      }
  18.  
  19.      if((hour24>=0)&&(hour24<1)&&(minute24>=0)&&(minute24<=59))
  20.      {
  21.           houramOrPM=12;
  22.           amOrPM= 'A';
  23.      }
  24.      if (hour24==12)
  25.      {
  26.           houramOrPM=12;
  27.           amOrPM='P';
  28.      } 
  29.  
  30.      assert(1 <= houramOrPM && houramOrPM <= 12); /* houramOrPM must be in this range!
  31.      /* add other assert statements here to state result ranges */
  32. }
  33.  
Hope this helps.


Adrian
Feb 11 '07 #5
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
Feb 14 '07 #6
AdrianH
1,251 Expert 1GB
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
Feb 14 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: soup_or_power | last post by:
Hi I have a requirement to compare the user entry for mm/dd/yy/HH/mm (month, day, year, hour, minute) to the Easter Standard Time. I am using the following script, but it is not giving the right...
4
by: Wanhua Yi | last post by:
Hi all, anybody out there with experience in using EMC's Time Finder Software and DB2 UDB EEE on AIX ? Especially in using BCV for Backup ? Any white papers ?
0
by: Miranda Evans | last post by:
I noticed several postings about issues related to "run-time error 3061", and I observed that the solutions to these issues appear to involve correcting something within the SQL code. I'm...
2
by: Glen | last post by:
I'm working on a custom assembly and I'm trying to figure out the best approach to handling known constraints within the assembly, once compiled, to alert the developer at compile time of a...
9
by: BadOmen | last post by:
I know the obvious answer here on the VB.Net... I bought for a couple of years ago Visual Studio 6.0 Pro and I wonder what I have to gain in getting me the .Net version? I am rather new to VB...
2
by: mac11 | last post by:
I have a question about getting the current time. Here is a small program that just gets the time and displays it: #include <stdio.h> #include <time.h> #include <sys/time.h> #include <unistd.h>...
3
by: palepimp | last post by:
Hello all, I have searched far and wide for a solution to my issue. In short, here is the problem: 1. 3 PC's enter data into an Access 2003 database (PC's are running Vista w/ Office 2007...
3
by: Liming | last post by:
Hello All, In our project management software, we just noticed an issue. If team members are across the world, then there is an issue with due dates being consistent. (even though we have not...
4
by: Brian | last post by:
I have a 2000/2002 Access db that I use to collect and store my exercisetime using a form to enter. I wanted to see a summary of the total timefor each exercise so I have a subform that does this....
4
by: Ken Fine | last post by:
I've been living with a frustrating issue with VS.NET for some months now and I need to figure out what the problem is. Hopefully someone has run into the same issue and can suggest a fix. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.