GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Jul-2004, 16:30
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough

Need a help with C code-Segmentation Fault


Hi guys,

Just started learning C a few days back.Working on following code with structures.But when i execute i get segmentation fault.Could some one pls help with this ? Right now only choice "1"(Add Record) is being coded.
CPP / C++ / C Code:
#include <stdio.h>
typedef struct emprec
	{
		unsigned short int empno;
		char *name;
		char *dob;
		char *doj;
		char *location;
		float salary;
	}emp;
emp *records;
void add_record(emp *records);
/*void view_record(int empno,emp record);
void modify_record(int empno,emp record);
void delete_record(int empno,emp record);
void report(emp records[]);*/
#define MAX_RECORDS 10
int main()
{	
	int choice,loop;
	printf("\t\t          *Employee records systems*              \n");
	printf("1. Add a record\n");
	printf("2. View a record\n");
	printf("3. Modify a record\n");
	printf("4. Delete a record\n");
	printf("5. Report\n");
	printf("6. Exit\n");
	
	do
	{
	printf("Enter selecttion: ");
	scanf("%d",&choice);
	switch (choice)
	{
	case 1: 	add_record(records);
				loop=1;
				break;
	case 2:		view_record();
				loop=1;
				break;
	case 3:		modify_record();
				loop=1;
				break;
	case 4:		delete_record();
				loop=1;
				break;
	case 5:		report();
				loop=1;
				break;
	case 6:		printf("Thanks you.Bye\n");
				loop=0;
				break;
	default:	printf("Wrong Choice again\n");
				loop=1;
	}
	}
	while (loop);
	return 0;
}
void add_record(emp *records)
{	static unsigned short int i=0;
	char *name;
	char *dob;
	char *doj;
	char *location;
	float salary;
	int add=1;
	i++;
	if (i == MAX_RECORDS)
	{
		printf("Last Empleyee\n");
	} else if (i>MAX_RECORDS)
	{
		printf("Sorry Max number of records reached\n");
		add=0;

	}
	if (add)
	{
		printf("Add record for Employee no: %d\n",i);
		printf("\nEmployee Name: ");
		scanf("%100s",name);
		printf("Date Of Birth(mm-dd-yy): ");
		scanf("%20s",dob);
		printf("Date of Joining(mm/dd/yy): ");
		scanf("%20s",doj);
		printf("Employee Location: ");
		scanf("%20s",location);
		printf("Salary: ");
		scanf("%f",&salary);
		records[i].empno=i;
		printf(".");
		records[i].name=name;
		printf(".");
		records[i].dob=dob;
		printf(".");
		records[i].doj=doj;
		printf(".");
		records[i].location=location;
		printf(".");
		records[i].salary=salary;
		printf(".");
		printf("Record added to Database.");
	}
}
void delete_record()
{
	printf("Hi\n");
}
void modify_record()
{
	printf("Hi\n");
}
void view_record()
{
	printf("Hi\n");
}
void report()
{
	printf("Hi\n");
}
Last edited by dsmith : 16-Jul-2004 at 16:33. Reason: Please use [c] & [/c] for syntax highlighting
  #2  
Old 16-Jul-2004, 16:42
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi nkhambal. Welcome to GIDForums.

If you are new to C, I would try to avoid using pointers until you are a bit more comfortable with C. For example, instead of:


CPP / C++ / C Code:
emp *records;

Make this an array of structures like:

CPP / C++ / C Code:
emp records[MAX_RECORDS]

The reason that you are getting a segfault is that you are trying to use unallocated memory. When you use, "emp* records", you are simply defining a memory address that will eventually point to a type of structure emp*, but you are not allocating any memory for this. So unless you want to use malloc & a list, I would recommend using an array. (Especially for something this small.)

In addition, when you define your structure, use arrays for now to get used to C and then go back and use pointers:

CPP / C++ / C Code:
typedef struct emprec
  {
    unsigned short int empno;
    char name[30];
    char dob[20];
    char doj[20];
    char location[50];
    float salary;
  }emp;

Now to add data, you can use something more like:
CPP / C++ / C Code:
printf("Add record for Employee no: %d\n",i);
printf("\nEmployee Name: ");
scanf("%s",records[i].name);
And so on...

Also, you may want to use fgets instead of scanf. It allows you to limit your input, like so:
CPP / C++ / C Code:
fgets(stdin,30,records[i].name);

Good luck!
  #3  
Old 16-Jul-2004, 17:11
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Thanks dsmith,
That was quick.

