Validating help in C - c

I'm trying to get this validation for a date entered, from Jan 01 1901 to Dec 31 2299, if the date is out of range, give a error message, this is my code, some how it doesn't validate correctly, am I doing something wrong
int main (void)
{
// insert code here...
int day,month,year;
printf("Enter Year, Month and Day as YYYY,MM,DD\n");
scanf("4%d 2%d 2%d", &year, &month, &day);
if (year>1900 && year <2300) {
if (month>=1 && month <=12)
if (day >=1 && day <=31)
printf("correct/n");
else
printf("invalid/n");
}
return 0;
}

The problem is that you're using nested if statements, but you don't have enough else statements. If you simply consolidate the statements it should work.
if (year > 1900 && year < 2300 &&
month > 0 && month < 13 &&
day > 0 && day < 32)
{
printf("correct\n");
}
else
{
printf("invalid\n");
}

If I were doing this, I think I'd start with a small function like:
int in_range(int val, int lower, int upper) {
return val >= lower && val <= upper;
}
Then I'd write something like:
if (in_range(year, 1901, 2299) && in_range(month, 1, 12) && in_range(day, 1, 31))
printf("Corrent\n");
else
printf("invalid\n";

You don't say anything for a number of conditions. Fix:
if (year>1900 && year <2300) {
if (month>=1 && month <=12) {
if (day >=1 && day <=31)
printf("correct/n");
else
printf("invalid/n");
} else {
printf("invalid/n");
} else {
printf("invalid/n");
}
Of course, you could collapse that code to avoid redundancies.
if ( year > 1900 && year < 2300
&& month >= 1 && month <= 12
&& day >= 1 && day <= 31
) {
printf("correct/n");
} else {
printf("invalid/n");
}
I'd move that logic into a function.
int is_valid_date(int year, int month, int day) {
return ( year > 1900 && year < 2300
&& month >= 1 && month <= 12
&& day >= 1 && day <= 31
);
}
if (is_valid_date(year, month, day)){
printf("correct/n");
} else {
printf("invalid/n");
}

Related

Using macro as a range in C. Nested Loops. Logic

I am trying to make this program which takes YYYY and MM from the user. I have defined macros outside the main function, which is kinda range for years to be taken as input. For months, i have declared JAN and DEC as 1 and 12 respectively. What I am trying to is,show an error when something(month and year) is out of range, and the loop should repeat untill the right input has been entered. I tried my best to do that, but i didnt get anything. here is my code.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define MIN_YEAR 2012
#define MAX_YEAR 2022
int main(void)
{
const int JAN = 1;
const int DEC = 12;
int year = 0;
int month = 0;
do
{
printf("Set the year and month(YYYY MM): ");
scanf(" %d %d", &year, &month);
if ((MIN_YEAR > year && year > MAX_YEAR))
{
printf("ERROR: The year must be between 2012 and 2022 inclusive");
}
if ((JAN >= month >= DEC))
{
printf("ERROR: Jan.(1) - Dec.(12)");
}
else
{
printf("*** Log date set! ***");
}
} while (MIN_YEAR <= year <= MAX_YEAR , JAN <= month <= DEC);
return 0;
}
This condition
if ((MIN_YEAR > year && year > MAX_YEAR))
is logically incorrect. It seems you mean
if ( year < MIN_YEAR || year > MAX_YEAR )
The condition in this if statement
if ((JAN >= month >= DEC))
will always evaluate to false.
It is equivalent to
if ( (JAN >= month ) >= DEC )
So the sub-expression (JAN >= month ) evaluates either to 0 or 1 that in any case is less than DEC.
It seems you mean
if ( month < JAN || month > DEC )
In this condition
while (MIN_YEAR <= year <= MAX_YEAR , JAN <= month <= DEC)
there is used the comma operator. Its value is the values of the second operand JAN <= month <= DEC. That is as the first operand has no side effect then in fact the first operand MIN_YEAR <= year <= MAX_YEAR is ignored and does not influence on the result.
It seems you mean
while ( year < MIN_YEAR || year > MAX_YEAR || month < JAN || month > DEC)
Also there is another logical error
if ( ... )
{
printf("ERROR: The year must be between 2012 and 2022 inclusive");
}
if ( ... )
{
printf("ERROR: Jan.(1) - Dec.(12)");
}
else
{
printf("*** Log date set! ***");
}
The else statements belongs to the second if statement. So even if the year was entered incorrectly but the month was specified correctly then the else statement gets the control.
You should write for example
if ( ... )
{
printf("ERROR: The year must be between 2012 and 2022 inclusive");
}
else if ( ... )
{
printf("ERROR: Jan.(1) - Dec.(12)");
}
else
{
printf("*** Log date set! ***");
}

Validate a date up to current time

i'm doing a program in c that needs to have random dates that have to be valid and after the current date. For the generation there aren't any problems at all. The only thing i can't do is to accept only the up to date dates.
I've already tried to firstly check if the date is valid and then to check if is greater than current date. Then i've tried to do the opposite but any of these solutions didn't worked.
This is the function that checks the date:
short dateControl( const unsigned short day, const unsigned short month, const unsigned short year, const int min_year, const int max_year, struct tm curTime)
{
short correct = ZERO;
if( (year >= curTime.tm_year) && (year <= max_year) ){
if( (month >= 1) && (month <= 12) ){
if( (day >= 1 && day <= 31) && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) ){
correct = ONE;
}else if( (day >= 1 && day <= 30) && (month == 4 || month == 6 || month == 9 || month == 11) ){
correct = ONE;
}else if( (day >= 1 && day <= 28) && (month == 2) ){
correct = ONE;
}else if( day == 29 && month == 2 && (year % 400 == 0 ||(year % 4 == 0 && year % 100 != 0)) ){
correct = ONE;
}else{
correct = ZERO;
}
} else{
correct = ZERO;
}
}else{
correct = ZERO;
}
if(correct == ONE){
if( year > curTime.tm_year ){
correct = ONE;
}else if( year == curTime.tm_year ){
if( month > curTime.tm_mon ){
correct = ONE;
}else if( month == curTime.tm_mon ){
if( day > curTime.tm_mday ){
correct = ONE;
}else if(day == curTime.tm_mday){
correct = ZERO;
}else if(day < curTime.tm_mday){
correct = ZERO;
}
}else if( month < curTime.tm_mon ){
correct = ZERO;
}
}
}
}
return correct;
}
This is the part where the date is generated
day = random_int(1, 31);
month = random_int(1, 12);
year = random_int(local->tm_year, local->tm_year + 1);
validDate = dateControl(tempTrip.dep_date.day, tempTrip.dep_date.month, tempTrip.dep_date.year, local->tm_year, (local->tm_year + 1), *local);
while(validDate == ZERO){
tempDriver.sub_date.day = random_int(1, 31);
tempDriver.sub_date.month = random_int(1, 12);
tempDriver.sub_date.year = random_int(local->tm_year, (local->tm_year + 1));
validDate = dateControl(tempTrip.dep_date.day, tempTrip.dep_date.month, tempTrip.dep_date.year, local->tm_year, (local->tm_year + 1), *local);
}
}
I expect that if the generated date is 2/7/2019 and today is the 18/8/2019, the generated date needs to be discarded and another needs to be generated.
i'm doing a program in c that needs to have random dates that have to be valid and after the current date. For the generation there aren't any problems at all. The only thing i can't do is to accept only the up to date dates.
The fact that you need to determine if the date is/isn't valid indicates that the way you're generating random dates is a problem.
Specifically, if you get the current time (using time()) and add a random positive value to it (while being careful to avoid overflow), and then convert the result into a date (e.g. using gmtime()); then you can guarantee that the random dates are valid and in the future without doing any checking and you can delete all of your dateControl() code.

