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 01-Nov-2003, 03:31
mycashmoney mycashmoney is offline
New Member
 
Join Date: Nov 2003
Posts: 1
mycashmoney is an unknown quantity at this point

Urgent ! Pls Help Me !


hi all,

I wanna your help urgently, bcs i wanna submit my project at 7/11/03 at that time. As a result, i am still unable to solve my code.

Got error ! that why ... he he .. wanna help !

My program is student database system.
Specification must included dynamic data struture and binary file.

Pls help me to check wht error for my code and if you hv time pls give me solution or source sample i can refer.

Problem Program
--------------------
1.Unable to read/write data into file.
2.Delete function will critical message when i delete data frm it.

Below is my sources code.
Compiler: VC++

Thks A LOT THKS A LOT !! HELP ME PLS !!
==============================================
CPP / C++ / C Code:
// Header Files
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include "mainMenu.h"

#define TRUE  1
#define FALSE 0
//////////////////////////////////////////////////////////////////////////////////////////////
// Data structure defintiion

struct stud_record {
	char	name[25];
	int		id;
	float		height;
	float	weight;
};

struct stud_node {
	struct stud_record data;
	struct stud_node *next;
};

////////////////////////////////////////////////////////////////////////////////////////////// 
// Function prototype declaration


int mainMenu();
int isEmpty(struct stud_node *start);
void record_list(struct stud_node *head);
void title(void);
void spc(char text[]);

struct stud_node * insert(struct stud_record temp, struct stud_node *start);
struct stud_node *insertFirst (struct stud_record *temp, struct stud_node *start);
struct stud_node *insertFront (struct stud_record *temp, struct stud_node *start);
struct stud_node *insertOthers(struct stud_record *temp, struct stud_node *start);
struct stud_node *delete(int target, struct stud_node *start);
struct stud_node *search(int target, struct stud_node *start);
struct stud_node *edit(struct stud_node *start);
struct stud_node *addData(struct stud_node *start);
struct stud_node *readFile(struct stud_node *);
struct stud_node *updateFile(struct stud_node *);


/////////////////////////////////////////////////////////////////////////////////////////////
// Main Program

