3 Replies - 275 Views - Last Post: 16 January 2012 - 02:13 PM Rate Topic: -----

#1 FarshadFallah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-March 11

C Program Crashed

Posted 16 January 2012 - 01:22 PM

Hi All
I wrote my Program but i have a problem everytime i run it crash need
#include <stdio.h>


typedef struct Student *P_Student;
typedef struct Student
{
  char *name;
  char *lname;
  char *fname;
  char *care;
  int sid;
  int avg;
  int term;
  int vahed;
  int vor;
  char *mashr;
  P_Student link;
};
void Add(P_Student p)
{
  P_Student ne;
  printf("Please Enter a Name :\n");
  scanf("%s",ne->name);
   printf("Please Enter a LastName :\n");
  scanf("%s",ne->lname);
    printf("Please Enter a Father Name :\n");
  scanf("%s",ne->fname);
 printf("Please Enter a Student ID :\n");
  scanf("%d",ne->sid);
 printf("Please Enter a Career :\n");
  scanf("%s",ne->care);
 printf("Please Enter a Average :\n");
  scanf("%d",ne->avg);
  printf("Please Enter a Term Number :\n");
  scanf("%d",ne->term);
  printf("Please Enter Points Still  :\n");
  scanf("%d",ne->vahed);
  ne->link=NULL;
  if(ne->avg>12)
  {
   ne->mashr="Mashroot";
  }
  else
  {
   ne->mashr="not Mashroot";
  }
  if(p==NULL)
  {
	  p=ne;
  }
  else
  {
  while(p->link!=NULL)
  {
	  p=p->link;

  }
  p->link=ne;
  }
}
void sename(P_Student p)
{
	char temname[20];
	char temlname[20];
	printf("Please Enter a Name :\n");
  scanf("%s",temname);
   printf("Please Enter a LastName :\n");
  scanf("%s",temlname);
  while(temname!=p->name && temlname!=p->lname)
  {
	  p=p->link;
  }
  if(temname==p->name && temlname==p->lname)
  {
	  printf("Student ID is: %d\n",p->sid);
	  printf("Student Father Name is: %s\n",p->fname);
	  printf("Student Career is: %s\n",p->care);
	  printf("Student Avrage is: %d\n",p->avg);
	  printf("Student Term is: %d\n",p->term);
	  printf("Student Must Pass %d Points\n",p->vahed);
	  printf("Student is %s \n",p->mashr);

  }
  else
  {
	  printf("Not Find\n");
  }


}
void seid(P_Student p)
{
	int temsid;
	printf("Please Enter a Student ID :\n");
  scanf("%s",temsid);
  
  while(temsid!=p->sid)
  {
	  p=p->link;
  }
  if(temsid!=p->sid)
  {
	  printf("Student Name is: %s\n",p->name);
	  printf("Student Last Name is: %s\n",p->lname);
	  printf("Student Father Name is: %s\n",p->fname);
	  printf("Student Career is: %s\n",p->care);
	  printf("Student Avrage is: %d\n",p->avg);
	  printf("Student Term is: %d\n",p->term);
	  printf("Student Must Pass %d Points\n",p->vahed);
	  printf("Student is %s \n",p->mashr);

  }
  else
  {
	  printf("Not Find \n");
  }
}
  void Deletename(P_Student p)
{
	P_Student q;
	char temname[20];
	char temlname[20];

	printf("Please Enter a Name : \n");
  scanf("%s",temname);
   printf("Please Enter a LastName : \n");
  scanf("%s",temlname);
  while(temname!=p->name && temlname!=p->lname)
  {
	  q=p;
	  p=p->link;

  }
  if(temname==p->name && temlname==p->lname)
  {
	  q->link=p->link;
	  delete(p);
  }
  else
  {
	  printf("Not Find \n");
  }


}
void Deleteid(P_Student p)
{
	P_Student q;
	int temsid;
	printf("Please Enter a Student ID : \n");
  scanf("%d",temsid);

  while(temsid!=p->sid)
  {
	  q=p;
	  p=p->link;
  }
  if(temsid!=p->sid)
  {
	  q->link=p->link;
	  delete(p);

  }
  else
  {
	  printf("Not Find \n");
  }

}
void changename(P_Student p)
{
	char temname[20];
	char temlname[20];
	printf("Please Enter a Name : \n");
  scanf("%s",temname);
   printf("Please Enter a LastName : \n");
  scanf("%s",temlname);
  while(temname!=p->name && temlname!=p->lname)
  {
	  p=p->link;
  }
  if(temname==p->name && temlname==p->lname)
  {
	  printf("Please Enter a New Name : \n");
  scanf("%s",p->name);
   printf("Please Enter a New LastName : \n");
  scanf("%s",p->lname);
    printf("Please Enter a New Father Name : \n");
  scanf("%s",p->fname);
 printf("Please Enter a New Student ID : \n");
  scanf("%d",p->sid);
 printf("Please Enter a New Career : \n");
  scanf("%s",p->care);
 printf("Please Enter a New Average : \n");
  scanf("%d",p->avg);
  printf("Please Enter a New Term Number : \n");
  scanf("%d",p->term);
  printf("Please Enter Points Still  : \n");
  scanf("%d",p->vahed);
  if(p->avg>12)
  {
   p->mashr="Mashroot";
  }
  else
  {
	 p->mashr="not Mashroot";
  }

  }
  else
  {
	  printf("Not Find \n");
  }


}
void changeid(P_Student p)
{
	int temsid;
	printf("Please Enter a Student ID : \n");
  scanf("%d",temsid);

  while(temsid!=p->sid)
  {
	  p=p->link;
  }
  if(temsid!=p->sid)
  {
	  printf("Please Enter a New Name : \n");
  scanf("%s",p->name);
   printf("Please Enter a New LastName : \n");
  scanf("%s",p->lname);
    printf("Please Enter a New Father Name : \n");
  scanf("%s",p->fname);
 printf("Please Enter a New Student ID : \n");
  scanf("%d",p->sid);
 printf("Please Enter a New Career : \n");
  scanf("%s",p->care);
 printf("Please Enter a New Average : \n");
  scanf("%d",p->avg);
  printf("Please Enter a New Term Number : \n");
  scanf("%d",p->term);
  printf("Please Enter Points Still  : \n");
  scanf("%d",p->vahed);
  if(p->avg>12)
  {
   p->mashr="Mashroot \n";
  }
  else
  {
	 p->mashr="not Mashroot \n";
  }

  }
  else
  {
	  printf("Not Find \n");
  }
}
void searchbest(P_Student p)
{
	int tem=0;
	P_Student q;
	if(p==NULL)
	{
		printf("DataBase Is Empty \n");
		return ;
	}
	while(p!=NULL)
	{
		if(tem>p->avg)
		{
			tem=p->avg;
			q=p;
		}
		p=p->link;
	}
	printf("Best Student Name is: %s\n",p->name);
	printf("Best Student Last Name is: %s\n",p->lname);
	printf("Best Student ID is: %d \n",q->sid);
	printf("Best Student Father Name is: %s \n",q->fname);
	printf("Best Student Career is: %s \n",q->care);
	printf("Best Student Avrage is: %d \n",q->avg);
	printf("Best Student Term is: %d \n",q->term);
	printf("Best Student Must Pass %d Points \n",q->vahed);
}