C program that reads in a date and prints out the next day date

Hi I have just started learning C language at University. Now I am faced with a problem I just don't know how to solve. So far we have only used
library and nothing else, also scanf is a topic we are going to
learn next week. For now we just use printf for everything.
I have learned how to say if a year is a leap year or not, however, my task is the next one: Need to create a program that reads in a date and prints out the next day date with output like this:
Enter a date in the form day month year: 17 5 2010
The date of the next day is: 18/5/2010
My dilemma is that I have no idea on what operations to use or how to set the code to make sure that leap year is considered and for example if today's date is 28 2 2010 the next date needs to be 1 3 2010 because it wasn't leap year.
The only library used is and there is no scanf yet (with scanf yet)
So far I got this:
#include <stdio.h>
int day, month, year, ndays, leapdays;
bool leapyear;
int main () {
day = 28;
month = 2;
year = 2010;
ndays = day + 1;
leapdays = 31;
leapyear = false;
if (leapyear % 4 == 0) {
leapyear = true;
}
if (leapyear % 100 == 0) {
leapyear = false;
}
if (leapyear % 400 == 0) {
leapyear = true;
}
if ((leapyear) && (month == 12 || month == 1 || month == 3 || month == 5
|| month == 7 || month == 8 || month == 10 )) {
leapdays = 31;
}
if ((leapyear) && (month == 4 || month == 6 || month == 9 || month == 11
)) {
leapdays = 30;
}
if ((leapyear) && (month == 2 )) {
leapdays = 29;
} else if ((leapyear == false) && (month == 2)) {
leapdays = 28;
}
printf ("Enter a date in the form day month year: %d %d %d \n", day,
month, year);
printf ("The date of the next day is: %d/%d/%d", ndays, month, year);
}
Consider a different flow. First find the days per month first, then test if at end of month, and end of year.
int year, month, day;
// set year, month, day in some fashion
day++; // tomorrow
int days_per_month = 31;
if (month == 4 || month == 6 || month == 9 || month == 11) {
days_per_month = 30;
} else if (month == 2) {
days_per_month = 28;
if (year % 4 == 0) {
days_per_month = 29;
if (year % 100 == 0) {
days_per_month = 28;
if (year % 400 == 0) {
days_per_month = 29;
}
}
}
}
if (day > days_per_month) {
day = 1;
month++;
if (month > 12) {
month = 1;
year++;
}
}
Other improvement would use helper functions, enumerated types and various defines. Yet this code seems to reflect OP's level.
Short and simpler. Only check for leap year when needed.
int main()
{
int Iyear, Imonth, Iday;
Iyear = 2016;
Imonth = 4;
Iday = 24;
printf ("Enter a date in the form day month year: %d %d %d \n", Iday, Imonth, Iyear);
NextDate(&Iyear, &Imonth, &Iday);
printf ("The date of the next day is: %d/%d/%d", Iday, Imonth, Iyear);
}
void NextDate(int *year, int *month, int *day)
{
int daysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
*day = *day +1;
if (( *month == 2 ) && (*day == 29))
{
// Leap year checking, if yes, Feb is 29 days.
if(*year % 400 == 0 || (*year % 100 != 0 && *year % 4 == 0))
{
daysInMonth[1] = 29;
}
}
if (*day > daysInMonth[*month -1])
{
*day = 1;
*month = *month +1;
if (*month > 12)
{
*month = 1;
*year = *year +1;
}
}
}
After you add 1 to the day, check to see if that value is greater than the number of days in that month. If so, set the day to 1 and add 1 to the month. Then check if month is greater than 12, and if so set the month to 1 and add 1 to the year.
As for determining the number of days in a month, all months except February have the same number of days whether or not the year is a leap year. Right now you're checking if the year is a leap year for those other months. You can leave that check out and just check the month number.
if (month == 12 || month == 1 || month == 3 || month == 5
|| month == 7 || month == 8 || month == 10 ) {
leapdays = 31;
}
if (month == 4 || month == 6 || month == 9 || month == 11) {
leapdays = 30;
}
Got it right :) thanks for all the help
#include <stdio.h>
int day, month, year, ndays, leapdays;
bool leapyear;
int main () {
day = 31;
month = 12;
year = 2010;
ndays = day + 1;
leapyear = false;
printf ("Enter a date in the form day month year: %d %d %d \n", day, month, year);
if (year % 4 == 0) {
leapyear = true;
}
if (year % 100 == 0) {
leapyear = false;
}
if (year % 400 == 0) {
leapyear = true;
}
if ((leapyear) && (month == 12 || month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 )) {
leapdays = 31;
}
if ((leapyear) && (month == 4 || month == 6 || month == 9 || month == 11 )) {
leapdays = 30;
}
if ((leapyear) && (month == 2 )) {
leapdays = 29;
} else if (leapyear == false) {
leapdays = 28;
}
if ((leapdays == 31) && (day == 31)) {
ndays = 1;
month = month + 1;
}else if ((leapdays == 30) && (day == 30)) {
ndays = 1;
month = month + 1;
}else if ((leapdays == 29) && (day == 29)) {
ndays = 1;
month = month + 1;
}else if ((leapdays == 28) && (day == 28)) {
ndays = 1;
month = month + 1;
}else if ((month == 12) && (day == 31)) {
ndays = 1;
month = 1;
year = year + 1;
}
printf ("The date of the next day is: %d/%d/%d", ndays, month, year);
}

