-
1 Attachment(s)
help with this C code
I am trying to do this project as a home study the problem is i don't know where the errors are int his program could someone please help me out here i have enclose a file from borland c++ 4.5 and also pasted the code here i am getting 2 errors just don't know how to relsove them this is the code and you can download this file
/* order system */
# include <stdio.h>
# include "struct.h"
void addnew( void);
void display(void);
void find_rec(void);
void sort(void );
void delete_rec(void);
main()
{
int choice;
char answer,password[30], newpassword[30];
char sex, fpsex;
FILE * fp;
putchar(26);
fp=fopen("sex.dat","a+");
if(fp==(FILE*)NULL)
{
printf("Error can't open\n");
}
printf("\t\t\t***********************************\n");
printf("\t\t\t Welcome to KCT System \n");
printf("\t\t\t***********************************\n");
if(newpassword=="")
{
printf("Please create a new password->\n");
fflush(stdin);
gets(newpassword);
fwrite((char*)&sex,sizeof(sex),1,fp);
}
else
{
printf("Enter Password to access data-->\n");
fflush(stdin);
gets(password);
fread((char*)&sex,sizeof(sex),1,fp);
}
if(stremp(newpassword, password))
{
answer= 'y';
do
{
printf("\t\t\t***********************************\n");
printf("\t\t\t KCT TOP SECERT \n");
printf("\t\t\t Access for Staff Only \n");
printf("\t\t\t***********************************\n");
puts("\n\n");
printf("\t\t\t***********************************\n");
printf("\t\t\t** 1.Add new record **\n");
printf("\t\t\t** 2.Find a record **\n");
printf("\t\t\t** 3.Display Record **\n");
printf("\t\t\t** 4.Sort in order **\n");
printf("\t\t\t** 5.Delete record **\n");
printf("\t\t\t** 6.Quit **\n");
printf("\t\t\t***********************************\n");
fflush(stdin);
scanf("%d",&choice);
switch(choice)
case 1: addnew();
break;
case 2: find_rec();
break;
case 3: display();
break;
case 4: sort();
break;
case 5: delete_rec();
break;
case 6: printf("\n\nGood bye!\n");
break;
default : printf("Please select \n");
break;
/* end of switch */
}while(choice != 6);/* end of do statement*/
} /* end of if */
else
{
printf("Wrong password\n");
fread((char*)&sex,sizeof(sex),1,fp);
} /*end of else */
fclose(fp);
} /* end of the while station ifeof */
void addnew(void)
{
INFORMATTION data, *ptr;
FILE *fp;
char answer;
putchar(26);
ptr= &data;
fp=fopen("personal.dat","a+");
if(fp==(FILE*)NULL)
{
printf("Error can't open!\n");
exit();
}
{while(answer != 'n' && answer != 'N')
do
{
printf("\t\t\t***********************************\n");
printf("\t\t\t ADD NEW RECORD \n");
printf("\t\t\t KCT TOP INFORMATION \n");
printf("\t\t\t***********************************\n");
puts("\n\n");
printf("Please enter your first name:--> \n");
fflush(stdin);
gets(data.surname);
printf("Please Enter age:--> \n");
fflush(stdin);
scanf("%d",&data.age);
prinf("Please Enter address:--> \n");
fflush(stdin);
gets(data.address);
printf("Enter ID number:--> \n");
fflush(stdin);
scanf("%d",&data.ID);
printf("\n These are your details \n");
printf("Name: %s\n",ptr->name);
printf("Surname: %s\n",ptr->surname);
printf("Age : %d\n", ptr->age);
printf("Address: %s \n", ptr->address);
printf("ID : %d\n",ptr->ID);
printf("are these details correct? \n");
printf("\n Type Y=yes N=no");
fflush(stdin);
scanf("%c",&answer);
}while(answer !='y' && answer !='Y');
printf("\nDo you have any more records !-->");
printf("\n Type Y=yes N=no");
fflush(stdin);
scanf("%c",&answer);
fwrite((char*)&data,sizeof(INFORMATION),1,FP);
} /*END OF THIS LOOP */
fclose(fp);
}
-
-
I've seen a load of errors that are just syntax etc.
e.g.
- no function type for main - int main(void) etc. This might not be necessary I suppose but the compiler will wonder what the return type should be
- switch statement requires braces '{' around case statements
- I assume 'stremp' is actually 'strcmp'
- INFORMATTION type is not declared
I wont continue cos this will cause all sorts of problems.
If I've not called this correctly then sorry, but most of what I've seen in the code is simple syntax.
HD
-
If it's old C, then main() { } is valid, due to the "implicit int" mis-feature. This has since been dropped, so C99 will complain about that.
-
i shall re- read my code again thanks guy's for spotting out the mistake .