Related
I've been staring at the same code for hours and I just need another person to proof read it because I cant figure out the problem anymore ive probably read my 1000 line code a hundred times already. This is the chunk with the problem. I know the solution is probably an extra * or & but i just need help finding it. I would really appreciate it thanks.
My error says:
Cannot convert int** to int* for argument 1 to void dayCheck(int*, int*, float*, float*, int, int, float*, char*, char*)
void restDayCheck(float *, float *, char *, char *);
void dayCheck(int *, int *, float *, float *, int, int, float *, char *,
char *);
void timeLoop(int *, int *, int *, int *, int *, float *, int *, int *, float *,
float *);
void restDayCheck(float *totalSalary, float *hourlyRate, char *restAns,
char *restWorkAns) {
do {
printf("\n\tIs this day a rest day? [y/n] : ");
scanf(" %c", restAns);
printf("\n\tDid you work on this day? [y/n] : ");
scanf(" %c", restWorkAns);
if (*restAns != 'y' || *restAns != 'Y' || *restAns != 'n' ||
*restAns != 'N')
printf("Answer/s are invalid.");
} while (*restAns != 'y' || *restAns != 'Y' || *restAns != 'n' ||
*restAns != 'N');
}
void timeLoop(int *startMonth, int *startDate, int *endMonth, int *endDate,
int *totalDays, float *totalSalary, int *shiftHours,
int *shiftMins, float *hourlyRate, float *minRate, char *restAns,
char *restWorkAns) {
// VARIABLES
int timeIN, timeOUT;
int newDate, newMonth, i;
// int x, y; extra if i need
float restDayPay = (*hourlyRate * 8) * 0.30;
printf("\n \t \tCLOCKING IN\n");
// LOOP
for (i = 0; i >= *totalDays; i++) {
float OTrate = *hourlyRate * 0.25;
if (*startMonth == 1 || *startMonth == 3 || *startMonth == 5 ||
*startMonth == 7 || *startMonth == 8 || *startMonth == 10 ||
*startMonth == 12) {
do {
printf("\n\tDate : %d/%d", *startMonth, *startDate);
do {
printf("\n \tEnter Time-In (Military Time 0000-2359): ");
scanf("\t%d", &timeIN);
if (timeIN < 0 || timeIN > 2359)
printf("\n \t Time-In is invalid (Military time ONLY)");
} while (timeIN < 0 || timeIN > 2359);
do {
printf("\n \tEnter Time-Out (Military Time 0000-2359): ");
scanf("\t%d", &timeOUT);
if (timeOUT < 0 || timeOUT > 2359)
printf("\n \t Time-Out is invalid (Military time ONLY)");
} while (timeOUT < 0 || timeOUT > 2359);
computeTotalHours(timeIN, timeOUT, shiftHours, shiftMins);
restDayCheck(totalSalary, hourlyRate, restAns, restWorkAns);
dayCheck(&startDate, &startMonth, &hourlyRate, &minRate, timeIN,
timeOUT, &totalSalary, &shiftHours, &shiftMins, &restAns,
&restWorkAns);
*startDate = *startDate + 1;
// TEMPORARY to check if it worked
printf("\n\tTotal Hours: %d\n", *shiftHours);
printf("\n\tTotal Minutes: %d\n", *shiftMins);
} while (*startDate <
32); // less than 32 to limit date of the months to 31
}
if (*startMonth == 2) {
do {
printf("\n\tDate : %d/%d", *startMonth, *startDate);
do {
printf("\n \tEnter Time-In (Military Time 0000-2359): ");
scanf("\t%d", &timeIN);
if (timeIN < 0 || timeIN > 2359)
printf("\n \t Time-In is invalid (Military time ONLY)");
} while (timeIN < 0 || timeIN > 2359);
do {
printf("\n \tEnter Time-Out (Military Time 0000-2359): ");
scanf("\t%d", &timeOUT);
if (timeOUT < 0 || timeOUT > 2359)
printf("\n \t Time-Out is invalid (Military time ONLY)");
} while (timeOUT < 0 || timeOUT > 2359);
computeTotalHours(timeIN, timeOUT, shiftHours, shiftMins);
restDayCheck(totalSalary, hourlyRate, restAns, restWorkAns);
dayCheck(&startDate, &startMonth, &hourlyRate, &minRate, timeIN,
timeOUT, &totalSalary, &shiftHours, &shiftMins, &restAns,
&restWorkAns);
*startDate = *startDate + 1;
// TEMPORARY to check if it worked
printf("\n\tTotal Hours: %d\n", *shiftHours);
printf("\n\tTotal Minutes: %d\n", *shiftMins);
} while (*startDate < 29); // since 28 is the total days in february, less
// than 29 to limit the days
}
if (*startMonth == 4 || *startMonth == 6 || *startMonth == 9 ||
*startMonth == 11) {
do {
printf("\n\tDate : %d/%d", *startMonth, *startDate);
do {
printf("\n \tEnter Time-In (Military Time 0000-2359): ");
scanf("\t%d", &timeIN);
if (timeIN < 0 || timeIN > 2359)
printf("\n \t Time-In is invalid (Military time ONLY)");
} while (timeIN < 0 || timeIN > 2359);
do {
printf("\n \tEnter Time-Out (Military Time 0000-2359): ");
scanf("\t%d", &timeOUT);
if (timeOUT < 0 || timeOUT > 2359)
printf("\n \t Time-Out is invalid (Military time ONLY)");
} while (timeOUT < 0 || timeOUT > 2359);
computeTotalHours(timeIN, timeOUT, shiftHours, shiftMins);
restDayCheck(totalSalary, hourlyRate, restAns, restWorkAns);
dayCheck(&startDate, &startMonth, &hourlyRate, &minRate, timeIN,
timeOUT, &totalSalary, &shiftHours, &shiftMins, &restAns,
&restWorkAns);
*startDate = *startDate + 1;
// TEMPORARY to check if it worked
printf("\n\tTotal Hours: %d\n", *shiftHours);
printf("\n\tTotal Minutes: %d\n", *shiftMins);
} while (*startDate < 31); // 31 is less than 30 which is the total days
// in the months listed in the if statement
}
*startMonth = *startMonth + 1;
}
}
void dayCheck(int *startDate, int *startMonth, float *hourlyRate,
float *minRate, int timeIN, int timeOUT, float *totalSalary,
int *shiftHours, int *shiftMins, char *restAns,
char *restWorkAns) {}
I did not include the code in dayCheck anymore because my error is with calling the function. Thanks so much again.
Ive tried pretty much everything. I know my error is just something ive overlooked or skipped. I just need another set of eyes tbh.
This is my code to find the days in between days. For example 03 15 to 03 24 have 9 days between each other.
#include <stdio.h>
int main(void) {
int mm,dd,yy, mm2, dd2, yy2;
printf("Please enter in first date (MM/DD/YYYY format): ");
scanf("%d/%d/%d",&mm,&dd,&yy);
printf("Please enter in second date (MM/DD/YYYY format): ");
scanf("%d/%d/%d",&mm2,&dd2,&yy2);
if(yy>=2000 && yy<=2019) {//check year if it between 2000 to 2019
if(mm>=1 && mm<=12){ //check month
if((dd>=1 && dd<=31) && (mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12)) //check the days of these months.
printf("The first date is valid.\n");
else if((dd>=1 && dd<=30) && (mm==4 || mm==6 || mm==9 || mm==11)) //check the days of these months.
printf("The first date is valid.\n");
else if((dd>=1 && dd<=28) && (mm==2)) //check the days of these month because February only have 28 days when its not a leap year
printf("The first date is valid.\n");
else if(dd==29 && mm==2 && (yy%400==0 ||(yy%4==0 && yy%100!=0))) // a leap year check
printf("The first date is valid.\n");
else
printf("The first day is invalid.\n"); // run if the user have enter in a invalid day
} else {
printf("The first month is not valid.\n"); // run if the user have enter in a invalid month
}
} else {
printf("The first year is not valid.\n"); // run if the user have enter in a invalid year
}
if(mm < mm2) { // check to see if mm2 is greater than mm
int s = dd2-dd;
s=s+dd2;
for (mm=mm;mm<mm2+1;mm++) {
s=s+mm;
}
printf("The total number states which is strictly between given two dates is: %d\n", s);
} else if(mm == mm2) {// check to see if it is in the same month
if(dd <= dd2) { // check to see if day2 is greater to equal to day1
// print out the number that are between those days
printf("The total number states which is strictly between given two dates is: %d\n", dd2-dd);
} else {
printf("Second date precedes the first date\n"); // if the second is greater then the first date, it will print this out
}
}
return 0;
}
For some reason when I enter in 02/10/2019 and 03/07/2019. I get 4 for the days in between which is wrong but when I do 01/01/2019 and 12/03/2019, I get 81 which is correct.
Here's a simpler version
#include<stdio.h>
#include<conio.h>
void days(int,int,int,int,int,int);
int month(int,int);
int mon[12]={31,28,31,30,31,30,31,31,30,31,30,31};
main()
{
int a1,b1,c1,a2,b2,c2;
clrscr();
printf("Enter first date(dd mm yyyy) : ");
scanf("%d%d%d",&a1,&b1,&c1);
printf("\nEnter second date(dd mm yyyy) : ");
scanf("%d%d%d",&a2,&b2,&c2);
if(c2>=c1)// checking which date is lesser
days(c1,c2,b1,b2,a1,a2);
else
days(c2,c1,b2,b1,a2,a1);
getch();
}
void days(int y1,int y2,int m1,int m2,int d1,int d2)
{
int count=0,i;
for(i=y1;i<y2;i++)//just adding days
{
if(i%4==0)//leap year
count+=366;
else
count+=365;
}
count-=month(m1,y1);//sub month
count-=d1;//day
count+=month(m2,y2);//add month
count+=d2;//day
if(count<0)
count=count*-1;
printf("The no. of days b/w the 2 dates = %d days",count);
}
int month(int a,int yy)
{
int x=0,c;
for(c=0;c<a-1;c++)
{
if(c==1)// feb
{
if(yy%4==0)
x+=29;
else
x+=28;
}
else
x+=mon[c];//get no. Of days in the month
}
return(x);
}
You already got one answer, and that one looks really good. I am adding answer to give you an optional method. Ultimately, you can always convert the date strings to timestamps. After that, you can look for the time difference between the two dates by subtracting the second date's timestamp to the first date time stamp. If that yields negative, that is a precedent day. If not, that is all fine.
To be able to do that, you can use mktime() and struct tm from . When you use "time.h" you would still need to validate your dates like now. It just, it would be easier on the calculation.
For tm_year from struct tm, you would need to subtract the year value to 1900. Also, it seemed that you only wanted your program to work for dates from the year 2000 - 2019. Thus, 19 years. Thus, I think a long or int_64 should be able to hold the maximum seconds different between two dates. Also, mktime() return value are seconds.
Take notice that I did not write the code to calculate where date 2 is before date 1. Thus, if you attempt to calculate that, it might give you an error.
#include <stdio.h>
#include <time.h>
int main(void) {
int mm,dd,yy, mm2, dd2, yy2;
printf("Please enter in first date (MM/DD/YYYY format): ");
scanf("%d/%d/%d",&mm,&dd,&yy);
printf("Please enter in second date (MM/DD/YYYY format): ");
scanf("%d/%d/%d",&mm2,&dd2,&yy2);
if(yy>=2000 && yy<=2019) {//check year if it between 2000 to 2019
if(mm>=1 && mm<=12){ //check month
if((dd>=1 && dd<=31) && (mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12)) //check the days of these months.
printf("The first date is valid.\n");
else if((dd>=1 && dd<=30) && (mm==4 || mm==6 || mm==9 || mm==11)) //check the days of these months.
printf("The first date is valid.\n");
else if((dd>=1 && dd<=28) && (mm==2)) //check the days of these month because February only have 28 days when its not a leap year
printf("The first date is valid.\n");
else if(dd==29 && mm==2 && (yy%400==0 ||(yy%4==0 && yy%100!=0))) // a leap year check
printf("The first date is valid.\n");
else {
printf("The first day is invalid.\n"); // run if the user have enter in a invalid day
return 1;
}
} else {
printf("The first month is not valid.\n"); // run if the user have enter in a invalid month
return 1;
}
} else {
printf("The first year is not valid.\n"); // run if the user have enter in a invalid year
return 1;
}
struct tm firstDate;
struct tm secondDate;
firstDate.tm_sec = 0;
firstDate.tm_min = 0;
firstDate.tm_hour = 0;
firstDate.tm_mday = dd;
firstDate.tm_mon = mm;
firstDate.tm_year = yy-1900;
firstDate.tm_isdst = 0;
secondDate.tm_sec = 0;
secondDate.tm_min = 0;
secondDate.tm_hour = 0;
secondDate.tm_mday = dd2;
secondDate.tm_mon = mm2;
secondDate.tm_year = yy2-1900;
secondDate.tm_isdst = 0;
long epochFirstDate = (long) mktime(&firstDate);
long epochSecondDate = (long) mktime(&secondDate);
long dayDifferent = (epochSecondDate - epochFirstDate);
if ( dayDifferent > 0 ) dayDifferent = dayDifferent / 60 / 60 / 24;
if ( dayDifferent > -1 )
printf("The total number states which is strictly between given two dates is: %lu\n", dayDifferent);
else
printf("Second date precedes the first date\n"); // if the second is greater then the first date, it will print this out
return 0;
}
This is a menu driven program that allows a user to:
(1)view a calendar
(2)view on which day of the week a particular date falls
(3)exit the program.
But its not running please help:
#include <stdio.h>
#include<conio.h>
#include<math.h>
void load_menu();
void view_calender();
void view_date();``
int main (int argv, char** arv)
{
load_menu();
return ;
}
void load_menu()
{
int choice;
do
{
printf("Menu\n");
printff("1. View Calender\n");
printf("2. View on which day a particular dau falls\n");
printf("3. Exit Programme\n");
scanf("%d", &choice);
switch(choice)
{
case 1: view_calender();
break;
case 2: view_date();
break;
case 3: printf("Exiting Programme!\n");
exit(0);
break;
default: printf("Invalid Choice chosen\n");
break;
}
} while (choice != 3);
}
void view_calender()
{
int is_leap_year( int year ); /* True if leap year */
int leap_years( int year ); /* The number of leap year */
int today_of( int y, int m, int d); /* The number of days since the beginning of the year */
long days( int y, int m, int d); /* Total number of days */
void calendar(int y, int m); /* display calendar at m y */
int year,month;
printf("Enter the Month(mm): ");
scanf("%d", &month);
printf("Enter the Year(yyyy): ");
scanf("%d", &year);
calendar(year, month);
return 0;
int isLeapYear( int y ) /* True if leap year */
{
return(y % 400 == 0) || ((y % 4 == 0) && (y % 100 != 0));
}
int leapYears( int y ) /* The number of leap year */
{
return y/4 - y/100 + y/400;
}
int TodayOf( int y, int m, int d) /* The number of days since the beginning of the year */
{
static int DayOfMonth[] =
{ -1,0,31,59,90,120,151,181,212,243,273,304,334};
return DayOfMonth[m] + d + ((m>2 && is_leap_year(y))? 1 : 0);
}
long Days( int y, int m, int d) /* Total number of days */
{
int last_year;
last_year = y - 1;
return 365L * last_year + leapy_years(last_year) + TodayOf(y,m,d);
}
void view_calendar() /* display calendar at m y */
{
int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char* day[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday" "Friday", "Saturday"};
int d;
int moti;
int yel;
int i;
printf("Enter date in this format dd-mm-yyyy: ");
scanf("%d-%d-%d", &d, &moti, &yel);
// correction for leap year
for (i = 1900; i < yel; i++)
if (i % 4 == 0 && (i % 100 != 0 || i % 400 == 0))
d += 366;
else
d += 365;
for (i = 0; i < moti - 1; i++)
d += month[i];
printf("This is a %s.\n", day[d % 7]);
return 0;
}
}
If the user selects option (2), the program must display the day on which the user's entered date falls.
For instance, if the user wants to know on which day 24th October 2021 falls, they must be prompted to enter the year (i.e. 2021), the month (i.e. 10 for October), and the date (i.e. 24).
The output must be the day of the week e.g. "Sunday".
The program, after executing option (1) or (2) must display the menu again.
The source code you show gets a warning about an incompatible definition of exit because:
exit is a reserved identifier; it should be used only for the standard library routine named exit.
Your program does not include the <stdlib.h> that would provide a declaration of exit.
Your program uses exit without a declaration for it. When a routine is used without a declaration, your compiler supplies a default declaration.
The default declaration is for a function returning int, but the correct declaration for exit is a function returning nothing (void), so the compiler warnings you the declaration (provided by default) is incompatible with the built-in standard function.
To fix this, insert #include <stdlib.h> in your program.
When I enter the date 08/01/2015 the program prints 00/00/00 instead of 08/01/2015. What seems to be the problem?
#include <stdio.h>
int main ()
{
int month = 0;
int day = 0;
int year = 0;
printf("Enter today's date (mm/dd/yyyy): ");
scanf("%i/%i/%i", &month, &day, &year);
printf("%.2i/%.2i/%.2i", month, day, year);
return 0;
}
I amended your original program to deal with several issues. But the main one was to use the correct format specifier %d in scanf(). Using %i will interpret an input as octal if it has a leading 0 - very likely when typing a date, especially as you encourage that with the output format. Months 01 to 07 did not fail because the octal input works correctly - but 08 and 09 do not, as 8 and 9 do not exist in octal number representation.
I also checked the return value from scanf() to ensure the integer date fields were correctly entered. If you had done that, you would have seen there was something wrong there.
Other things I changed are:
Checked the date you entered to be valid to avoid the GIGO syndrome.
Avoided writing to the array of days per month as commented earlier.
Restricted the year to the Gregorian calendar, as some days were missing.
Separated form from functionality. For example, your leapYear() function checked for a leap year and also tested (inadequately) - and reported errors in the date entered.
Moved the global date variables to be local vars, which are passed as function arguments.
#include <stdio.h>
struct date {
int month;
int day;
int year;
};
int daysPerMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int leapyear(int year) {
return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
}
int validdate(struct date now) {
int daysmon;
if (now.year <= 1582) // pre-Gregorian
return 0;
if (now.month < 1 || now.month > 12)
return 0;
daysmon = daysPerMonth[now.month-1];
if (leapyear(now.year) && now.month == 2)
daysmon++;
if (now.day < 1 || now.day > daysmon)
return 0;
return 1;
}
struct date bumpdate(struct date now) {
int daysmon = daysPerMonth[now.month-1];
if (leapyear(now.year) && now.month == 2)
daysmon++;
if (++now.day > daysmon) {
now.day = 1;
if (++now.month > 12) {
now.month = 1;
now.year++;
}
}
return now;
}
int main (void) {
struct date today, tomorrow;
printf("Enter today's date (mm/dd/yyyy): ");
if (3 != scanf("%d %*[/-] %d %*[/-] %d", &today.month, &today.day, &today.year)) {
printf ("Need the proper date format\n");
return 1;
}
if (!validdate(today)) {
printf ("Invalid date\n");
return 1;
}
tomorrow = bumpdate(today);
printf("Tomorrow's date is %02d/%02d/%04d", tomorrow.month, tomorrow.day, tomorrow.year);
return 0;
}
I want to add x number of days to a date.
Can someone help me with my logic especially in adding x number of days to a specific date.
It seems to work for small number of days added but if I add a large number of days, it starts to give really funny answers.
Also no functions please, just logic.
int
main(int argc, char *argv[]) {
int dd, mm, yyyy, daysthismonth, days, option;
printf("Please enter a date in the following format: dd/mm/yyyy:\n");
if(scanf("%d/%d/%d",&dd,&mm,&yyyy)!= 3){
printf("Please enter a valid date\n");
exit(EXIT_FAILURE);
/* Calculating days in a month */
}
if((mm == 4)|| (mm == 6) || (mm == 9) || (mm == 11)){
daysthismonth = 30;
}
else if((mm == 2)){
if((yyyy%4 == 0) && ( (yyyy%100 == 0) || (yyyy%400 == 0))){
daysthismonth = 29;
}
else{
daysthismonth = 28;
}
}
else {
daysthismonth = 31;
}
/* Calculating the validity of User Input */
if( (dd<0) || (dd>daysthismonth) || (mm<0) || (mm>12) || (yyyy < 0) || (yyyy>9999) ){
printf("Plase enter a valid date between from a day from 0 AD to 9999 AD\n.");
}
printf("Todays date: %02d/%02d/%04d\n"
"How many days would you like to go in the future? \n",dd,mm,yyyy);
if(scanf("%d",&days) != 1){
printf("Please enter a valid input\n");
}
printf("The date %d days in the future is:\n",days);
while(days > (daysthismonth - dd)){
mm = mm + 1;
days = days - (daysthismonth - dd);
if( days < daysthismonth){
dd = days;
}
if( mm>12){
mm = 1;
yyyy = yyyy + 1;
}
}
dd = dd + days;
}
printf(" %02d/%02d/%04d\n",dd,mm,yyyy);
exit(EXIT_SUCCESS);
}
I guess my logic was if you added a large number of days, it would subtract the next month's days until you get to a point where the days to be added is less than the days in the month. At that point the date can just be the days you have to add.
That's why I implemented a while? loop so it could reiterate
edited: scanf() != 1
Just store your day/month/year into a struct tm, call mktime() on it to convert to epoch seconds, add days*24*60*60 to advance it, then call localtime_r() (or localtime()) on the result to get day/month/year again.
Be mindful of daylight saving time: you should probably set the hours component to noon to avoid any possibility of gaining or losing a day when DST changes.
So you want to add days on a date.
The easy logic is to convert the date to days, add the x days, then convert days to date.
The complex logic is to just make the add like in maths
yyyy/mm/dd
+ dd
-----------
result
like in the maths the numbers can only be from 0-9 here the date / month / year have to be valid based on the rest.
converting the days you want to add to date before the add and then add before making valid can help.
Example in javascript (so that you can easy run it on the browser's console):
function daysInMonth(month,year){
// Does not matter how you make this function
return new Date(year, month, 0).getDate();
}
var year = 2014;
var month = 8;
var day = 8;
var daysToAdd = 123434;
day += daysToAdd;
var maxDay = daysInMonth( month, year);
while( day>maxDay ){
day -= maxDay;
month += 1;
if(month==13){
month = 1;
year += 1;
}
maxDay = daysInMonth( month, year);
}
console.log(day+'/'+month+'/'+year);
It works like a charm :)
C code (After a request) :
#include <stdio.h>
main(){
// Init the variables we use
int year = 0;
int month = 0;
int day = 0;
int maxDay = 0;
int daysToAdd = 0;
// Ask user for a date
printf("Please enter a date in the following format: dd/mm/yyyy:\n");
// Validate data
while( scanf( "%d/%d/%d", &day, &month, &year)!= 3 || day<=0 || month<=0 || year<0 || daysInMonth(month,year)<day ){
printf("Please enter a valid date.\n");
}
// Ask user for days to add
printf("Please enter days to add: \n");
// Validate data
while( scanf( "%d", &daysToAdd)!= 1 || daysToAdd<=0 ){
printf("Please enter a positive number.\n");
}
// Calculate date
day += daysToAdd;
maxDay = daysInMonth( month, year);
while( day>maxDay ){
day -= maxDay;
month += 1;
if(month==13){
month = 1;
year += 1;
}
maxDay = daysInMonth( month, year);
}
printf("Result date : %d/%d/%d", day, month, year);
return(0);
}
// Get days in the month
int daysInMonth( int month, int year){
if( month==4 || month==6 || month==9 || month==11 ){
return 30;
}else if( month== 2 ){
if( year%4==0 && ( year%100==0 || year%400==0) )
return 29;
else
return 28;
}else{
return 31;
}
}
Online test link : http://ideone.com/CGXgtP
Happy coding.