I made the changes in code.It works now.One small problem in fuction add_record below.
CPP / C++ / C Code:
void add_record(emp records[])
{	static unsigned short int i=0;
	int add=1;
	float sal;
	i++;
	if (i == MAX_RECORDS)
	{
		printf("Last Empleyee\n");
	} else if (i>MAX_RECORDS)
	{
		printf("Sorry Max number of records reached\n");
		add=0;

	}
	if (add)
	{
		printf("Add record for Employee no: %d\n",i);
		printf("\nEmployee Name: ");
		scanf("%s",records[i].name);
		printf("Date Of Birth(mm/dd/yy): ");
		scanf("%s",records[i].dob);
		printf("Date of Joining(mm/dd/yy): ");
		scanf("%s",records[i].doj);
		printf("Employee Location: ");
		scanf("%s",records[i].location);
		printf("Salary: ");
		scanf("%f",&sal);
		records[i].empno=i;
		printf("\n....");
		records[i].salary=sal;
		printf("....\n");
		printf("Record added to Database\n");
	}
}

when i try to run the program and try adding record program does not wait on Date of birth printf it goes to next printf for date of joining.Here is my output.

*Employee records systems*
1. Add a record
2. View a record
3. Modify a record
4. Delete a record
5. Report
6. Exit
Enter selecttion: 1
Add record for Employee no: 3

Employee Name: Johny English
Date Of Birth(mm/dd/yy): Date of Joining(mm/dd/yy):


Any suggetions ?

Thanks,
  #4  
Old 16-Jul-2004, 17:55
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi nkhambal. I am glad that things are working.

I don't use scanf alot, especially in the situations of accepting user input as in the program you have. Scanf is better suited for structured input and reading files IMO.

Instead, I would recommend looking at fgets. I gave you the wrong format last time. It is like this:

CPP / C++ / C Code:
char *fgets(char *s, int size, FILE *stream);

I would even get your floating point using a temporary string and then using atof to convert it to a floating point, ala:

CPP / C++ / C Code:
char temp[30];
printf("Enter Salary: ");
fgets(temp,29,stdin);
records[i].salary=(float) atof(temp);

See if fgets solves the problem. It is much more suitable for accepting user input.
  #5  
Old 16-Jul-2004, 18:13
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Hi dsmith,

Tried with fgets but still program does not hault at Printf plus i get complier error saying "incompatible types in assignment".Following are some changes I did,
CPP / C++ / C Code:
char name[100];
printf("\nEmployee Name: ");
fgets(name,50,stdin);
records[i].name=name;

It gives complier error for assignment statement.Where am i going wrong?

Thanks,
  #6  
Old 16-Jul-2004, 19:41
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by nkhambal
Hi dsmith,

Tried with fgets but still program does not hault at Printf plus i get complier error saying "incompatible types in assignment".Following are some changes I did,
CPP / C++ / C Code:
char name[100];
printf("\nEmployee Name: ");
fgets(name,50,stdin);
records[i].name=name;

It gives complier error for assignment statement.Where am i going wrong?

Thanks,

You can't assign c string (char arrays or char pointers) with the assignment operator. You need to use the strcpy command to copy the string from one memory location to another.
CPP / C++ / C Code:
strcpy(records[i].name,name);

Also, why are you using the seperate name variable? Is there a reason that you can't just use:

CPP / C++ / C Code:
fgets(record[i].name,50,stdin);

Is that giving you an error?

Also, can you post all of your data entry routine again? I am not sure why it is not stopping to allow for input if you have removed all of the scanfs...
  #7  
Old 16-Jul-2004, 19:52
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Hi dsmith,

Now the compilation error is gone.I am using records[i].variable in fgets.However the problem with printf is still there.Programs does not wait a printf for adding name.Following is my add record function

CPP / C++ / C Code:
void add_record(emp records[])
{	static unsigned short int i=0;
	int add=1;
	float sal;
	i++;
	if (i == MAX_RECORDS)
	{
		printf("Last Empleyee\n");
	} else if (i>MAX_RECORDS)
	{
		printf("Sorry Max number of records reached\n");
		add=0;

	}
	if (add)
	{
		printf("Add record for Employee no: %d\n",i);
		printf("\nEmployee Name:");
		fgets(records[i].name,50,stdin);
		printf("Date Of Birth(mm/dd/yy):");
		fgets(records[i].dob,50,stdin);
		printf("Date of Joining(mm/dd/yy):");
		fgets(records[i].doj,50,stdin);
		printf("Employee Location:");
		fgets(records[i].location,50,stdin);
		printf("Salary:");
		scanf("%f",&sal);
		records[i].empno=i;
		printf("....");
		records[i].salary=sal;
		printf("....\n");
		printf("Record added to Database\n");
	}
}

