Oghenebrume Oghenebrume - 18 days ago 6
C Question

issues with struct, why do I get this error?

I have searched for answers here I saw some few related post but I couldn't comprehend it all, am new to C and just dealing with struct. I believe the erros are as a result of how the struct are handled but am not sure.

This are the errors I get when I try compiling my code, I will add the code below it

30 38 C:\Users\PCPCPCPC\Documents\clock.c [Warning] 'struct dateAndtime'


declared inside parameter list

30 38 C:\Users\PCPCPCPC\Documents\clock.c [Warning] its scope is only this definition or declaration, which is probably not what you want

42 20 C:\Users\PCPCPCPC\Documents\clock.c [Error] type of formal parameter 1 is incomplete

48 38 C:\Users\PCPCPCPC\Documents\clock.c [Warning] 'struct dateAndtime' declared inside parameter list

48 50 C:\Users\PCPCPCPC\Documents\clock.c [Error] parameter 1 ('current') has incomplete type

The Code

#include <stdio.h>
#include <stdbool.h>

//Global struct variables
struct date
{
int month;
int day;
int year;
};

struct time
{
int seconds;
int minutes;
int hours;
};

struct dateAndTime
{
struct date sDate;
struct time sTime;
};

//prototypes
int numberOfDays (struct date d);
bool isLeapYear (struct date d);
struct date dateUpdate(struct date today);
struct time timeUpdate(struct time now);
struct dateAndTime clocKeeper(struct dateAndtime current);

int main()
{
//struct dateAndTime dt, future;
/*printf("please enter the correct date and time");
printf("mm/dd/year hr:mm:sc");
scanf("%i %i %i %i %i %i",dt.sDate.month,dt.sDate.day,dt.sDate.year,dt.sTime.hours,dt.sTime.minutes,dt.sTime.seconds);*/

struct dateAndTime dt1 = {{12, 31, 2004}, {23, 59, 59}};
struct dateAndTime dt2 = {{2, 28, 2008}, {23, 59, 58}};
//future = clocKeeper(dt1);

dt1 = clocKeeper (dt1);

printf("the updated date is %i/%i/%i ", dt1.sDate.month, dt1.sDate.day, dt1.sDate.year % 100);
printf("the updated time is %.2i:%.2i:%.2i", dt1.sTime.hours,dt1.sTime.minutes,dt1.sTime.seconds);
}

struct dateAndTime clocKeeper(struct dateAndtime current)
{
current.sTime = timeUpdate(current.sTime);

if (current.sTime.hours == 0 && current.sTime.minutes == 59 && current.sTime.seconds == 59)
current.sDate = dateUpdate(current.sDate);

return current;
}

struct time timeUpdate(struct time now)
{
++now.seconds;
if ( now.seconds == 60 ) { // next minute
now.seconds = 0;
++now.minutes;
if ( now.minutes == 60 ) { // next hour
now.minutes = 0;
++now.hours;
if ( now.hours == 24 ) // midnight
now.hours = 0;
}
}
return now;
}

// Function to find the number of days in a month
int numberOfDays (struct date d)
{
int days;
bool isLeapYear (struct date d);
const int daysPerMonth[12] ={ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if ( isLeapYear (d) == true && d.month == 2 )
days = 29;
else
days = daysPerMonth[d.month - 1];

return days;
}

// Function to determine if it's a leap year
bool isLeapYear (struct date d)
{
bool leapYearFlag;
if ( (d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0 )
leapYearFlag = true; // It's a leap year
else
leapYearFlag = false; // Not a leap year
return leapYearFlag;
}

struct date dateUpdate(struct date today)
{
struct date tomorrow;
int numberOfDays (struct date d);
if ( today.day != numberOfDays (today) ) {
tomorrow.day = today.day + 1;
tomorrow.month = today.month;
tomorrow.year = today.year;
}
else if ( today.month == 12 ) { // end of year
tomorrow.day = 1;
tomorrow.month = 1;
tomorrow.year = today.year + 1;
}
else { // end of month
tomorrow.day = 1;
tomorrow.month = today.month + 1;
tomorrow.year = today.year;
}
return tomorrow;
}

Answer

you passed wrong structure name as argument in clocKeeper() function.

 struct dateAndTime clocKeeper(struct dateAndtime current)

Right way is:-

 struct dateAndTime clocKeeper(struct dateAndTime current)