954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help printing a struct in c

hello everyone umm I'm having trouble in printing a structure in c... is a program to calculate benefits of employees in a company when they are fired.. getting trouble when printing the hired date of an employee here its the code

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

void menu();

struct employee{
	char company[70];
	char name[50];
   char hired_date[10];
   char fired_date[10];
   double salary[5];
}empl;

int main()
{
	menu();
   gotoxy(31,4); gets(empl.company);              
   gotoxy(31,5); gets(empl.name);                 
   gotoxy(31,6); scanf("%s", empl.hired_date);
   gotoxy(66,6); scanf("%s", empl.fired_date);


   gotoxy(20,19);printf("%s", empl.company);
   gotoxy(20,20);printf("%s", empl.name);
   gotoxy(20,21);printf("%s", empl.hired_date);  //PROBLEM HERE!!
   gotoxy(20,22);printf("%s", empl.fired_date);



	getch();
   gotoxy(45,24);system("pause");
   return 0;

void menu()
{

	cuadrado(75,6,2,2);
	gotoxy(5,4); printf("Company Name.......:");
   gotoxy(5,5); printf("Employee Name...........:");
   gotoxy(5,6); printf("Hired Date..............:");   gotoxy(42,6); printf("Fired Date..........:");

}


.. HELP!! why when I printf the hired date it print both? hired and fired date?? like this

01/01/2011/01/01/2012

DNHK
Newbie Poster
2 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

Because the dates are stored as character arrays, not strings. A string ends in with '\0'. You have space for the date only: 01/01/2012 = 10 characters.

Define your dates as 11 or more characters make sure you load a '\0' in the 11th position.

Also, see this about using gets() and scanf()

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

I would also use printf format string properly (for example "%30s") instead of gotoxy.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: