database

database for trn using structures or arrays

Page 1 of 1

4 Replies - 891 Views - Last Post: 21 March 2007 - 10:55 PM Rate Topic: -----

#1 pascal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-February 07

database

Post icon  Posted 21 March 2007 - 08:45 PM

[help help please]

this is the program:
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
 
# define MAX 50  /* constant*/
 
struct addr {				  /*struct called
 list*/
			 char trn[50]  ;
		  
		  
		   
 
			} list[MAX];	   /* 50 records*/
 
main()
{
	int
 choice;
 
	init_list();	  /* initialze the list */
 
	for() {
	  choice = menu_select();	/* get user's selection*/		switch(choice)
 {
		case 1: enter();		 /* enter a new entry */
				break;
	  }
	}
}
 
/*********************************************************/
/*		 Function del								  */
/*********************************************************/
del()
{
	int
 i;
	char str[9];
 
	inputs("enter trn: " , str , 9);
	i = find(str);
	if (i>=0) *list[i].trn = '\0'  ;
	else	  printf("not found\n") ;
}
 
 
/**********************************************************/
/*				   Function display					 */
/**********************************************************/
display(int i)
{
	printf("%s\n" , list[i].trn);
 
}
 
/**********************************************************/
/*					 Function enter					 */
/**********************************************************/
enter()
{
	int i;
 
	for(;;) {
	  i = find_free();		/* find a free structure */
	  if(i<0) {
		printf("list full\n");
		return;
	  }
	  inputs("enter trn: ", list[i].trn,50);		if(!*list[i].trn) break;	  /* stop entering */

	}
}
 
/**********************************************************/
/*					   Function find					*/
/**********************************************************/
find(char *trn)
{
	int i;  
	for(i=0; i<MAX; i++) 
	  if(!strcmp(trn ,list[i].trn)) break;
	if(i==MAX) return
	else 
	return i;
}
 
/**********************************************************/
/*				   Function find_free				   */
/**********************************************************/
find_free()
{
	register int i;   
	for(i=0; i<MAX; i++) 
	  if(!*list[i].trn) return i;  
	return;
}
 
/**********************************************************/
/*					  Function init_list				*/
/**********************************************************/
init_list()
{
	register int i;   
	for (i=0; i<MAX; i++) 
	  *list[i].trn = '\0'
}
 
/**********************************************************/
/*					Function inputs					 */
/**********************************************************/
inputs( char *prompt , char *s , int count)
{
	char str[9];
 
	do {
		printf(prompt);
		gets(str);
		if(strlen(str) 1; 1; MAX;>=count); 
			printf("\ntoo long\n");
	   } while(strlen(str)>=count);
 
	strcpy(s , str);
}
 
/**********************************************************/
/*						 Function load				  */
/**********************************************************/
load()
{
	FILE *fp;
 
	if ( (fp=fopen("mlist" , "rb")) == NULL) {
	   printf("cannot open file\n");
	   return;
	}
	printf("\nloading file\n");
	fread(list , sizeof list , 1 , fp);
	if (ferror(fp)) 
	   printf("An error occurred while reading file.\n");
	fclose(fp);
}
 
/**********************************************************/
/*						  Function menu_select		  */
/**********************************************************/
 
menu_select()
{
	char s[9];
	int c;
	printf("1. Enter a trn\n") ;
	
 
   
  
   
   
 
	do {
		printf("\nEnter your choice: ");
		gets(s);
		c = atoi(s);
	   } while(c<0 || c>7);
 
	return c;
}
 
/**********************************************************/
/*					  Function save					 */
/**********************************************************/
save()
{
	FILE *fp;
 
	if ( (fp=fopen("mlist" , "wb")) == NULL) {
	  printf("cannot open file\n");
	  return;
	}
	printf("\nsaving file\n");
	fwrite(list , sizeof list , 1 , fp);
	if (ferror(fp)) 
	  printf("An error occurred while writing file.\n");
	fclose(fp);
}
 
/**********************************************************/
/*						Function search				 */
/**********************************************************/
search()
{
	int i;
	char trn[50];
 
	inputs("enter trn to find: " , trn , 50);
	if ((i=find(trn))<0) 
	  printf("not found\n");
	else display(i);
}
 
