HWZ Forums

Login Register FAQ Mark Forums Read

C program experts lip lai!

Share This Page
HardwareZone.com on Facebook
Reply
 
LinkBack Thread Tools
Old 09-07-2017, 12:14 AM   #16
Master Member
 
rollanddie's Avatar
 
Join Date: Aug 2012
Posts: 4,194
Can I just put 600 instead of 0600 to make it a valid int?

Will the system capture it as 600 if user keys 0600?

Sent from 1000char using GAGT
rollanddie is online now   Reply With Quote
Old 09-07-2017, 12:49 AM   #17
Master Member
 
rollanddie's Avatar
 
Join Date: Aug 2012
Posts: 4,194
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?
i'm facing a problem.

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

but it is always returning as false. any idea why?
Sent from 1000char using GAGT
rollanddie is online now   Reply With Quote
Old 09-07-2017, 10:46 AM   #18
Master Member
 
Join Date: Apr 2003
Posts: 3,881
If you are doing calculation on time, then it should be easier to deal with it in Unix time.

https://www.epochconverter.com/programming/c
peterchan75 is offline   Reply With Quote
Old 09-07-2017, 12:09 PM   #19
Senior Member
 
Join Date: Sep 2000
Posts: 552
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 by keenklee; 09-07-2017 at 12:11 PM..
keenklee is offline   Reply With Quote
Old 15-07-2017, 03:49 AM   #20
Junior Member
 
Join Date: Jul 2017
Posts: 10
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 by Zehjie374; 15-07-2017 at 05:48 AM..
Zehjie374 is offline   Reply With Quote
Old 15-07-2017, 08:14 PM   #21
Senior Member
 
Join Date: Sep 2000
Posts: 552
Eh... if TS is C Programming in Poly, then cannot be too complicated. Must follow syllabus..... Maybe already hand up assignment.
keenklee is offline   Reply With Quote
Old 15-07-2017, 10:57 PM   #22
Junior Member
 
Join Date: Jul 2017
Posts: 10
Be it poly or uni, if you're doimg programming, it's best to write good codes that cater to all use cases
Zehjie374 is offline   Reply With Quote
Old 16-07-2017, 02:27 PM   #23
Master Member
 
rollanddie's Avatar
 
Join Date: Aug 2012
Posts: 4,194
Eh... if TS is C Programming in Poly, then cannot be too complicated. Must follow syllabus..... Maybe already hand up assignment.
It's a mini project sort of program. I believe having some out of syllabus coding will be considered as well.

Nevertheless, I appreciate all the inputs

Posted with D6653 via PCWX
rollanddie is online now   Reply With Quote
Old 17-07-2017, 05:45 PM   #24
Supremacy Member
 
davidktw's Avatar
 
Join Date: Apr 2010
Posts: 8,453
You can always try something like this. scanf is very functional, but you need to know how to use it properly to serve you. Also get to know variants of it.

https://goo.gl/sqevSe
#include <stdio.h> int isCorrectDate(int _date[]) { // just a placeholder return 1; } int isCorrectTime(int _time[]) { return _time[0] >= 0 && _time[0] <= 24 && _time[1] >= 0 && _time[1] <= 60 && _time[2] >= 0 && _time[2] <= 60; } int main() { char byte[1024]; int _date[3]; int _time[3]; while (1) { printf("Enter datetime [DD/MM/YYYY HH:MM:SS]\n"); fgets(byte, 1024, stdin); if (sscanf(byte, "%2d/%2d/%4d %2d:%2d:%2d", _date, _date+1, _date+2, _time, _time+1, _time+2) != 6) { fprintf(stderr, "ERROR: Incorrect input - Please try again\n"); } else { if (isCorrectDate(_date) && isCorrectTime(_time)) { printf("%02d/%02d/%04d %02d:%02d:%02d\n", _date[0], _date[1], _date[2], _time[0], _time[1], _time[2]); break; } else { fprintf(stderr, "ERROR: Incorrect input range - Please try again\n"); } } } return 0; }

Last edited by davidktw; 17-07-2017 at 06:50 PM..
davidktw is offline   Reply With Quote
Old 17-07-2017, 07:24 PM   #25
Senior Member
 
Join Date: Sep 2000
Posts: 552
Good codes start from the basics.
keenklee is offline   Reply With Quote
Reply
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 Terms of Service for more information.


Thread Tools

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On