Hello,
I am writing an application which i need to use the date function and i dont know how to implement it. I wanted the date to be ask from user and be stored in a text file. anyone with hints i can really use some help.

Thanks.

ok why am asking is because i try to use an array of int for the day month and year but each time i try to run it it just terminate. something like this:

printf("Time Rented:");
	fflush(stdin);
	//getdate(d);
	newrent->TRented[0]=d.tm_mday;
	newrent->TRented[1]=d.tm_mon;
	newrent->TRented[2]=d.tm_year;
	the=mktime(&d);

so I dont know why this is not running.

Well, for one fflush(stdin); is wrong. See this.

Where is d defined and loaded?

With this little bit of code, we can tell nothing. And your description tells us nothing about your error. So, there's nothing to really say without details that explain better what the problem is.

d is a global variable and am writing an application that manage a video store. so i needed to ask the user time rented and the due date and all this should be store in a text file. here is the full code:

void AddRent(){
	struct RentData *n;
	//RentData *newrent;
	
	int str2[3];
	int str3[3];
	int init;
	
	char another='y',rec;
	n=(struct RentData *)malloc(sizeof(struct RentData));
	do
	{
	newrent= new RentData;

	printf("Video Name:");
	fflush(stdin);
	gets_s(newrent->VName);
	
	

	printf("Time Rented:");
	fflush(stdin);
	//getdate(d);
	newrent->TRented[0]=d.tm_mday;
	
	newrent->TRented[1]=d.tm_mon;
	
	newrent->TRented[2]=d.tm_year;
	
	
	
	
	//gets_s(newrent->);
	//strcpy_s(newrent->TRented,str2);

	printf("Costumer Name:");
	fflush(stdin);
	gets_s(newrent->CName);
	//strcpy_s(newrent->CName,str3);
	

	printf("Price:");
	fflush(stdin);
	scanf("%d",&init);
	newrent->Price=init;
	

	printf("Due date:");
	fflush(stdin);
	newrent->Due_Date[0]=d.tm_mday;
	newrent->Due_Date[1]=d.tm_mon;
	newrent->Due_Date[2]=d.tm_year;
	the=mktime(&d);
	printf("%d/%d/%d",d.tm_mday,d.tm_mon,d.tm_year);
	
	if ((fr = fopen("c:\\users\\Emmas4impact\\Documents\\tran.bin", "a+b"))==NULL)
   {
      printf("Cannot open file for writing \n");
      exit(1);
   }  
   fwrite(newrent, sizeof(struct RentData),2, fr);
   fclose(fr); 
   //Binary file reading
   if ((fr = fopen("c:\\users\\Emmas4impact\\Documents\\tran.bin", "a+b"))==NULL)
   {
      printf("Cannot open file for reading \n");
      exit(1);
   }  
   fread(newrent, sizeof(struct RentData),2, fr);
   fclose(fr);                                        


	
	 printf("DO YOU WANT TO ADD ANOTHER TRANSACTION(Y/N)");
	 fflush(stdin);
	 another=getchar();
	 }while(another=='Y');
	newrent->Rent=new RentData;
	//fclose(fr);
    printf("PRESS ANY KEY TO BACK TO TRANSACTION MENU");
	//exit(1);
    rent();

	
		
}

d is define as a global variable and i was actually trying to define it in the addRent() function but only i dont know how to define it and go about it.

So what you're saying is you defined d as a global variable of some indeterminate type, probably never loaded it with data, and expect proper values to be set into newrent fields using

newrent->Due_Date[0]=d.tm_mday;
newrent->Due_Date[1]=d.tm_mon;
newrent->Due_Date[2]=d.tm_year;
the=mktime(&d);

It that correct?

I say 'indeterminate' and 'probably' because you still leave out important details and won't completely answer questions. I dislike pulling information out of people like a dentist.

Plus you still use fflush(stdin) and changing gets() to gets_s() is a bandaid rather than a solution.

I think I'll let others help.

Edited 5 Years Ago by WaltP: n/a

So what you're saying is you defined d as a global variable of some indeterminate type

Though there's a good enough chance of it being struct tm that you can safely make that assumption rather than acting like a pedantic douche. ;)

changing gets() to gets_s() is a bandaid rather than a solution

No more of a Band-Aid than changing gets() to fgets(), and very few people have a problem with fgets(). The difference, of course, is that gets_s() is not portable. So rather than calling gets_s() a non-solution, I'd simply warn about it's portability and call it a day. At least the OP knows gets() is an issue and used something better.

This article has been dead for over six months. Start a new discussion instead.