Below are a program that supposed to ask a user to enter a user ID and if found, it will then modify the very same person details in the record and if not found then calls the add function to prompt the user details and add into the record.txt as new entry I've been doing this about 3 days and error still occured pls help

/*Function 1a*/

void modify_record(){
char Name[50];
int Target, ID, selection, Found=0;
FILE *rec, *temp;

temp=fopen("temp.txt", "w+");
if((rec=fopen("record.txt", "r+")) == NULL)
		printf("\nerror: file not found.\n");
else	{
		printf(" ENTER the employee ID to be modified: ");
      scanf("%d", &ID);
      while(!feof(rec)){
			fscanf(rec, "%i %*c %c %*c", &ID, &Name, &Gender, &Department);
			if(feof(rec))
         break;
         else if(Target==ID)
         fprintf(temp, "%i %*c %c %*c", ID, Name, Gender, Department);


         else
	         {
   	      Found=1;
            printf("\n Record Found! Please re-enter details for the employee ::%s::", Name);

      /*Modify employee details*/


            gotoxy(1,15);
             printf("\n Employee details edit ");
                 do
                 {
   	   		gotoxy(1, 22);
					printf(" Name  : 											  ");
              	gotoxy(10, 22);
              	fflush(stdin);
	           	scanf("%s",&Name);
					printf(" Gender : ");
              	fflush(stdin);
	           	scanf("%s",&Gender);
	           	printf("Please enter the department for this employee, [0] Administration [1] Management [2] Accounting [3] Others", empty, empty, empty, empty);
              	fflush(stdin);
	           	scanf("%d",&selection);
	              	if(selection==0 ){
                   printf("\n Your selection is %s\n\n%s%s\n ", Department[selection], empty, empty);
		         	fprintf(temp, "%i %*c %c %*c", ID, Name, Gender, Department[selection]);
                 	}
                  else if(selection==1){
                  printf("\n Your selection is %s\n\n%s%s\n ", Department[selection], empty, empty);
		         	fprintf(temp, "%i %*c %c %*c", ID, Name, Gender, Department[selection]);
                  }
                  else if(selection==2){
                  printf("\n Your selection is %s\n\n%s%s\n ", Department[selection], empty, empty);
		         	fprintf(temp, "%i %*c %c %*c", ID, Name, Gender, Department[selection]);
                  }
                  else if(selection==3){
                   printf("\n Your selection is %s\n\n%s%s\n ", Department[selection], empty, empty);
		         	fprintf(temp, "%i %*c %c %*c", ID, Name, Gender, Department[selection]);
                  }
                  else
            	printf("\n\n INVALID CHOICE! PLEASE Re-Enter.\a");
                  } while(selection!=0 && selection!=1 && selection!=2 && selection!=3);
                  }

             }
              if(!Found)
              printf("\n RECORD CANNOT BE FOUND!\n");
              add_record();
             }
fclose(rec);
fclose(temp);
remove("record.txt");
rename("temp.txt", "record.txt");
getch();
clrscr();
menu();
             }




             /*Function 1b adding employee*/

             void add_record(){
             int ID;
             int selection;
             FILE *rec, *temp;

if ((rec=fopen("record.txt","a+"))==NULL)
ID = 1;
else
{
 do{
    fscanf(rec, "%i %*c %c %*c", &ID, &Name, &Gender, &Department);
    }while(!feof(rec));
    ID += 1;
    }

        do	{
	gotoxy(2, 15);
	printf("\n\n 0) %s\n 1) %s\n 2) %s\n 3) %s\n\n [Enter  Department for this new employee]: ", Department[0], Department[1], Department[2], Department[3]);
   fflush(stdin);
	scanf("%d", &selection);
  	if((selection != 0) && (selection != 1) && (selection !=2) && (selection !=3)) {gotoxy(1, 24); printf(" >>invalid Department<< Retry!\a");}
  	} while((selection != 0) && (selection != 1) && (selection !=2) && (selection !=3));


   			clrscr();
            printf("The new ID for this employee: %04i", ID);
   			printf("\n Employee Name : \n");
            fflush(stdin);
            gets(Name);
   			printf("\n Employee Gender: \n");
            scanf("%c", &Gender);
            printf("\nThe New Employee Record have successfully Saved!");
         	   		fprintf(rec, "%i %[^/]%*c %c %c", ID, Name, Gender, Department);
            			printf("\n Employee Status Stored!");
            			fclose(rec);

 }

Recommended Answers

All 13 Replies

where the hell did you learn this i cant learn any programming things help im a newbie sorry that this does not answeer ur ques wish i could help

>and error still occured pls help
You didn't specify what the error was.

>I am a girl doing my 1st simple program
"Oh great, is Narue going to bash the title now?" Yes. :) First, it's not informative at all. Second, it has useless information. We don't care if it's a simple program or not, nor do we care about your gender. A better title would be one that tells us what the problem is because you didn't say in the body of your post. :rolleyes:

Maybe I should make all of my re: titles "I'm a girl mocking my 93756th newbie". :mrgreen:

