HWZ Forums

Login Register FAQ Mark Forums Read

C program experts lip lai!

Like Tree29Likes
Reply
 
LinkBack Thread Tools
Old 09-07-2017, 12:14 AM   #16
Master Member
 
rollanddie's Avatar
 
Join Date: Aug 2012
Posts: 4,739
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?

~Dragonite~ likes this.
rollanddie is offline   Reply With Quote
Old 09-07-2017, 12:49 AM   #17
Master Member
 
rollanddie's Avatar
 
Join Date: Aug 2012
Posts: 4,739
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?
~Dragonite~ likes this.
rollanddie is offline   Reply With Quote
Old 09-07-2017, 10:46 AM   #18
Master Member
 
Join Date: Apr 2003
Posts: 4,020
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
~Dragonite~ likes this.
peterchan75 is offline   Reply With Quote
Old 09-07-2017, 12:09 PM   #19
Senior Member
 
Join Date: Sep 2000
Posts: 647
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 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.
~Dragonite~ likes this.

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: 23
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.
~Dragonite~ likes this.

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: 647
Eh... if TS is C Programming in Poly, then cannot be too complicated. Must follow syllabus..... Maybe already hand up assignment.
~Dragonite~ likes this.
keenklee is offline   Reply With Quote
Old 15-07-2017, 10:57 PM   #22
Junior Member
 
Join Date: Jul 2017
Posts: 23
Be it poly or uni, if you're doimg programming, it's best to write good codes that cater to all use cases
~Dragonite~ likes this.
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,739
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
~Dragonite~ likes this.
rollanddie is offline   Reply With Quote
Old 17-07-2017, 05:45 PM   #24
Supremacy Member
 
davidktw's Avatar
 
Join Date: Apr 2010
Posts: 8,793
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; }
~Dragonite~ likes this.

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: 647
Good codes start from the basics.
~Dragonite~ likes this.
keenklee is offline   Reply With Quote
Old 25-07-2017, 03:17 PM   #26
Master Member
 
rollanddie's Avatar
 
Join Date: Aug 2012
Posts: 4,739
To score for the project, need some out of syllabus code.

I'm thinking to implement asking the user to input time...

~Dragonite~ likes this.
rollanddie is offline   Reply With Quote
Old 25-07-2017, 03:19 PM   #27
Supremacy Member
 
davidktw's Avatar
 
Join Date: Apr 2010
Posts: 8,793
To score for the project, need some out of syllabus code.

I'm thinking to implement asking the user to input time...
Didn't my code above already managed that ?
~Dragonite~ likes this.
davidktw is offline   Reply With Quote
Old 25-07-2017, 03:21 PM   #28
Master Member
 
rollanddie's Avatar
 
Join Date: Aug 2012
Posts: 4,739
Didn't my code above already managed that ?
Yup you did.

But I'm not sure how to combine that code into my current code

~Dragonite~ likes this.
rollanddie is offline   Reply With Quote
Old 25-07-2017, 03:23 PM   #29
Supremacy Member
 
davidktw's Avatar
 
Join Date: Apr 2010
Posts: 8,793
Yup you did.

But I'm not sure how to combine that code into my current code
Perhaps you should go understand what my codes does.
~Dragonite~ likes this.
davidktw 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