/**********************************************************/
/*						 Function show_list			 */
/**********************************************************/
show_list()
{
	int i;
 
   
 for(i=0; i<MAX; i++) {
	  if(*list[i].trn) {
		display(i);
		printf("\n\n");
	  }
	}
 
	printf("\n\n");
}


Is This A Good Question/Topic? 0
  • +

Replies To: database

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: database

Posted 21 March 2007 - 08:59 PM

You will need to provide a description of the problem you are having in order for anyone to be able to help.

Include error messages or any unexpected output you may be experiencing.
Was This Post Helpful? 0
  • +
  • -

#3 pascal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-February 07

Re: database

Posted 21 March 2007 - 09:01 PM

View Postjayman9, on 21 Mar, 2007 - 08:59 PM, said:

You will need to provide a description of the problem you are having in order for anyone to be able to help.

Include error messages or any unexpected output you may be experiencing.



ok errors are:

:\Documents and Settings\p.J\My Documents\database.cpp(21) : error C2065: 'init_list' : undeclared identifier
C:\Documents and Settings\p.J\My Documents\database.cpp(23) : error C2143: syntax error : missing ';' before ')'
C:\Documents and Settings\p.J\My Documents\database.cpp(23) : error C2143: syntax error : missing ';' before ')'
C:\Documents and Settings\p.J\My Documents\database.cpp(24) : error C2065: 'menu_select' : undeclared identifier
C:\Documents and Settings\p.J\My Documents\database.cpp(26) : error C2065: 'enter' : undeclared identifier
C:\Documents and Settings\p.J\My Documents\database.cpp(30) : warning C4508: 'main' : function should return a value; 'void' return type assumed
C:\Documents and Settings\p.J\My Documents\database.cpp(41) : error C2065: 'inputs' : undeclared identifier
C:\Documents and Settings\p.J\My Documents\database.cpp(42) : error C2065: 'find' : undeclared identifier
C:\Documents and Settings\p.J\My Documents\database.cpp(45) : warning C4508: 'del' : function should return a value; 'void' return type assumed
C:\Documents and Settings\p.J\My Documents\database.cpp(55) : warning C4508: 'display' : function should return a value; 'void' return type assumed
C:\Documents and Settings\p.J\My Documents\database.cpp(61) : error C2373: 'enter' : redefinition; different type modifiers
C:\Documents and Settings\p.J\My Documents\database.cpp(65) : error C2065: 'find_free' : undeclared identifier
C:\Documents and Settings\p.J\My Documents\database.cpp(68) : warning C4508: 'enter' : function should return a value; 'void' return type assumed
C:\Documents and Settings\p.J\My Documents\database.cpp(79) : error C2373: 'find' : redefinition; different type modifiers
C:\Documents and Settings\p.J\My Documents\database.cpp(84) : error C2059: syntax error : 'else'
C:\Documents and Settings\p.J\My Documents\database.cpp(86) : warning C4508: 'find' : function should return a value; 'void' return type assumed
C:\Documents and Settings\p.J\My Documents\database.cpp(92) : error C2373: 'find_free' : redefinition; different type modifiers
C:\Documents and Settings\p.J\My Documents\database.cpp(96) : error C2617: 'find_free' : inconsistent return statement
C:\Documents and Settings\p.J\My Documents\database.cpp(91) : see declaration of 'find_free'
C:\Documents and Settings\p.J\My Documents\database.cpp(103) : error C2373: 'init_list' : redefinition; different type modifiers
C:\Documents and Settings\p.J\My Documents\database.cpp(107) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\p.J\My Documents\database.cpp(107) : warning C4508: 'init_list' : function should return a value; 'void' return type assumed
C:\Documents and Settings\p.J\My Documents\database.cpp(113) : error C2373: 'inputs' : redefinition; different type modifiers
C:\Documents and Settings\p.J\My Documents\database.cpp(119) : error C2143: syntax error : missing ')' before 'constant'
C:\Documents and Settings\p.J\My Documents\database.cpp(119) : error C2143: syntax error : missing ';' before '>='
C:\Documents and Settings\p.J\My Documents\database.cpp(119) : error C2059: syntax error : ')'
C:\Documents and Settings\p.J\My Documents\database.cpp(121) : warning C4018: '>=' : signed/unsigned mismatch
C:\Documents and Settings\p.J\My Documents\database.cpp(124) : warning C4508: 'inputs' : function should return a value; 'void' return type assumed
C:\Documents and Settings\p.J\My Documents\database.cpp(135) : warning C4508: 'load' : function should return a value; 'void' return type assumed
C:\Documents and Settings\p.J\My Documents\database.cpp(149) : error C2373: 'menu_select' : redefinition; different type modifiers
C:\Documents and Settings\p.J\My Documents\database.cpp(178) : warning C4508: 'save' : function should return a value; 'void' return type assumed
C:\Documents and Settings\p.J\My Documents\database.cpp(199) : warning C4508: 'search' : function should return a value; 'void' return type assumed
C:\Documents and Settings\p.J\My Documents\database.cpp(217) : warning C4508: 'show_list' : function should return a value; 'void' return type assumed
Error executing cl.exe.

