trn database

help with the program that i wrote

Page 1 of 1

1 Replies - 836 Views - Last Post: 07 March 2007 - 07:30 PM Rate Topic: -----

#1 pascal  Icon User is offline

  • New D.I.C Head

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

trn database

Post icon  Posted 07 March 2007 - 07:22 PM

[help please]

i am trying to write a c program to populate a database using structures or arrays.the database should hold 50 trn numbers. this is the program i have 28 errors:

# 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[255];

inputs("enter name: " , str , 30);
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 name: ", 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].name)) 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[255];

do {
printf(prompt);
gets(str);
if(strlen(str) 1; 1; ;>=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[80];
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 name[30];

inputs("enter trn
to find: " , trn , 30);
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");
}


errors:

c:\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(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(43) : error C2039: 'name' : is not a member of 'addr'
c:\documents and settings\p.j\my documents\database.cpp(7) : see declaration of 'addr'
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(82) : error C2039: 'name' : is not a member of 'addr'
c:\documents and settings\p.j\my documents\database.cpp(7) : see declaration of 'addr'
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(85) : warning C4508: 'find' : function should return a value; 'void' return type assumed
c:\documents and settings\p.j\my documents\database.cpp(91) : error C2373: 'find_free' : redefinition; different type modifiers
c:\documents and settings\p.j\my documents\database.cpp(96) : error C2059: syntax error : '}'
c:\documents and settings\p.j\my documents\database.cpp(102) : error C2373: 'init_list' : redefinition; different type modifiers
c:\documents and settings\p.j\my documents\database.cpp(106) : error C2143: syntax error : missing ';' before '}'
c:\documents and settings\p.j\my documents\database.cpp(106) : warning C4508: 'init_list' : function should return a value; 'void' return type assumed
c:\documents and settings\p.j\my documents\database.cpp(112) : error C2373: 'inputs' : redefinition; different type modifiers
c:\documents and settings\p.j\my documents\database.cpp(118) : error C2143: syntax error : missing ')' before 'constant'
c:\documents and settings\p.j\my documents\database.cpp(118) : error C2143: syntax error : missing ';' before '>='
c:\documents and settings\p.j\my documents\database.cpp(118) : error C2059: syntax error : ')'
c:\documents and settings\p.j\my documents\database.cpp(119) : warning C4018: '>=' : signed/unsigned mismatch
c:\documents and settings\p.j\my documents\database.cpp(122) : warning C4508: 'inputs' : function should return a value; 'void' return type assumed
c:\documents and settings\p.j\my documents\database.cpp(133) : warning C4508: 'load' : function should return a value; 'void' return type assumed
c:\documents and settings\p.j\my documents\database.cpp(138) : error C2001: newline in constant
c:\documents and settings\p.j\my documents\database.cpp(139) : error C2146: syntax error : missing ')' before identifier 'file'
c:\documents and settings\p.j\my documents\database.cpp(139) : error C2017: illegal escape sequence
c:\documents and settings\p.j\my documents\database.cpp(139) : error C2001: newline in constant
c:\documents and settings\p.j\my documents\database.cpp(148) : error C2373: 'menu_select' : redefinition; different type modifiers
c:\documents and settings\p.j\my documents\database.cpp(177) : warning C4508: 'save' : function should return a value; 'void' return type assumed
c:\documents and settings\p.j\my documents\database.cpp(194) : error C2001: newline in constant
c:\documents and settings\p.j\my documents\database.cpp(195) : error C2146: syntax error : missing ')' before identifier 'to'
c:\documents and settings\p.j\my documents\database.cpp(195) : error C2001: newline in constant
c:\documents and settings\p.j\my documents\database.cpp(198) : error C2181: illegal else without matching if
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 - 28 error(s), 12 warning(s)

Is This A Good Question/Topic? 0
  • +

Replies To: trn database

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: trn database

Posted 07 March 2007 - 07:30 PM

To begin with, you are getting a lot of undeclared identifier errors when you call a function because you have not provided a prototype before the main function (or alternately defined the functions before the main function). Move your function definitions above the main function. Another area that needs attention is that you have a case statement in your main function, but are not switching on a variable. there are some misplaced semi colons, and missing elements to if else statements as well.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1