By the way, your code makes almost every mistake in the book. I say almost because the example is incomplete. I'm sure if it were a fully compilable example it would make every mistak in the book.

thats a bit harsh she only asking for help

The error was that if supposed to create a Record.txt and place the employee details there and i found out that if a record.txt is there it will stuck in the menu where you it prompt you to insert a menu and you make your selection. When I delete the record.txt, i works as if it was perfect when you add a new entry but the data that it stored in the record.txt may caused some errors as it is as

> 4198977 %[^/]%*c %c %c

I dont know if it's correct but I am thinking it should be

> 4198977(id) Robinson(name) M(gender) Accounting(department)

below are the partial full program I did not include the other 2 for I think it dont have any errors

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

char Name[50];
char Department [4] [50] = {"Accounting", "Administration", "Management" , "Others"};
int ID[50];
char Gender [2] [50] = {"Male", "Female"} ;
char *stars="********================*********";
char *lines="---------------------------------";
char *empty="                                 ";
int txt_inserted;

/*Functions*/

void modify_record();
void add_record();
void search_record();
//void list_record();
void delete_record();
//void exit();
void menu();

/* MAIN */
main(){
clrscr();
menu();
getch();
return 0;
}

/* FUNCTION: MENU */
void menu(){
int selection;

FILE *start;
if((start = fopen("record.txt", "a+")) == NULL)
printf(" File record.txt is not found! Previous saved employee records did not loaded\n\n");
else {
fscanf(start, "%i %*c %c %*c, &ID", &Name, &Gender, &Department);
printf(" Values Uploaded: %i %*c %c %*c", &ID, &Name, &Gender, &Department);
fclose(start);}

gotoxy(15,1);
puts("\n        -------* [Welcome To Net Communication Human Resource] *-------");
printf("\t%s\n",stars);
puts("\t#\t1.Staff Biodata Maintenance\t#");
puts("\t#\t2.Search Employee Records via Emplyee ID\t#");
puts("\t#\t3.Display all employee by department\t#");
puts("\t#\t4.Employee Record deletion\t#");
puts("\t#\t5.Exit\t#");
printf("\t%s\n",stars);
printf("\n Enter Your Selection: ");
fflush(stdin);
scanf("%d", &selection);

if (selection == 1) add_record();
else if (selection == 2) search_record();
//else if (selection == 3) list_record();
else if (selection == 4) delete_record();
//else if (selection == 5) exit();
else{ printf(" Please only select from the above menu"); getch(); clrscr(); menu();}
}

/*Function 1a*/

void modify_record(){
char Name[50];
int Target, ID, selection, Found=0;
FILE *rec, *temp;

temp=fopen("temp.txt", "w+");
if((rec=fopen("record.txt", "r+")) == NULL)
		printf("\nerror: file not found.\n");
else	{
		printf(" ENTER the employee ID to be modified: ");
      scanf("%d", &Target);
      while(!feof(rec)){
			fscanf(rec, "%i %*c %c %*c", &ID, &Name, &Gender, &Department);
			if(feof(rec))
         break;
         else if(Target==ID)
         fprintf(temp, "%i %*c %c %*c", ID, Name, Gender, Department);


         else
	         {
   	      Found=1;
            printf("\n Record Found! Please re-enter details for the employee ::%s::", Name);

      /*Modify employee details*/


            gotoxy(1,15);
             printf("\n Employee details edit ");
                 do
                 {
   	   		gotoxy(1, 22);
					printf(" Name  : 											  ");
              	gotoxy(10, 22);
              	fflush(stdin);
	           	scanf("%s",&Name);
					printf(" Gender : ");
              	fflush(stdin);
	           	scanf("%s",&Gender);
	           	printf("Please enter the department for this employee, [0] Administration [1] Management [2] Accounting [3] Others", empty, empty, empty, empty);
              	fflush(stdin);
	           	scanf("%d",&selection);
	              	if(selection==0 ){
                   printf("\n Your selection is %s\n\n%s%s\n ", Department[selection], empty, empty);
		         	fprintf(temp, "%i %*c %c %*c", ID, Name, Gender, Department[selection]);
                 	}
                  else if(selection==1){
                  printf("\n Your selection is %s\n\n%s%s\n ", Department[selection], empty, empty);
		         	fprintf(temp, "%i %*c %c %*c", ID, Name, Gender, Department[selection]);
                  }
                  else if(selection==2){
                  printf("\n Your selection is %s\n\n%s%s\n ", Department[selection], empty, empty);
		         	fprintf(temp, "%i %*c %c %*c", ID, Name, Gender, Department[selection]);
                  }
                  else if(selection==3){
                   printf("\n Your selection is %s\n\n%s%s\n ", Department[selection], empty, empty);
		         	fprintf(temp, "%i %*c %c %*c", ID, Name, Gender, Department[selection]);
                  }
                  else
            	printf("\n\n INVALID CHOICE! PLEASE Re-Enter.\a");
                  } while(selection < 0 || selection > 3);
                  }

             }
              if(!Found)
              printf("\n RECORD CANNOT BE FOUND!\n");
              add_record();
             }
fclose(rec);
fclose(temp);
remove("record.txt");
rename("temp.txt", "record.txt");
getch();
clrscr();
menu();
             }




             /*Function 1b adding employee*/

             void add_record(){
             int ID;
             int selection;
             FILE *rec, *temp;

if ((rec=fopen("record.txt","a+"))==NULL)
ID = 1;
else
{
 do{
    fscanf(rec, "%i %*c %c %*c", &ID, &Name, &Gender, &Department);
    }while(!feof(rec));
    ID += 1;
    }

        do	{
	gotoxy(2, 15);
	printf("\n\n 0) %s\n 1) %s\n 2) %s\n 3) %s\n\n [Enter  Department for this new employee]: ", Department[0], Department[1], Department[2], Department[3]);
   fflush(stdin);
	scanf("%d", &selection);
  	if((selection != 0) && (selection != 1) && (selection !=2) && (selection !=3)) {gotoxy(1, 24); printf(" >>invalid Department<< Retry!\a");}
  	} while((selection != 0) && (selection != 1) && (selection !=2) && (selection !=3));


   			clrscr();
            printf("The new ID for this employee: %04i", ID);
   			printf("\n Employee Name : \n");
            fflush(stdin);
            fgets(Name, 49, stdin);
   			printf("\n Employee Gender: \n");
            scanf("%c", &Gender);
            printf("\nThe New Employee Record have successfully Saved!");
         	   		fprintf(rec, "%i %[^/]%*c %c %c", ID, Name, Gender, Department);
            			printf("\n Employee Status Stored!");
            			fclose(rec);

 }

