Title: Can someone help me with Time.
gong - December 30, 2006 02:27 AM (GMT)
Write a menu-driven program that repeatedly allows the user to select one of the following tasks:
(Task 1) User enters the time of city A, time difference between cities A and B, and the program will output the equivalent time of city B.
Define and implement a class Time. Time difference is a positive value if time of city B is ahead of city A. It is a negative value if time of city B is behind city A.
Typical Sample Runs for Task 1 (user inputs in bold):
Enter hrs, mins and am/pm of time of city A: 2 10 pm
Enter time difference (in hrs) between cities A & B: 6
The equivalent time of city B is 8:10 pm.
Enter hrs, mins and am/pm of time of city A: 9 45 am
Enter time difference (in hrs) between cities A & B: -3
The equivalent time of city B is 6:45 am.
Can someone help me how to start. My C++ not so good.. Thanks.
Bench - December 31, 2006 12:27 AM (GMT)
You could start by listing the data you need to store ... This will give you an idea of which data members you need in your class - That will probably include everything you need to retrieve from the user.
Then work out which calculations (Processes) you need to perform.... eg, How would you work out the time difference between London and New York on paper? The processes will be your member functions.
The easiest way to solve a problem is to work it out first on paper - if you do a bit of design work, you'll have a much clearer idea of what you need to do when you're actually writing the program.
Don't build the whole thing at once - When you add something to your program, compile & test it - this way, you'll pick up errors straight away, and they'll be easy to troubleshoot. Don't try building the entire program at once.
When testing, it might be an idea to use some pre-defined values at first, before you attempt to create the user-input side of the program.
gong - January 3, 2007 03:52 AM (GMT)
Thanks...but i really cant solve the problem. i try so many time still cant work. can someone help me. stress. :(
tubapro12 - January 3, 2007 04:09 AM (GMT)
Just sit and think out the problem at hand. If minutes a + minutes b is greater than 60, remove 60 as needed and add the number of times you removed 60 to the hours. Figure AM/PM last I'd say. Once you've figured out how to add times it shouldn't be too hard to figure out how to subtract them.
Besides, we don't do homework for very cheap around here. ;)
TheHawgMaster - January 3, 2007 05:07 AM (GMT)
Try only think about the time in terms of minutes (Or seconds or whatever), only translate to H:M form when all calculations are finished (If you need to at all) The time at the other city will simply be the sum of the time at the first city and the time difference.
A couple of the first questions you should be asking are
what unit of time am I going to use internally? (Hours/Minutes/Seconds or whatever for varying accuracy) and
how am I going to turn that into an object (What variables am I going to need in a Class to represent this concept?)
So perhaps you'll end up with a class that stores time values as signed integers. After you get a couple basic things in place it's often easier to keep going :)
The C standard library has
extensive support for this type of thing as well, though using them extensively might defeat part of the purpose of this exercise.
Shonoby - January 4, 2007 12:49 AM (GMT)
Well, one of the ways to approch this problem is with a object-oriented approch which should be used with, classes, and other type of object oriented keywords type and if u want a template look at the date writer i created with user input, all you have to do is follow some of the examples already implemented in the program and just change it according to your specifications, and the date write is is
hereso there is a plain example using month, day, hour, minute, but if u alterate u can make it with hour, minute, seconds(optional), then change the values and set it as city A then make another set of hour, minute, seconds(optional) for city B
But if u can figure it out all u will need is just one set of the time values, which is simple enough, if u know enough about operation overloading (not
template <class ......> )
gong - January 10, 2007 01:59 PM (GMT)
I'm stuck. can someone help me.
This is what i do. I cant write the am/pm and cant find the equivalent time of city B.
thanks
| CODE |
#include <iostream> using namespace std;
class Time { private: int hrs, mins;
public: int h; int m; int h1; void output(); };
void main() { Time equivalent; int h; int h1; int total= h + h1;
cout << "Enter hrs, mins and am/pm of time of city A: "; cin >> equivalent.h >> equivalent.h1; cout << "Enter time difference (in hrs) between cities A & B: "; cin >> equivalent.h1;
cout << "The equivalent time of city B is: " << total << endl; }
|
myork - January 10, 2007 04:16 PM (GMT)
| CODE |
#include <iostream> using namespace std;
class Time { private: int hrs; int mins; bool isPM;
public: Time(int hrs,int min,bool isPM); void output(); };
void main() { int hrs; int mns; char AMPM;
cout << "City A: Enter hrs" << std::endl; cin >> hrs;
cout << "City A: Enter mins" << std::endl; cin >> mns;
do { cout << "City A: AM (A) or PM (P)" << std::endl; cin >> AMPM; } while (!(AMPM == 'A' || AMPM == 'P'));
Time CityA(hrs,mns,AMPM == 'P');
// etc
} |
Shonoby - January 10, 2007 08:59 PM (GMT)
Well "myork", i know that u alrdy gave him the code that would work but not efficiently, i fixed my code to do what u wanted and i had a couple 'if' and 'else' struc. that change your inputed values to 'City B' by 2 hours and 10 minutes ( just a rand() i chose ). Well here is the code i will explain after the code( working code ):
| CODE |
// Time Zones Of City ( A, B ) // By: Darwin Aguirre // Idea By: gong #include <iostream>
using std::cout; using std::cin; using std::endl;
#include <iomanip>
using std::setfill; using std::setw;
class Time { public: Time(); void setTime( int, int ); void printsetUp(); void print_A(); void print_B();
private: int h; int m; };
Time::Time() { h = m = 0; }
void Time::setTime( int p, int l ) { h = ( p >= 0 && p < 24 ) ? p : 0; m = ( l >= 0 && l < 60 ) ? l : 0; }
void Time::printsetUp() { cout << setfill( '0' ) << setw( 2 ) << h << ":" << setw( 2 ) << m; }
void Time::print_A() { cout << ( ( h == 0 || h == 12 ) ? 12 : h % 12 ) << ":" << setfill( '0' ) << setw( 2 ) << m << ( h < 12 ? " AM" : " PM" ) << endl; }
void Time::print_B() { cout << ( ( h == 0 || h == 12 ) ? 12 : h % 12 ) << ":" << setfill( '0' ) << setw( 2 ) << m << ( h < 12 ? " AM" : " PM" ) << endl; }
int main() { Time t; int h; int m;
cout << "Current values are:\n"; cout << "City A: "; t.print_A(); cout << "City B: "; t.print_B(); cout << "Enter values for City A:\nHour: "; cin >> h; cout << "Minute: "; cin >> m;
t.setTime( h, m ); cout << "City A: "; t.print_A();
if ( h <= 2 ) h -= 0; else if ( h > 2 && h < 24 ) h -= 2;
if ( m <= 5 ) m -= 0; else if ( m > 2 && m < 60 ) m -= 10;
t.setTime( h, m ); cout << "City B: "; t.print_B();
return 0; }
|
Ok, well for starters, when u are going to make a 'class', always put 'functions' ( prototypes ) in 'public', instead of values because that is considered poor programming, not only that but because we are showing the 'client' how we create the program which is also poor programming.
So always put 'functions' in 'public'( lines 17-22 ) and put values in 'private'( lines 24-26 ),so that we can hide how we created the code from the 'client'. So just keep that in mind, in line 18, we see something that looks like it has not been initialized, but in reality it has, it is a 'classes' 'constructor'( is almost like a default value, and have the same name as the class )( line 28-31 ), what this does is set the values in 'private' storage class to the programmer desires value(s), respectively. This is done because values in a 'class' can not be set to a value inside the 'class', so the 'constructor' does this for 'class'. 'class', 'public' 'function' 'setTime( int, int )'( lines 33-37 ), is used so that the values can never go over 23 ( for hours, also normal day time ), and 59 ( for minutes, also normal day time ). 'class', 'public' 'function' 'printsetUp()'( lines 39-43 ) is what tells 'functions' 'print_A()'( lines 45-50 ) && 'print_B()'( lines 52-57 ), if there is a 0 needed in the place of hours or minutes, so we can tell the difference between tens and ones, or not needed. 'class', 'public' 'functions' 'print_A()' && 'print_B()', prints it's time and also tells the compiler if it is AM || PM and also display 12 hours if value is 0 ( 'Time()' 'constructor' ).
Now 'int main()'( lines 59-94 ), uses all of the 'classes' 'public' 'functions'. Values 'int' 'h' && 'm' are like the operation overloading for 'classes' 'private' because they are used to invoke 'class', 'public' 'function' 'setTime()'. After the user has inputed the values in for hour and minutes, they are displayed as 'City A', the 2 'if' && 2 'else' strucs.( lines 79-87 ) are what changes the value of 'City A' to 'City B' like the beginning of this post said.
Do Not Manipulate All Over Web
Rights Signed And Owned By MeLOL, yea but for real don't. :lol:
P.S: To make the values PM || AM, u have to use universal time, it is explained
here
myork - January 10, 2007 10:39 PM (GMT)
| QUOTE (Shonoby @ Jan 10 2007, 03:59 PM) |
| Well "myork", i know that u alrdy gave him the code that would work but not efficiently, |
Now that is a funny statment.
You should be a commmedian :-)
Back to designing real code :)
Time is probably a bad name for a class unless you want to put it in a specific namespace. So I have just renamed it CityTime.
Given your first post you need two constructors.
1) That takes a time value.
2) That takes another city and an offset.
You will also need a ouput operator to print the object.
Here is how I would design the class. I leave the implementation to you.
| CODE |
class CityTime { public: CityTime(int hrs,int min,bool isPM); CityTime(const CityTime& city1,int timeDiffInHours);
private: friend std::ostream& operator<<(std::ostream& s,const CityTime& cityTime); int hrs; int min; bool isPM; };
std::ostream& operator<<(std::ostream& s,const CityTime& cityTime); |
Shonoby - January 10, 2007 11:11 PM (GMT)
Mmm, true true, well not i am not a commmedian but anways ur right about, needing TWO(u spelled it wrong LOL), yea u do but given that u have more experience than me u should, be able to beat any of my codes any day, and also i don't know about friend yet and not do tell me what it is my book will teach me soon enough, but yea that is a better way to approach the time situation...(but i dont really understand it but w/e LOL ^_^ )
Ravotus - January 10, 2007 11:21 PM (GMT)
Maybe before learing more of the C++ language you should brush up on the English language... :ph43r:
Shonoby - January 10, 2007 11:43 PM (GMT)
Damn it i don't care if i cant spell, thats y Microsoft developed
SPELL CHECK
myork - January 11, 2007 03:15 PM (GMT)
| QUOTE (Shonoby @ Jan 10 2007, 06:11 PM) |
| Mmm, true true, well not i am not a commmedian but anways ur right about, needing TWO(u spelled it wrong LOL), yea u do but given that u have more experience than me u should, be able to beat any of my codes any day, and also i don't know about friend yet and not do tell me what it is my book will teach me soon enough, but yea that is a better way to approach the time situation...(but i dont really understand it but w/e LOL ^_^ ) |
Sorry, the google translater failed to turn that into English :)
All good fun B)
You are right about encapsulation.
All variables should be private members. Access to these members should be via public methods. This allows:
1) Controlled access to the state of the variables (ie you can check for valid values)
2) Allows you to do other things like logging when a variable is accessed.
3) Allows you to change the implementation without the need to change code that uses the class (as long as the public methods are not changed).
This also makes things like the docorator patter easy to implement (Note relavant to this thread but I though I would drop another name so that aspiring programmers could look it up).