dear all,
i have used the trial and error method to solve the above problem. the code below works just as i expected.however i did am not writing to any file in this program. maybe i will try that later.
for now it works when the program is running.
I DON'T UNDERSTAND HOW THE ABOVE DID NOT WORK. i have put the SCANF(); before the fgets(); and it magically worked. can anyone please tell me why the above did now work but the following did. THANKS A LOT.
please just run this code.
Code:
#include<stdio.h>
#include<math.h>
#include<iostream.h>
#include<string.h>
struct appointments
{
char desc[100];
int start_h;
int dur;
};
void show(struct appointments appo[24]);
int add(struct appointments appo[24],char desc[100],int start_h,int dur);
void del(struct appointments appo[24],int start_h,int dur);
int main()
{
char desc[100];
int start_h,dur,clash,op;
struct appointments appo[24] = {{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0}};
//memset(&appo[0],0,24*sizeof(struct appointments));
printf("\n\t\t\tSCHEDULER program.\n\n");
printf("THIS is a SIMPLE digital scheduler. The three parameters that can be input are\n");
printf("a ONE Line description, WHOLE NUMBER start time(in railway hours ONLY) and\n");
printf("Duration(in 1 <= hours <= 23 ONLY).\n\n\n");
while(1)
{
printf("\nSHOW - 1,ADD - 2,DEL - 3 appointments, QUIT - 4\n");
scanf("%d",&op);
if(op == 1)
{
printf("\n\n\t\tIf appointments NOT shown then it is FREE to use.\n\n\n");
show(appo);
}
if(op == 2)
{
printf("\nENTER start time, duration and descrption.\n");
scanf("%d",&start_h);
scanf("%d",&dur);
if(dur == 0)
{
printf("\n\n\t\tWARNING : UNABLE to add, Duration must be > 0\n\n");
continue;
}
fflush(stdin);
fgets(desc,100,stdin);
clash = add(appo,desc,start_h,dur);
if(clash == 1)
{
printf("\n\n\t\tWARNING : UNABLE to add due to CLASHES\n\n");
}
else
{
printf("\n\n\t\tSUCCESSFULLY added to schedule\n\n");
}
}
if(op == 3)
{
printf("\n\tWARNING : Enter the Start time and Duration to DELETE appointments\n");
scanf("%d%d",&start_h,&dur);
del(appo,start_h,dur);
}
if(op == 4)
{
return 0;
}
}
system("pause > null");
return 0;
}
void show(struct appointments appo[24])
{
int i=0;
while(i<24)
{
if(strcmp((appo[i].desc),"FREE"))
{
printf("\t\tDescription : %s\n",(appo[i].desc));
printf("\t\tStart Time : %d\n",(appo[i].start_h));
printf("\t\tDuration : %d\n\n",(appo[i].dur));
}
i++;
}
return;
}
int add(struct appointments appo[24],char desc[100],int start_h,int dur)
{
int var = start_h + 1;
int d = dur - 1;
if(!strcmp((appo[start_h].desc),"FREE"))
{
strcpy((appo[start_h].desc),desc);
(appo[start_h].start_h) = start_h;
(appo[start_h].dur) = dur;
while(var < (start_h + dur))
{
strcpy((appo[var].desc),desc);
(appo[var].start_h) = var;
(appo[var].dur) = d;
var++;
d--;
}
return 0;
}
else
return 1;
}
void del(struct appointments appo[24],int start_h,int dur)
{
int var = start_h;
while(var < (start_h + dur))
{
if(strcmp((appo[var].desc),"FREE"))
{
strcpy((appo[var].desc),"FREE");
(appo[var].start_h) = 0;
(appo[var].dur) = 0;
}
var++;
}
printf("\n\n\t\tSuccessfully DELETED, now Free to use.\n\n");
return;
}