help???

somehow im making a program that goes like this:(and if possible you guys get the idea of it)
#include<iostream>
using namespace std;

int a,b,c;

int main();
{
cout<<"Enter number of hours:";
cin>>a;
cout<<"Enter number of minutes:";
cin>>b;
cout<<"Select time zone (-12 to +12):";
cin>>c;

switch(c)
{
case '-12':
a=(a-12);
cout<<a<<":"<<b;
break;
case '-11':
a=(a-11);
cout<<a<<":"<<b;
}
sytsem("pause");
return 0;
}




is this right or wrong or am i missing something?
c is an int. this '-12' classifies a char.

instead of the switch you can simply do this:
a+=c;

Edit: you need to be aware that negative hours don't exist and you need to evaluate that to an existing hour
Last edited on
Well, the code above doesn't compile, there are several errors. As for the overall approach, the switch-case mechanism here is an unwieldy approach, as there are 25 different options as well as a default (out of range).

A simpler approach would use addition and the modulo operator % to get the result with much less code.
ok, like in class, head hurts, but i get the idea......
so what will be the new code for it though?
something like this, assuming you use a 24-hour clock. Otherwise you need to calculate am/pm too.
int adjusted_hour = (hour + time_zone + 24) % 24;
closed account (o3hC5Di1)
To expand on previous posts:

If you wrap something in single quotes , such as '-12' it is considered by C++ as a character literal, which means that it represents a single character, like 'a'. So that is one source of errors.

Because the timezone can be negative, let's take -12 as an example, when you add the numbers:
a (=23) + (-12) , evaluates to 23 -12 = 11. Off course, you need to check for when the result goes below zero:

5-12 = -7, in which case you need to subtract 7 from 24: 24-7 = 17. (Because -7 is the amount of hours you need to subtract from the previous day. The same goes for any result higher than 7.

There is an easier way to achieve this last feature using the modulo operator.
This will give you the remainder of a division, for instance: 24%27 27%24 = 3
So suppose a= 15 and c=+10, a+c=25 => 25%24=1, so 1AM in timezone C.

Hope that clears it up for you,you don't need all those switch statements.

All the best,
NwN
Last edited on
@NwN I'm sure it was a typo - 24%27 = 3 is not correct.
In fact, 24%27 = 24
I'm pretty sure it was supposed to be 27%24 = 3
closed account (o3hC5Di1)
@Chervil

I'm pretty sure I'm just absolutely horrible at Mathematics :D
Thanks for pointing that out, you are correct indeed.

All the best,
NwN
so im not good in math or logic maybe?,
but how do i make like a rule that the new time will not go over 24 or something like that
linEmoguy wrote:
how do i make like a rule that the new time will not go over 24 or something like that

http://www.cplusplus.com/forum/beginner/90253/#msg485212
ah, ok,
thanks for the help
Topic archived. No new replies allowed.