database.obj - 20 error(s), 12 warning(s)
Was This Post Helpful? 0
  • +
  • -

#4 ajwsurfer  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 373
  • Joined: 24-October 06

Re: database

Posted 21 March 2007 - 09:21 PM

Well, first all the functions need to be declared in front of any function that is using them (like main).

Example:

....
void del();

int main()
{
....


Second, all the functions need return types.

Third, if you are using C, which it looks like you are, all the functions need parameter types:

Example:

void del(void);

Aside from that, you just need to pick each compile time error off the top. Fix it and try again. Do not let the whole list get you down. Just fix the top error (which includes the line number btw).
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: database

Posted 21 March 2007 - 10:55 PM

Ok the first major error I found was the line:
if(strlen(str) 1; 1; MAX;>=count);
from inputs(), this is just one but syntax error. (I also found some missing semi-colons).

Next, I hate to do this, but I have to say something about the for(;;) this is a TERRIBLE structure and should NOT be used. I know that it works, and programmers use it all the time but it is not how the structure was meant to be used and it really is asking for trouble. Might as well just add in a 10: goto 10; line.

This structure works just fine. The problem comes in when the program gets large and there are many exits, or not enough exits. Very often the "break" command that are supposed to exit you from the loop end up inside of other structures (switches or other loops) and the program falls into a continuous loop etc. This useage is considered a hack (an acceptable but unintentional use of the language) and should only be used when other structures can't be used, or would be to complicated to impliment. i.e. when it is the RIGHT tool for the job. In this case it is not.

enter()
{
	int i;

	do 
	{
		i = find_free();		/* find a free structure */
		if(i<0) 
		{
			printf("list full\n");
			//return;
			break;
		}
		inputs("enter trn: ", list[i].trn,50);
		//if(!*list[i].trn) break;	  /* stop entering */
	} while (*list[i].trn)
	return; //try to only have one exit to a function (makes it easy to find).
}


The following will compile. However I did not run it because I already know it will enter an infinite loop and since I don't really know what it is doing I am not qualified to fix it. There are LOTS of changes. I used brackets to delineate what the if statments and for loops actually would do, I don't think this was always your intention but I hope that the brackets help illustrate why you should use them even when you don't think you need to.

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

# define MAX 50  /* constant*/
void show_list();
void del();
void display(int i);
void enter();
int find(char *trn);
int find_free();
void init_list();
void inputs( char *prompt , char *s , int count);
void load();
char menu_select();
void save();
void search();
void show_list();

struct addr 
{   /*struct called list*/
	char trn[50];
} list[MAX];	   /* 50 records*/

int main()
{
	int choice;
	init_list();	  /* initialze the list */
	for(;;) //No exit from this loop...
	{
		choice = menu_select();	/* get user's selection*/
		switch(choice)
		{
			case 1: 
				enter();		 /* enter a new entry */
				break;  //This breaks the switch statement but not the for(;;)
		}
	}
}

/*********************************************************/
/*		 Function del								  */
/*********************************************************/
void del()
{
	int i;
	char str[9];

	inputs("enter trn: " , str , 9);
	i = find(str);
	if (i>=0) { *list[i].trn = '\0'; }
	else { printf("not found\n");}
	return;
}


/**********************************************************/
/*				   Function display					 */
/**********************************************************/
void display(int i)
{
	printf("%s\n" , list[i].trn);
}

/**********************************************************/
/*					 Function enter					 */
/**********************************************************/
void enter()
{
	int i;

/*	Using the '//' type of line comments rather than the /* kind allows
	  you to comment out sections of code which is better for debugging.
	for(;;) 
	{
		i = find_free();		// find a free structure 
		if(i<0) 
		{
			printf("list full\n");
			return;
		}
		inputs("enter trn: ", list[i].trn,50);
		if(!*list[i].trn) break;	  // stop entering 
	}
	return;
*/
	do
	{
		i = find_free();		// find a free structure 
		if(i<0) 
		{
			printf("list full\n");
			return;
		}
		inputs("enter trn: ", list[i].trn,50);
		//if(!*list[i].trn) break;	  // stop entering 
	} while (*list[i].trn);
	return;
}

/**********************************************************/
/*					   Function find					*/
/**********************************************************/
int find(char *trn)
{
	int i;  
	for(i=0; i<MAX; i++)
	{
		if(!strcmp(trn ,list[i].trn)) { break;}
	}
	if(i==MAX) { return 0; }
	else {return i; }
}

/**********************************************************/
/*				   Function find_free				   */
/**********************************************************/
int find_free()
{
	register int i;  
	for(i=0; i<MAX; i++)
	{
		if(!*list[i].trn) return i;
	}
	return 0;
}

/**********************************************************/
/*					  Function init_list				*/
/**********************************************************/
void init_list()
{
	register int i;  
	for (i=0; i<MAX; i++)
	{
	  *list[i].trn = '\0';
	}
	return;
}

/**********************************************************/
/*					Function inputs					 */
/**********************************************************/
void inputs( char *prompt , char *s , int count)
{
	char str[9];

	do {
		printf(prompt);
		gets(str);
		//if(strlen(str) 1; 1; MAX;>=count);
			printf("\ntoo long\n");
	   } while(strlen(str)>=(unsigned int)count);

	strcpy(s , str);
	return;
}

/**********************************************************/
/*						 Function load				  */
/**********************************************************/
void load()
{
	FILE *fp;

	if ( (fp=fopen("mlist" , "rb")) == NULL) 
	{
		printf("cannot open file\n");
		return;
	}
	printf("\nloading file\n");
	fread(list , sizeof list , 1 , fp);
	if (ferror(fp))
	{
		printf("An error occurred while reading file.\n");
	}
	fclose(fp);
	return;
}

/**********************************************************/
/*						  Function menu_select		  */
/**********************************************************/

char menu_select()
{
	char s[9];
	int c;
	printf("1. Enter a trn\n");
	
	do 
	{
		printf("\nEnter your choice: ");
		gets(s);
		c = atoi(s);
	} while(c<0 || c>7);

	return c;
}

/**********************************************************/
/*					  Function save					 */
/**********************************************************/
void save()
{
	FILE *fp;

	if ( (fp=fopen("mlist" , "wb")) == NULL) 
	{
		printf("cannot open file\n");
		return;
	}
	printf("\nsaving file\n");
	fwrite(list , sizeof list , 1 , fp);
	if (ferror(fp))
	{
		printf("An error occurred while writing file.\n");
	}
	fclose(fp);
	return;
}

/**********************************************************/
/*						Function search				 */
/**********************************************************/
void search()
{
	int i;
	char trn[50];

	inputs("enter trn to find: " , trn , 50);
	if ((i=find(trn))<0)
	{
	  printf("not found\n");
	} else 
	{ 
		display(i);
	}
	return;
}

/**********************************************************/
/*						 Function show_list			 */
/**********************************************************/
void show_list()
{
	int i;

  
	for(i=0; i<MAX; i++) 
	{
		if(*list[i].trn) 
		{
			display(i);
			printf("\n\n");
		}
	}
	printf("\n\n");
	return;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1