void mashroot(P_Student p)
{
	while(p!=NULL)
	{
		if(p->mashr=="Mashroot")
		{
	       printf("Student ID is: %s",p->sid);
	       printf("Student Father Name is: %s \n",p->fname);
           printf("Student Career is: %s \n",p->care);
	       printf("Student Avrage is: %d \n",p->avg);
	       printf("Student Term is: %d \n",p->term);
           printf("Student Must Pass %d Points \n",p->vahed);
		}
	}
}
void Search(P_Student p);
void Delete(P_Student p);
void Change(P_Student p);
void main()
{
	P_Student F=NULL;
	int Chance;
	int f=1;
	printf("Please Choose one of Below : \n");
	printf("1-Add New Students \n");
	printf("2-Find \n");
	printf("3-Delete \n");
	printf("4-Exit \n");
	scanf("%d",Chance);
	

	while(f)
	{
		switch(Chance)
		{
		case(1):
		{
			Add(F);
			break;
		}
		case(2):
		{
			Search(F);
			break;

		}
		case(3):
		{
			Delete(F);
			break;
		}
		case(4):
		{
			Change(F);
		}
		case(5):
		{
			f=0;
			break;
		}
		}



	}
}
void Search(P_Student p)
{
	int Chance;
	printf("\nPlease Choose one of Below : \n");
	printf("1-Find By Name And Lastname \n");
	printf("2-Find By ID \n");
	printf("3-Back \n");
	scanf("\n%d",Chance);
	int f=0;
	while(f)
	{
		switch(Chance)
		{
		case(1):
		{
			sename(p);
			break;
		}
		case(2):
		{
			seid(p);
			break;
		}
		case(3):
		{
			f=0;
			break;
		}
		}
    }
}
void Delete(P_Student p)
{
	int Chance;
	printf("Please Choose one of Below : \n");
	printf("1-Delete By Name And Lastname \n");
	printf("2-Delete By ID\n");
	printf("3-Back\n");
	scanf("%d",Chance);
	int f;
	while(f)
	{
		switch(Chance)
		{
		case(1):
		{
			sename(p);
			break;
		}
		case(2):
		{
			seid(p);
			break;
		}
		case(3):
		{
			f=0;
			break;
		}
		}
    }
}
void Change(P_Student p)
{
	int Chance;
	printf("Please Choose one of Below : \n");
	printf("1-Change By Name And Lastname \n");
	printf("2-Change By ID \n");
	printf("3-Back \n");
	scanf("%d",Chance);
	int f;
	while(f)
	{
		switch(Chance)
		{
		case(1):
		{
			changename(p);
			break;
		}
		case(2):
		{
			changeid(p);
			break;
		}
		case(3):
		{
			f=0;
			break;
		}
		}
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: C Program Crashed

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4220
  • View blog
  • Posts: 13,231
  • Joined: 25-December 09

Re: C Program Crashed

Posted 16 January 2012 - 01:36 PM

Where do you ever allocate memory for your structure member variables? You have quite a few members that are character pointers, these variables will need to have memory allocated before you can use these variables.


Jim

This post has been edited by jimblumberg: 16 January 2012 - 01:38 PM

Was This Post Helpful? 1
  • +
  • -

#3 FarshadFallah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-March 11

Re: C Program Crashed

Posted 16 January 2012 - 01:54 PM

No its not my problem i found and fix it but i now have another problem my my switches repeat them self my new code
#include <stdio.h>


typedef struct Student *P_Student;
typedef struct Student
{
  char *name;
  char *lname;
  char *fname;
  char *care;
  int sid;
  int avg;
  int term;
  int vahed;
  int vor;
  char *mashr;
  P_Student link;
};
void Add(P_Student p)
{
  P_Student ne;
  printf("Please Enter a Name :\n");
  scanf("%s",ne->name);
   printf("Please Enter a LastName :\n");
  scanf("%s",ne->lname);
    printf("Please Enter a Father Name :\n");
  scanf("%s",ne->fname);
 printf("Please Enter a Student ID :\n");
  scanf("%d",ne->sid);
 printf("Please Enter a Career :\n");
  scanf("%s",ne->care);
 printf("Please Enter a Average :\n");
  scanf("%d",ne->avg);
  printf("Please Enter a Term Number :\n");
  scanf("%d",ne->term);
  printf("Please Enter Points Still  :\n");
  scanf("%d",ne->vahed);
  ne->link=NULL;
  if(ne->avg>12)
  {
   ne->mashr="Mashroot";
  }
  else
  {
   ne->mashr="not Mashroot";
  }
  if(p==NULL)
  {
	  p=ne;
  }
  else
  {
  while(p->link!=NULL)
  {
	  p=p->link;

  }
  p->link=ne;
  }
}
void sename(P_Student p)
{
	char temname[20];
	char temlname[20];
	printf("Please Enter a Name :\n");
  scanf("%s",temname);
   printf("Please Enter a LastName :\n");
  scanf("%s",temlname);
  while(temname!=p->name && temlname!=p->lname)
  {
	  p=p->link;
  }
  if(temname==p->name && temlname==p->lname)
  {
	  printf("Student ID is: %d\n",p->sid);
	  printf("Student Father Name is: %s\n",p->fname);
	  printf("Student Career is: %s\n",p->care);
	  printf("Student Avrage is: %d\n",p->avg);
	  printf("Student Term is: %d\n",p->term);
	  printf("Student Must Pass %d Points\n",p->vahed);
	  printf("Student is %s \n",p->mashr);

  }
  else
  {
	  printf("Not Find\n");
  }


}
void seid(P_Student p)
{
	int temsid;
	printf("Please Enter a Student ID :\n");
  scanf("%s",temsid);
  
  while(temsid!=p->sid)
  {
	  p=p->link;
  }
  if(temsid!=p->sid)
  {
	  printf("Student Name is: %s\n",p->name);
	  printf("Student Last Name is: %s\n",p->lname);
	  printf("Student Father Name is: %s\n",p->fname);
	  printf("Student Career is: %s\n",p->care);
	  printf("Student Avrage is: %d\n",p->avg);
	  printf("Student Term is: %d\n",p->term);
	  printf("Student Must Pass %d Points\n",p->vahed);
	  printf("Student is %s \n",p->mashr);

  }
  else
  {
	  printf("Not Find \n");
  }
}
  void Deletename(P_Student p)
{
	P_Student q;
	char temname[20];
	char temlname[20];

	printf("Please Enter a Name : \n");
  scanf("%s",temname);
   printf("Please Enter a LastName : \n");
  scanf("%s",temlname);
  while(temname!=p->name && temlname!=p->lname)
  {
	  q=p;
	  p=p->link;

  }
  if(temname==p->name && temlname==p->lname)
  {
	  q->link=p->link;
	  delete(p);
  }
  else
  {
	  printf("Not Find \n");
  }


}
void Deleteid(P_Student p)
{
	P_Student q;
	int temsid;
	printf("Please Enter a Student ID : \n");
  scanf("%d",temsid);

  while(temsid!=p->sid)
  {
	  q=p;
	  p=p->link;
  }
  if(temsid!=p->sid)
  {
	  q->link=p->link;
	  delete(p);

  }
  else
  {
	  printf("Not Find \n");
  }

}
void changename(P_Student p)
{
	char temname[20];
	char temlname[20];
	printf("Please Enter a Name : \n");
  scanf("%s",temname);
   printf("Please Enter a LastName : \n");
  scanf("%s",temlname);
  while(temname!=p->name && temlname!=p->lname)
  {
	  p=p->link;
  }
  if(temname==p->name && temlname==p->lname)
  {
	  printf("Please Enter a New Name : \n");
  scanf("%s",p->name);
   printf("Please Enter a New LastName : \n");
  scanf("%s",p->lname);
    printf("Please Enter a New Father Name : \n");
  scanf("%s",p->fname);
 printf("Please Enter a New Student ID : \n");
  scanf("%d",p->sid);
 printf("Please Enter a New Career : \n");
  scanf("%s",p->care);
 printf("Please Enter a New Average : \n");
  scanf("%d",p->avg);
  printf("Please Enter a New Term Number : \n");
  scanf("%d",p->term);
  printf("Please Enter Points Still  : \n");
  scanf("%d",p->vahed);
  if(p->avg>12)
  {
   p->mashr="Mashroot";
  }
  else
  {
	 p->mashr="not Mashroot";
  }

  }
  else
  {
	  printf("Not Find \n");
  }


}
void changeid(P_Student p)
{
	int temsid;
	printf("Please Enter a Student ID : \n");
  scanf("%d",temsid);

  while(temsid!=p->sid)
  {
	  p=p->link;
  }
  if(temsid!=p->sid)
  {
	  printf("Please Enter a New Name : \n");
  scanf("%s",p->name);
   printf("Please Enter a New LastName : \n");
  scanf("%s",p->lname);
    printf("Please Enter a New Father Name : \n");
  scanf("%s",p->fname);
 printf("Please Enter a New Student ID : \n");
  scanf("%d",p->sid);
 printf("Please Enter a New Career : \n");
  scanf("%s",p->care);
 printf("Please Enter a New Average : \n");
  scanf("%d",p->avg);
  printf("Please Enter a New Term Number : \n");
  scanf("%d",p->term);
  printf("Please Enter Points Still  : \n");
  scanf("%d",p->vahed);
  if(p->avg>12)
  {
   p->mashr="Mashroot \n";
  }
  else
  {
	 p->mashr="not Mashroot \n";
  }

  }
  else
  {
	  printf("Not Find \n");
  }
}
void searchbest(P_Student p)
{
	int tem=0;
	P_Student q;
	if(p==NULL)
	{
		printf("DataBase Is Empty \n");
		return ;
	}
	while(p!=NULL)
	{
		if(tem>p->avg)
		{
			tem=p->avg;
			q=p;
		}
		p=p->link;
	}
	printf("Best Student Name is: %s\n",p->name);
	printf("Best Student Last Name is: %s\n",p->lname);
	printf("Best Student ID is: %d \n",q->sid);
	printf("Best Student Father Name is: %s \n",q->fname);
	printf("Best Student Career is: %s \n",q->care);
	printf("Best Student Avrage is: %d \n",q->avg);
	printf("Best Student Term is: %d \n",q->term);
	printf("Best Student Must Pass %d Points \n",q->vahed);
}

void mashroot(P_Student p)
{
	while(p!=NULL)
	{
		if(p->mashr=="Mashroot")
		{
	       printf("Student ID is: %s",p->sid);
	       printf("Student Father Name is: %s \n",p->fname);
           printf("Student Career is: %s \n",p->care);
	       printf("Student Avrage is: %d \n",p->avg);
	       printf("Student Term is: %d \n",p->term);
           printf("Student Must Pass %d Points \n",p->vahed);
		}
	}
}
void Search(P_Student p);
void Delete(P_Student p);
void Change(P_Student p);
void main()
{
	P_Student F=NULL;
	int Chance;
	int f=1;
	printf("Please Choose one of Below : \n");
	printf("1-Add New Students \n");
	printf("2-Find \n");
	printf("3-Delete \n");
	printf("4-Exit \n");
	
	

	while(f)
	{
		scanf("%d",&Chance);
		switch(Chance)
		{
		case(1):
		{
			Add(F);
			break;
		}
		case(2):
		{
			Search(F);
			break;

		}
		case(3):
		{
			Delete(F);
			break;
		}
		case(4):
		{
			Change(F);
		}
		case(5):
		{
			f=0;
			break;
		}
		}



	}
}
void Search(P_Student p)
{
	int Chance;
	printf("\nPlease Choose one of Below : \n");
	printf("1-Find By Name And Lastname \n");
	printf("2-Find By ID \n");
	printf("3-Back \n");
	
	int f=0;
	while(f)
	{
		scanf("%d",&Chance);
		switch(Chance)
		{
		case(1):
		{
			sename(p);
			break;
		}
		case(2):
		{
			seid(p);
			break;
		}
		case(3):
		{
			f=0;
			break;
		}
		}
    }
}
void Delete(P_Student p)
{
	int Chance;
	printf("Please Choose one of Below : \n");
	printf("1-Delete By Name And Lastname \n");
	printf("2-Delete By ID\n");
	printf("3-Back\n");
	int f;
	while(f)
	{
		scanf("%d",&Chance);
		switch(Chance)
		{
		case(1):
		{
			sename(p);
			break;
		}
		case(2):
		{
			seid(p);
			break;
		}
		case(3):
		{
			f=0;
			break;
		}
		}
    }
}
void Change(P_Student p)
{
	int Chance;
	printf("Please Choose one of Below : \n");
	printf("1-Change By Name And Lastname \n");
	printf("2-Change By ID \n");
	printf("3-Back \n");
	int f;
	while(f)
	{
		scanf("%d",&Chance);
		switch(Chance)
		{
		case(1):
		{
			changename(p);
			break;
		}
		case(2):
		{
			changeid(p);
			break;
		}
		case(3):
		{
			f=0;
			break;
		}
		}
    }
}



and jim this is a Data linked program doesn't need to allocate memory
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,804
  • Joined: 16-October 07

Re: C Program Crashed

Posted 16 January 2012 - 02:13 PM

Jim is correct, storage is your problem.
/* this is bad
   never hide a pointer with a typedef
*/
typedef struct Student *P_Student;

/* this is pointless
   you say typedef but never assign a name to it...
typedef struct Student
*/
struct Student {
  char *name; /* this is just a pointer, it needs storage */
/* ... 
This is correct
*/
char temname[20]; /* you allocate a space of 20 for your string */
printf("Please Enter a Name :\n");
scanf("%s",temname); /* you read into existing storage */
/* ... 
This is incorrect
*/
char *name; /* you have some pointer */
printf("Please Enter a Name :\n");
scanf("%s",name); /* you read into an undefined location */



If you're trying to do a linked list, you'll want at least one malloc; you have none. You always need to allocate memory; somewhere.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1