Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,269 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,461 people online right now. Registration is fast and FREE... Join Now!




Time conversion

2 Pages V  1 2 >  
Reply to this topicStart new topic

Time conversion, AM to PM

dinci5
31 Oct, 2006 - 04:39 AM
Post #1

New D.I.C Head
*

Joined: 19 Oct, 2006
Posts: 33


My Contributions
Hi

I have an exercise where I have to write a program that converts from AM to PM

here is it:

Write a program that converts from 24-hour notation to 12-hour notation. For exmple, it should co,vert 14:25 to 2:25 PM. The input is given as two integers. There should e at least three functions, one for input, one to do the conversion and one for outpu. Record the AM/PM information as a value of type char, 'A' for Am and 'P' for PM. Thus, the function for doing the conversion will have a call-by-reference formal parameter of type char to record whether it is AM or PM. (The function will have other parameters as well)



I don't want you to do this exersice for me. That's not my style.
I need some help on how to start. to be more specific, I need help with the conersion part ... rolleyes.gif

Oh yeah, and a lil' explenation on call-by-reference
The explanation in my book is very complex, I don't know if I undestood everything huh.gif




I hope you guys will help
User is offlineProfile CardPM
+Quote Post

Xing
RE: Time Conversion
31 Oct, 2006 - 05:40 AM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
QUOTE(dinci5 @ 31 Oct, 2006 - 06:09 PM) *
Oh yeah, and a lil' explenation on call-by-reference
The explanation in my book is very complex, I don't know if I undestood everything huh.gif


http://www.cprogramming.com/tutorial/references.html
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Time Conversion
31 Oct, 2006 - 06:19 AM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
We can absolutely provide some guidance. Xing has provided a very good explanation of call by reference. As for the code, can you please post what you have tried so far?
User is offlineProfile CardPM
+Quote Post

DeeViLiSh
RE: Time Conversion
31 Oct, 2006 - 06:30 AM
Post #4

D.I.C Head
Group Icon

Joined: 25 Jul, 2006
Posts: 175



Thanked: 1 times
Dream Kudos: 575
My Contributions
Use the modulo operator for 24h to 12h conversion.
If the hours%12 > 1, then output PM else AM.

That's how I would have done it.
What have you done so far? huh.gif

-DeeViLiSh
User is offlineProfile CardPM
+Quote Post

dinci5
RE: Time Conversion
31 Oct, 2006 - 08:44 AM
Post #5

New D.I.C Head
*

Joined: 19 Oct, 2006
Posts: 33


My Contributions
I didn't do much sad.gif
cuz I didn't know how to start with the conversion
I will post here my code when I'm done
I'm working on it biggrin.gif

Xing thx for the link

This post has been edited by dinci5: 31 Oct, 2006 - 08:45 AM
User is offlineProfile CardPM
+Quote Post

Dark_Nexus
RE: Time Conversion
31 Oct, 2006 - 03:21 PM
Post #6

or something bad...real bad.
Group Icon

Joined: 2 May, 2004
Posts: 1,309



Thanked: 3 times
Dream Kudos: 625
My Contributions
if the hour is greater than 12, just subtract 12 and then tag on a pm
User is offlineProfile CardPM
+Quote Post

dinci5
RE: Time Conversion
2 Nov, 2006 - 04:41 AM
Post #7

New D.I.C Head
*

Joined: 19 Oct, 2006
Posts: 33


My Contributions
I'm sorry for a,swering this late
I didn't have time yesterday sad.gif sad.gif

here is the code that I have till now

CODE
#include <iostream>
using namespace std;


int main( )
{
    int hours, minutes;

    cout << "Enter hours: ";
    cin >> hours;

    cout << "Enter minutes: ";
    cin >> minutes;



    if (hours >= 12) {
        hours = hours - 12;
        cout << "It is " << hours;
        cout << ":" << minutes << "  PM" << endl;
    }
    
    else if (hours <= 12) {
        cout << "It is " << hours;
        cout << ":" << minutes << "  AM" << "\n\n";
    }

        return 0;
}




the conversion is not a problem, but there is another problem that I don't understand

I have to do it this way:

The input is given as two integers. There should be at least three functions, one for input, one to do the conversion and one for output. Record the AM/PM information as a value of type char, 'A' for Am and 'P' for PM. Thus, the function for doing the conversion will have a call-by-reference formal parameter of type char to record whether it is AM or PM.



"Record the AM/PM information as a value of type char"
I don't understand this sad.gif
I this sounds stupid, but I just started with c++ and I really don't get it


any suggestions on this?


thx for the help guys, you really are greate

This post has been edited by dinci5: 2 Nov, 2006 - 04:43 AM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Time Conversion
2 Nov, 2006 - 07:04 AM
Post #8

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Well, it appears as if you will have to modify your program to use functions for the work you're doing, butfor the portion you're asking about, it is simply asking you to set an additional variable for AM or PM. The function may look something like this (example only, not meant for compilation)
CODE

void setAMorPM(int hours, char &indicator)
{
   if(hours>12)
      indicator='P';
   else
      indicator='A';
}

As an FYI...you do not need an else if statement in your decision code...an else will suffice.
CODE

if (hours >12) {
        hours = hours - 12;
        cout << "It is " << hours;
        cout << ":" << minutes << "  PM" << endl;
    }
    
    else{
        cout << "It is " << hours;
        cout << ":" << minutes << "  AM" << "\n\n";
    }


User is offlineProfile CardPM
+Quote Post

dinci5
RE: Time Conversion
2 Nov, 2006 - 09:52 AM
Post #9

New D.I.C Head
*

Joined: 19 Oct, 2006
Posts: 33


My Contributions
I don't get it sad.gif
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Time Conversion
2 Nov, 2006 - 10:03 AM
Post #10

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Can you specify what it is you would like a deeper explanation of?
User is offlineProfile CardPM
+Quote Post

dinci5
RE: Time Conversion
2 Nov, 2006 - 10:26 AM
Post #11

New D.I.C Head
*

Joined: 19 Oct, 2006
Posts: 33


My Contributions
"Record the AM/PM information as a value of type char, 'A' for Am and 'P' for PM."

how can I do that when the input has to be an integer

I'm confused


"Thus, the function for doing the conversion will have a call-by-reference formal parameter of type char to record whether it is AM or PM"

I don't know how to do this ... I've read the text on the link above, but I don't get it
User is offlineProfile CardPM
+Quote Post

BitByte
RE: Time Conversion
2 Nov, 2006 - 10:36 AM
Post #12

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 2 times
My Contributions
It does not have to be an integer. You even put this in your post:

QUOTE
"Record the AM/PM information as a value of type char, 'A' for Am and 'P' for PM."


Type char is not an integer. Amadeus has given you the idea on how to do it.

This post has been edited by BitByte: 2 Nov, 2006 - 10:38 AM
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/4/08 07:45PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month