Thanks,
  #8  
Old 16-Jul-2004, 20:15
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by nkhambal
Hi dsmith,

Now the compilation error is gone.I am using records[i].variable in fgets.However the problem with printf is still there.Programs does not wait a printf for adding name.Following is my add record function

CPP / C++ / C Code:
void add_record(emp records[])
{	static unsigned short int i=0;
	int add=1;
	float sal;
	i++;
	if (i == MAX_RECORDS)
	{
		printf("Last Empleyee\n");
	} else if (i>MAX_RECORDS)
	{
		printf("Sorry Max number of records reached\n");
		add=0;

	}
	if (add)
	{
		printf("Add record for Employee no: %d\n",i);
		printf("\nEmployee Name:");
		fgets(records[i].name,50,stdin);
		printf("Date Of Birth(mm/dd/yy):");
		fgets(records[i].dob,50,stdin);
		printf("Date of Joining(mm/dd/yy):");
		fgets(records[i].doj,50,stdin);
		printf("Employee Location:");
		fgets(records[i].location,50,stdin);
		printf("Salary:");
		scanf("%f",&sal);
		records[i].empno=i;
		printf("....");
		records[i].salary=sal;
		printf("....\n");
		printf("Record added to Database\n");
	}
}

Thanks,

I apologize, but I don't use scanf often because of some of its weirdness and here is another one that I just found. If I use a fgets right after a scanf, the fgets will see the enter character from the previous scanf and think that the user has pressed enter.

Work-arounds:
  1. Totally eliminate any calls to scanf
  2. Totally eliminate any calls to fgets
  3. Clear the input buffer in between calls to scanf & fgets with:
    CPP / C++ / C Code:
    fgetc(stdin);
    

Sorry, I mainly use fgets so I have never encountered this before.

Hope this does the trick.
  #9  
Old 16-Jul-2004, 20:20
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
CPP / C++ / C Code:
typedef struct emprec
  {
    unsigned short int empno;
    char name[30];
    char dob[20];
    char doj[20];
    char location[50];
    float salary;
  }emp;
Above is your structure. Your code:
CPP / C++ / C Code:
if (add)
{
	printf("Add record for Employee no: %d\n",i);
	printf("\nEmployee Name:");
	fgets(records[i].name,50,stdin);  // name is defined as 30 -- potential memory error here.
	printf("Date Of Birth(mm/dd/yy):");
	fgets(records[i].dob,50,stdin);  // same here.
	printf("Date of Joining(mm/dd/yy):");
	fgets(records[i].doj,50,stdin);  // ditto
	printf("Employee Location:");
	fgets(records[i].location,50,stdin);  // ditto
	printf("Salary:");
	scanf("%f",&sal);
	records[i].empno=i;
	printf("....");
	records[i].salary=sal;
	printf("....\n");
	printf("Record added to Database\n");
}
Make sure you specify the MAXIMUM number of characters to read, not just a value at random otherwise you can have major program crashes.

After you enter 1 person's data is that when you have the "printf" problem? If so, it's because you still have a scanf() in your code. That call leaves the '\n' in the input buffer. switch to:
CPP / C++ / C Code:
printf("Salary:");
fgets(buffer, 50, stdin);  // read the line (be sure to define buffer[50])
sscanf(buffer, "%f", &sal);// convert to float
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #10  
Old 16-Jul-2004, 21:10
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Hi dsmith,
Clearing buffer did the job...Problem is solved now.Heres what i did..

CPP / C++ / C Code:
if (add)
	{
		fgetc(stdin);
		printf("Add record for Employee no: %d\n",i);
		printf("\nEmployee Name:");
		fgets(records[i].name,50,stdin);


Thanks for your help..
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Formatting C / C++ code WaltP C Programming Language 1 07-Jan-2008 00:59
very difficult code - program gaurav_sting C++ Forum 1 16-Jun-2004 01:59
Explain code in MS STL's binary_search rom C++ Forum 11 07-Mar-2004 21:11
child pid xxx exit signal Segmentation fault (11) bezak Apache Web Server Forum 1 24-Nov-2003 10:18

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:03.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.