-
Need help with time
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.
Im now doing on this and got stuck.
This is what i do. The code is below
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;
}
can someone help me. Im very poor on my C++
thanks.
-
Code:
class Time
{
public:
void set_time(int, int, std::string);
Time add_hours(int);
void print();
private:
int hours, minutes;
std::string am_pm;
};
How about something like that for the class prototype. Now try to figure out what the parameters for the methods mean, and try to implement them. Using this class in a program should be a piece of cake, if you got that done.
(Also note, that your code contains public data members. Don't do that! Data has to be private, especially since you may need to validate the parameters first. With public data, you're class would accept times such as 100.-23 FM.)
-
I have redo the program and come out of this.
Code:
#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;
}
can someone help me.
I want the user to key in the time difference (in hrs) between cities A & B like and show the equivalent time of city B.
The user can key in any number in the time difference (in hrs) between cities A & B. ex negative or positive number.
thanks
-
What do A and B refer to in your code?
-
So who wrote that newer piece of code?
It was clearly not written by the same person.
-
Well, this code looks very elegant, except that Time::print_A and Time::print_B are identical, Time::printSetup is unused (and not needed - and should be a private method anyway).
It looks as if you got some code from a solution to an earlier assignment (which you probably failed / stole from somebody), copied the print() function (because you have no idea what variables are and how classes work - that you make different instances of a class and call the same methods on them, not the other way round), and wrote a stupid driver program that doesn't take you one step closer to the solution.
Adding hours to a given time should be done by a class method, not in main.
Try harder.
-
Gong is learning from the popular Detiel C++ programming books. I have done this assignment, and like anon said it's quite easy with the correct class. The book does tell you before this task how to set a class out correctly. After this he will have to do an assignment about a post office. Good luck gong.
-
If he's learning from a book, then I'm sorry for my harsh words.
But really, instead of struggling on without understanding the basics, find the place where things got confusing, reread, do the exercises and try out the new concepts with similar programs.