main()
{
	int choice;
	int target=0;
	
	//start pointer
	struct stud_node *start;
	
	start = NULL;

	start = readFile(start);
	
	do{
		system("cls");
		choice = mainMenu();		
		
						if(choice == 1){
											system("cls");
											start = addData(start);
											printf("\n\n\n\nPress any key to continue...");
											getch();
											}	
						else if(choice == 2){
			
							system("cls");
							record_list(start);
							printf("\n\n\n\nPress any key to continue...");
							getch();
											
							}

						else if(choice == 3)
						{
						

							start=edit(start);
						}
						else if(choice ==4){

						system("cls");
						
						if(!isEmpty(start)){
				
						printf("\n\nEnter Id to delete :");
						scanf("%d", &target);
				
						start = delete(target,start);
			
						printf("\n\n\n\nPress any key to continue...");
											getch();
											}
				else {
						
					printf("\n\nRecord empty !\n\n");
				
						printf("\n\n\n\nPress any key to continue...");
											getch();
						}
						}

						else if(choice==5)
						{
							printf("Enter Id to find :");
							scanf("%d",&target);
						search(target,start);
						}
				else if(choice==6){

					updateFile(start);
			 		system("cls");
				}
				
				else if(choice==7){		
				
				system("cls");		
			    printf("System end  !! ...\n\n");
				break;
				}

				else{
					printf("Error..Invalid Choice...!\n");
				}
		}while(choice >1 || choice <8);
	
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Read data from binary file 
// 

struct stud_node *readFile(struct stud_node *start)
{

	// declaration start 
	FILE *fptr;
	struct	stud_record temp;

	int		open_file_error = FALSE;
	int		end = FALSE;
	int		found = FALSE;

	start = NULL;

	//start read file and checking 
	fptr = fopen("Student.bin","rb");
	if(fptr == NULL)
	{
		printf("Cannot open file...!\n");
		open_file_error = TRUE;
	}
		
	while(!open_file_error && !end) // i will consider file not end of file 
	{
				//record structure : size : date/read : file pointer
		if (fread(&temp,sizeof(temp),1,fptr)==0)//read file untill finished 
			end=TRUE; // after finished make condition false and break out
		else
			//otherwise space unavailable goto sort data 
			start = insert(temp,start);
	}

	
//	showAll(start); // to test successful read
	return start;
}

/////////////////////////////////////////////////////////////////////////////////////////////
// Display function
//

void record_list(struct stud_node *head)
{
	int lines=0,count=0;
	int i=0;
	title();
	for(i=0; i<80; i++)
	{
		printf("=");
	}
	printf("No:\t Student Id\t Name \t\t    \t   Height	    Weight\n");
    for(i=0; i<80; i++)
	{
		printf("=");
	} 

	//when structure pointer space unavailable 
	while(head != NULL)
	{
		if (lines < 18)
			lines++;
		else
		{
			system("cls");
			lines=0;
		}
		count++;
		printf("%-4d\t %-10d\t %-25s %-8.2f\t %10.2f\n",
				count,head->data.id,head->data.name,head->data.weight,head->data.height);
		head = head->next;		
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////
// List Title
//
void title(void)
{
	spc(" << SMK High School >> \n");
}

//////////////////////////////////////////////////////////////////////////////////////////////
// Screen space allocation
//

void spc(char text[])
{
	int space = 0 , loop;

	space = (80-(strlen(text)))/2;

	for(loop=0;loop<space;loop++)
		printf(" ");
	printf("%s\n",text);
}


/////////////////////////////////////////////////////////////////////////////////////////////
// This function was used to check struture pointer condition 
// and determine action to take

 
struct stud_node *insert(struct stud_record temp, struct stud_node *start)
{
//	printf("%d %s %d %f\n",temp.id,temp.name,temp.qty,temp.price); //to test
	// no record found, first adding start
	if (start==NULL)
		//&temp address passed into insertFirst() function 
		start = insertFirst(&temp,start);
	else
		//sort data by id
		if (temp.id < start->data.id)
			start = insertFront(&temp,start);
		else
			//record available, add other data 
			insertOthers(&temp,start);
	return start;
}
//////////////////////////////////////////////////////////////////////////////////////////////
//  Add first record
//

struct stud_node * insertFirst(struct stud_record *temp, struct stud_node *start)
{
	//allocate memory to store data into start pointer structure
	start = malloc(sizeof(temp)); // first size of memory allocated 
	//copy 
	strcpy(start->data.name,temp->name);
	start->data.id = temp->id;
	start->data.height= temp->height;
	start->data.weight = temp->weight;
	start->next = NULL;

	return start;
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Add record at front
// 
struct stud_node * insertFront(struct stud_record *temp, struct stud_node *start)
{
	struct stud_node *newData;
	newData = 0; //same as NULL

	newData = malloc(sizeof(struct stud_record));
	strcpy(newData->data.name,temp->name);
	newData->data.id = temp->id;
	newData->data.height= temp->height;
	newData->data.weight = temp->weight;
	newData->next = start;
	start = newData;

	return start;
}
/////////////////////////////////////////////////////////////////////////////////////////////
//  Add other data
//
struct stud_node * insertOthers(struct stud_record *temp, struct stud_node *start)
{
	struct stud_node *currentPtr,*previousPtr,*newData;

	currentPtr = previousPtr = newData = 0;

	currentPtr = start;
	
	//sort data at memory location untill all data in sequence
	while(currentPtr != NULL && temp->id > currentPtr->data.id)
	{
		previousPtr = currentPtr;
		currentPtr = currentPtr->next;
	}

	newData = malloc(sizeof(struct stud_record));
	strcpy(newData->data.name,temp->name);
	newData->data.id = temp->id;
	newData->data.height= temp->height;
	newData->data.weight = temp->weight;
	
	newData->next = previousPtr->next;
	previousPtr->next = newData;
	
	return start;
}

/////////////////////////////////////////////////////////////////////////////////////////////
// Add data process

struct stud_node *addData(struct stud_node *start)
{
	int i;
	int counter=0;
	//part is structure variable
	struct	stud_record parts;//create a new record structure node
	int continu_e = TRUE;
	
	printf("Enter The Following Information: \n");
	for(i=0; i<35; i++){
	printf("=");
	}
	while(continu_e)
	{
		counter++;
		parts.height=0;
		parts.weight=0;
		printf("\n\n%d.Enter student Name [Blank->return]?: ",counter);
		fflush(stdin);
		gets(parts.name);
		if(strlen(parts.name)==0)
			break;
		
		printf("\nPlease Enter Student ID : ? ");
		scanf("%d",&parts.id);
		fflush(stdin);
		
		printf("\nEnter Height : ? ");
		scanf("%f",&parts.height);
		fflush(stdin);
		
		printf("\nEnter Weight : ? ");
		scanf("%f",&parts.weight);
		fflush(stdin);
		start = insert(parts,start);
	}
	
	return start;

}
/////////////////////////////////////////////////////////////////////////////////////////////
// Delete function

struct stud_node *delete(int target, struct stud_node *start)
{

	struct stud_node *previousPtr, *currentPtr, *tempPtr;

	 //previousPtr=currentPtr =tempPtr=0;
	
	 if(target == start->data.id){
	
	tempPtr = start;
	start = start->next;

	free(tempPtr);
	
		return start;
}
else 
{
	
	previousPtr = start;
	currentPtr = start->next;
	
 
	//swap position
		while(currentPtr != NULL && currentPtr->data.id != target)
	{
		previousPtr = currentPtr;

		currentPtr = currentPtr->next;
	}
		

		if(currentPtr !=NULL && currentPtr->data.id == target){
	
		tempPtr = currentPtr;
		
		  	
		previousPtr->next = currentPtr->next;

	
			free(tempPtr);
	 printf("\n\n%d record deleted !\n\n", target);
	
		return start;
	
		}
}
			printf("No such record ! ");



return start;
}

/////////////////////////////////////////////////////////////////////////////////////////////
// Update function

struct stud_node *updateFile(struct stud_node *start)
{

	FILE *fptr;

	int end = FALSE;
	struct stud_record data;

	//open file to write

	fptr = fopen("Student.bin", "wb");
	
		if(fptr==NULL)
		{
			printf("Open file error !");
		}

	 else 

		// fseek(fptr, sizeof(struct stud_record), 1, SEEK_SET);
		while(!FALSE){
	
		
			if(fwrite(&data, sizeof(struct stud_record), 1, fptr)==0){
		
						end=TRUE;
			}
			else 
				break;
		}
			 fclose(fptr);
//	 fwrite(&start->, sizeof(struct stud_record), 1, fptr);
		getch();

	return start;

}

//////////////////////////////////////////////////////////////////////////////////////////////
// Search Record Function
struct stud_node *search(int target, struct stud_node *start)
{



	struct stud_node *previousPtr, *currentPtr;

	 previousPtr=currentPtr=0;
	
	 if(target == start->data.id){
	
	system("cls");

	printf("\nRecord Found !\n");
	printf("================\n");
		
	printf("Name : %s\n", start->data.name);
	printf("ID   : %4d\n", start->data.id);
	printf("Name : %.2f\n", start->data.weight);
	printf("Name : %.2f\n", start->data.height);
   
	 getch();
	
	
		return start;
}
else 
{
	
	previousPtr = start;
	currentPtr = start->next;
	
 
	//swap position
		while(currentPtr != NULL && currentPtr->data.id != target)
	{
		previousPtr = currentPtr;
		currentPtr = currentPtr->next;
	}

		if(currentPtr !=NULL && currentPtr->data.id == target){
	system("cls");
	printf("\nRecord Found !\n");
	printf("================\n");
	
	printf("Name : %s\n", currentPtr->data.name);
	printf("ID   : %4d\n", currentPtr->data.id);
	printf("Name : %.2f\n", currentPtr->data.weight);
	printf("Name : %.2f\n", currentPtr->data.height);
   
	getch();

	return start;
		}
	

		system("cls");
		printf("No Such Record !");
  	  	getch();
		return start;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Check Pointer 

int isEmpty(struct stud_node *start)
{
	return start == NULL;
}
/////////////////////////////////////////////////////////////////////////////////////////////
struct stud_node *edit(struct stud_node *start)
{
	
		
	int id; 

	printf("Enter Id to edit record :");
	scanf("%d", &id);

	getch();

	return start;
}

/////////////////////////////////////////////////////////////////////////////////////////////
  #2  
Old 01-Jul-2006, 08:46
JBMtK JBMtK is offline
New Member
 
Join Date: Jun 2006
Posts: 6
JBMtK is on a distinguished road

Re: Urgent ! Pls Help Me !


no one is gonna answer this post unless you change your topic title. Read the stickes.
  #3  
Old 01-Jul-2006, 10:09
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,242
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

Re: Urgent ! Pls Help Me !


It looks as though you read Guideline #1. You should have continued onto Guideline #2, #4, and especially #5.
__________________

Age is unimportant -- except in cheese
  #4  
Old 01-Jul-2006, 10:16
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough

Re: Urgent ! Pls Help Me !


Well, if they were holding their breath, they must be very blue. It's been a few years...

  #5  
Old 01-Jul-2006, 22:49
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,242
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

Re: Urgent ! Pls Help Me !


Good point, CG. I really hate it when new people resurrect old threads. Maybe we should automatically close them after 2 months of no action, so new people don't keep doing this. It's happened way too much lately.
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogToyota - 2008 August 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

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

All times are GMT -6. The time now is 20:50.


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