And to Narue, I didnt meant to try to mock or anything when I stated I am a female I just wanted to let others know who am I. I have my freedom to the limitation and exposure of my identity and you can skip it if you dont care. I am not trying to cause any trouble directly and indirectly and don't know stating i am a female can causes some misunderstanding but for that I apologised.

>and don't know stating i am a female can causes some misunderstanding
There's no misunderstanding at all, it just has connotations that I find insulting for women as a whole.

>fprintf(temp, "%i %*c %c %*c", ID, Name, Gender, Department);
Format modifiers for scanf and printf are not the same. The asterisk modifier for scanf means to discard the value, but for printf it means that the field width is specified by an argument. So you're trying to print ID characters from name (which is probably wrong), then one character of Gender, then all hell breaks loose because you don't have enough arguments after that.

Repeat that explanation as necessary for the number of offenses in your code.

That's the problem you know of. The problems you don't know of are many and they include:

>fflush(stdin);
This has undefined behavior.

>while(!feof(rec)){
feof is not designed to be used as a loop condition, so to avoid file reading problems you need to use ugly ad hoc workarounds.

These are only the real problems. Portability problems, error handling problems, clarity problems, etc. weren't mentioned.

Thanks for your help now there is still a problem in the add_record function where it always stored the department as Accounting eventhough if you entered as Administration or other or Management. I messed with it still cannot make it though

Are you properly indexing the array?

where the hell did you learn this i cant learn any programming things help im a newbie sorry that this does not answeer ur ques wish i could help

thats a bit harsh she only asking for help

...These two posts are kind of off-topic to this thread. If you need help learning something, start your own thread, and ask whatever questions you'd like.

Also, Narue(yes, I'm going to poke at you, Narue) can be a little abrasive at times. But, despite the abrasiveness, he was dead-on. It is of great importance to post a descriptive subject line, as people may actually ignore your post if you don't have a good subject line. It is to your advantage to provide as much information as possible...

...We now return you to your regularly scheduled C programming thread, already in progress...

>yes, I'm going to poke at you, Narue
Feel free. :)

>can be a little abrasive at times
Yes indeed, though it it makes anyone feel better (not that I really care), I'm a great deal nicer on other forums.

>he was dead-on
She was dead-on, thank you. Not that I'm anal about it, but people always ask me why I never mention that I'm a girl when they call me "he", and that gets old.

>yes, I'm going to poke at you, Narue
Feel free. :)

>can be a little abrasive at times
Yes indeed, though it it makes anyone feel better (not that I really care), I'm a great deal nicer on other forums.

>he was dead-on
She was dead-on, thank you. Not that I'm anal about it, but people always ask me why I never mention that I'm a girl when they call me "he", and that gets old.

Now standing corrected. I was misled by your avatar-- thought it was indicative of your gender. ;) I mean, we obviously know how manly I am, what being Cookie Monster and all. Heck... now that I think of it, I can't even tell whether the person depicted in your avatar is a guy or a girl!

>I can't even tell whether the person depicted in your avatar is a guy or a girl!
My my, so deprived that you haven't seen Cowboy Bebop? ;) My avatar is Radical Edward, who is a girl. Though I once used a crystal ball as my avatar, so you can't really judge gender based on that or the nickname.

your program are hard!! really, and i don't know where have u get an error

just try to analyzed or ask for help to your professor and guest he would guide you to your problem

Watch your dates, Acer - this thread was from 2004. <Yikes!> ;)

And Welcome to the forum, Acer!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.