5 Replies - 385 Views - Last Post: 27 January 2010 - 04:23 AM Rate Topic: -----

#1 rey24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-January 10

verify from expert

Post icon  Posted 22 January 2010 - 11:37 PM

Hi Guys,

Need help on how to come out a program with the below display. Appreciate for your advice. Cheers!

Enter hrs, mins and am/pm country A: 10 30 am
Enter time difference (in hrs) of country A & B: 4
Enter hrs and mins of flight duration: 1 20
The arrival local time in city B is 2:50 pm
Is This A Good Question/Topic? 0
  • +

Replies To: verify from expert

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: verify from expert

Posted 22 January 2010 - 11:47 PM

Well... why don't you make an attempt and if you get stuck come back and ask for some advice then? We can't do your work for you...

The task is not very hard at all, pretty basic stuff -- so give it a good attempt and let us know how it turns out.
Was This Post Helpful? 0
  • +
  • -

#3 kcamse0hs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-December 09

Re: verify from expert

Posted 23 January 2010 - 01:31 AM

2:50pm or 3:50pm?
Was This Post Helpful? 0
  • +
  • -

#4 rey24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-January 10

Re: verify from expert

Posted 27 January 2010 - 01:07 AM

This is my prog but i'm sure there's a lot of improvement to be made.

I can't seems to be able to get the am change to pm or vice versa. is this prog acceptable?

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int hour1, minute1;
int hour2, minute2;
int hourdif;
int hourdif1;

int main()
{
cout<<"Enter hrs,mins and am/pm of the time of city A:";
cin>>hour1>>minute1>>timetype;
cout<<"Enter time difference( in hrs) between cities A&B:";
cin>>hour2;

hourdif=hour1+hour2;

if (hourdif > 12)
{
hourdif1 = hourdif - 12 ;

cout<<"The equivalent time of city B is "<<hourdif1<<":"<<minute1<<" "<<timetype<<endl<<endl;
}
else
{
cout<<"The equivalent time of city B is "<<hourdif<<":"<<minute1<<" "<<timetype<<endl<<endl;
}
}


View Postkcamse0hs, on 23 Jan, 2010 - 12:31 AM, said:

2:50pm or 3:50pm?


it's supposed to be 3:50pm bro. sori typo error

*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 27 January 2010 - 06:01 AM

Was This Post Helpful? 0
  • +
  • -

#5 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: verify from expert

Posted 27 January 2010 - 01:08 AM

If you subract 12 from the time diff, then you need to flip the "AM" to "PM" or "PM" to "AM". That code is missing altogether.

This post has been edited by Martyn.Rae: 27 January 2010 - 01:09 AM

Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: verify from expert

Posted 27 January 2010 - 04:23 AM

1 - Use code tags whenever you post code. Like this :code:

2 - Your variable names are not good.

2a - Try to use names which are consistent and meaningful.
How about "hourCityA" where you use "hour1"

2b - Don't declare variables you don't use.
Since you never change the minutes why have more than a single variable "minutes".
There is no value to "minute1" and "minute2".

2c - There is not need to use a new variable to hold a transformed value.
You can just store the new value in the existing variable name.
This is programming, not maths.
x = x + 1;
would be bad in maths but it is correct in programming.
This
hourdif1 = hourdif - 12;
is not good programming.
Ignoring, for the moment, that the variable names are completely misleading (and therefore bad) this
hourdif = hourdif - 12;
is far better coding style.
If we consider some better variable names as well this would be even better
hourCityB = hourCityB - 12;

This post has been edited by janotte: 27 January 2010 - 04:24 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1