How to check if date is valid using mktime in C?

I have a little problem with my code. I need to check if user's given date is valid or not. I want to check it using 2 rules:
1. Date is valid date and found in a calendar (for example 29.2.2015 is invalid because there's only 28 days in February 2015) 2. Day and month can only be max 2 numbers (for example day 10 and 02 are valid but 010 and 002 are not). All help is appreciated!
This is my code this far:
void dateValidator(const char *date1) {
struct tm date = {0};
int day1;
int month1;
int year1;
int vday;
int vmonth;
int vyear;
sscanf(date1, "%3d.%3d.%d",&day1,&month1,&year1);
/**How do I check that if sscanf reads more than 2 characters on month and day, date is
invalid?**/
date.tm_year = year1 - 1900;
date.tm_mon = month1 - 1;
date.tm_mday = day1;
date.tm_isdst = -1;
vday = date.tm_mday;
vmonth = date.tm_mon;
vyear = date.tm_year;
mktime(&pvm);
if ((vday != date.tm_mday) || (vmonth != date.tm_mon) || (vyear != date.tm_year)) {
/**This doesnt seem to work**/
printf("Invalid date");
}
}
I have deleted my previous answer and posting another one. Actually your code is fine, You just have to send the correct parameter to mktime and to check its return value. I.e. change:
mktime(&pvm);
if ((vday != date.tm_mday) || (vmonth != date.tm_mon) || (vyear != date.tm_year)) {
/**This doesnt seem to work**/
printf("Invalid date");
}
to something like:
r = mktime(&date);
if (r == -1 || (vday != date.tm_mday) || (vmonth != date.tm_mon) || (vyear != date.tm_year)) {
printf("Invalid date");
}
and declare the variable r as time_t r;.
int IsValidDate(int year, int month, int day)
{
unsigned int leap;
unsigned char mon_day[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
/* check the rang of the year */
if ((year < 1) || (year >= 3200))
{
return 0;
}
if ((month < 1) || (month > 12))
{
return 0;
}
/* if it's leep year */
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
{
leap = 1;
}
else
{
leap = 0;
}
mon_day[1] += leap;
if ((day > mon_day[month - 1]) || (day < 1))
{
return 0;
}
return 1;
}
For ruler 1, I have wrote this code for check the date before.
And I hope this code can help you.
The ruler 2 can easily checked, you can check it by format the valid date and compare it with the input string.
How do I check that if sscanf reads more than 2 characters on month and day, date is invalid?
Day and month can only be max 2 numbers digits (for example day 10 and 02 are valid but 010 and 002 are not).
An easy check: Record the offset at the end of the scan and tests 1) that it is not the original 0 (scanning got that far) and 2) this is the end of the string so as to detect extra junk in data1[]. Change 3d to 2d.
int n = 0;
sscanf(date1, "%2d.%2d.%d %n",&day1,&month1,&year1, &n);
if (n > 0 && data1[n] == '\0') Success();
else Fail();
Pedantic code would check with "%[]" to disallow leading spaces and '+'.
int n = 0;
sscanf(date1, "%*2[0-9].%*2[0-9].%*4[0-9] %n", &n);
if (n == 0 || data1[n]) Fail();
else {
sscanf(date1, "%2d.%2d.%d",&day1,&month1,&year1);
...
How to check if date is valid using mktime?
#Marian posted a good solution which adds a test of the return value of mktime().
if (-1 == mktime(&data)) Fail();
if (vday != date.tm_mday) || (vmonth != date.tm_mon) || (vyear != date.tm_year) Fail();

Attempting to create multiple if-else conditions

I am writing a program that involves checking the validity of a date. I have set the conditions for the months which have 31 days in them, and I have set the conditions for the months which have 30 days in them. I left out February because there are issues with leap year.
When I run the program, it is able to print "SUCCESS" on the screen for all valid dates. However, when I try to enter an invalid date, the program fails to write "FAILURE" on the screen, as I have it set to do [at least I believe].
What is wrong with the code here?
#include <stdio.h>
int main ()
{
int month = 1;
int day = 32;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
|| month == 10 || month == 12)
{
if (0 < day && day < 32)
{
return printf("%s","SUCCESS");
}
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
if (0 < day && day < 31 )
{
return printf("%s","SUCCESS");
}
}
else
{
return printf("%s","FAILURE");
}
return 1;
}
Your code have a wrong flow and #JoshStir is right about it.
I may know when you enter in a conditional statement, the other conditionals in the same level are ignored.
You test guide your code to enter in your first condicional.
int month = 1;
int day = 32;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
|| month == 10 || month == 12)
{
//--> Flow pass through here ( cause month == 1 )
if (0 < day && day < 32)
{
//Flow DON'T pass through here ( cause day == 32)
return printf("%s","SUCCESS");
}
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
//--> Flow DON'T pass through here ( cause flow ENTERER FIRST IF STATEMENT BEFORE)
if (0 < day && day < 31 )
{
return printf("%s","SUCCESS");
}
}
else
{
//--> Flow DON'T pass through here ( cause flow ENTERED FIRST IF STATEMENT BEFORE)
return printf("%s","FAILURE");
}
I advice you to review your code.
Try rethink and considerate what did #JoshStir advice you too.
Maybe using months and days in same comparation operation a good idea.
We were all new once, so I'll try to answer your question as clearly as I can. Given your test data:
int month = 1;
int day = 32;
your code will enter the first if statement (as month == 1)
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
|| month == 10 || month == 12)
It will then check against the inner if statement (is day > 0 and < 32?)
if (0 < day && day < 32)
day == 32, so it will not enter the if and print "SUCCESS", it will exit the if and then exit the application. If you want the data above to enter the failure condition, you would want something somewhat like the following:
if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8|| month == 10 || month == 12) && (day > 0 && day < 32))

Resources