C program experts lip lai!

rollanddie

Supremacy Member
Joined
Aug 15, 2012
Messages
5,339
Reaction score
8
i'm facing a problem.

if(time >= 600 && time <= 900 || time >= 1800 && <= 2100)
{
}
else
{
}

but it is always returning as false. any idea why?
 

kuma-mon

Supremacy Member
Joined
Apr 16, 2017
Messages
8,677
Reaction score
2,994
i'm facing a problem.

if(time >= 600 && time <= 900 || time >= 1800 && <= 2100)
{
}
else
{
}

but it is always returning as false. any idea why?

hi

im assuming you are comparing the time, as in the system time value? it wont work because it is not even an integer. it is a set of values, eg: DDMMYYYYHHMMSS

system.out.println your time value first and check if its the value you want. you have to get the seconds/milliseconds of the time and convert it to integer.
 

rollanddie

Supremacy Member
Joined
Aug 15, 2012
Messages
5,339
Reaction score
8
hi

im assuming you are comparing the time, as in the system time value? it wont work because it is not even an integer. it is a set of values, eg: DDMMYYYYHHMMSS

system.out.println your time value first and check if its the value you want. you have to get the seconds/milliseconds of the time and convert it to integer.

not so complicated :D:D

i'm getting the user to input the time in 24hr format e.g. 0600 0700 1859 1959. doesn't matter if the user keys above 59 for the minute hand.
 

kuma-mon

Supremacy Member
Joined
Apr 16, 2017
Messages
8,677
Reaction score
2,994
not so complicated :D:D

i'm getting the user to input the time in 24hr format e.g. 0600 0700 1859 1959. doesn't matter if the user keys above 59 for the minute hand.

icic,

for this case, i think you can just simply convert the input to int, then compare the values.

for eg: 0600 input is not a valid int, because got 0 infront, so u have to remove the 0. go google on how to remove 0 infront.

you have to also consider input like 00001, and 0000. so this would be converted to 1 and 0 in int.

then you can compare 1 > 600.
 

keenklee

Arch-Supremacy Member
Joined
Sep 9, 2000
Messages
11,469
Reaction score
3,524
printf("input time(24hr, HHMM format): );
scanf("%d %d", &hr, &min);

Then how can i include the HHMM in the if statement condition like the quote below?


Sent from 1000char using GAGT

I think you should just have 1 input for time.
How the user type it depends on your instruction in the printf.

printf("\nDear User, please enter the time in 24-hours format (HHMM)\n);
scanf("%d",&hr_min);

If you are starting C programming, scanf is just a basic input function - it cannot stop the user from typing in 000600. I think 0600 would be accepted as 600.
 
Last edited:

Zehjie374

Junior Member
Joined
Jul 6, 2017
Messages
23
Reaction score
0
Not really a C expert but I think u can printf instructions asking user to enter in HHMM then do a length check on the user input if it's == 4

If it is 4, use atoi to convert it to int and do your relevant if else conditions

Else if the number of characters entered is not 4, prompt the user to enter the time again,

Make sure u do some error checking to check if it's all numeric.. iirc atoi doesn't do error checking.
 
Last edited:
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ. Forum members and moderators are responsible for their own posts.

Please refer to our Community Guidelines and Standards, Terms of Service and Member T&Cs for more information.
Top