944,207 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 4241
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 25th, 2004
0

I am a girl doing my 1st simple program

Expand Post »
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*/

  1. void modify_record(){
  2. char Name[50];
  3. int Target, ID, selection, Found=0;
  4. FILE *rec, *temp;
  5.  
  6. temp=fopen("temp.txt", "w+");
  7. if((rec=fopen("record.txt", "r+")) == NULL)
  8. printf("\nerror: file not found.\n");
  9. else {
  10. printf(" ENTER the employee ID to be modified: ");
  11. scanf("%d", &ID);
  12. while(!feof(rec)){
  13. fscanf(rec, "%i %*c %c %*c", &ID, &Name, &Gender, &Department);
  14. if(feof(rec))
  15. break;
  16. else if(Target==ID)
  17. fprintf(temp, "%i %*c %c %*c", ID, Name, Gender, Department);
  18.  
  19.  
  20. else
  21. {
  22. Found=1;
  23. printf("\n Record Found! Please re-enter details for the employee ::%s::", Name);
  24.  
  25. /*Modify employee details*/
  26.  
  27.  
  28. gotoxy(1,15);
  29. printf("\n Employee details edit ");
  30. do
  31. {
  32. gotoxy(1, 22);
  33. printf(" Name : ");
  34. gotoxy(10, 22);
  35. fflush(stdin);
  36. scanf("%s",&Name);
  37. printf(" Gender : ");
  38. fflush(stdin);
  39. scanf("%s",&Gender);
  40. printf("Please enter the department for this employee, [0] Administration [1] Management [2] Accounting [3] Others", empty, empty, empty, empty);
  41. fflush(stdin);
  42. scanf("%d",&selection);
  43. if(selection==0 ){
  44. printf("\n Your selection is %s\n\n%s%s\n ", Department[selection], empty, empty);
  45. fprintf(temp, "%i %*c %c %*c", ID, Name, Gender, Department[selection]);
  46. }
  47. else if(selection==1){
  48. printf("\n Your selection is %s\n\n%s%s\n ", Department[selection], empty, empty);
  49. fprintf(temp, "%i %*c %c %*c", ID, Name, Gender, Department[selection]);
  50. }
  51. else if(selection==2){
  52. printf("\n Your selection is %s\n\n%s%s\n ", Department[selection], empty, empty);
  53. fprintf(temp, "%i %*c %c %*c", ID, Name, Gender, Department[selection]);
  54. }
  55. else if(selection==3){
  56. printf("\n Your selection is %s\n\n%s%s\n ", Department[selection], empty, empty);
  57. fprintf(temp, "%i %*c %c %*c", ID, Name, Gender, Department[selection]);
  58. }
  59. else
  60. printf("\n\n INVALID CHOICE! PLEASE Re-Enter.\a");
  61. } while(selection!=0 && selection!=1 && selection!=2 && selection!=3);
  62. }
  63.  
  64. }
  65. if(!Found)
  66. printf("\n RECORD CANNOT BE FOUND!\n");
  67. add_record();
  68. }
  69. fclose(rec);
  70. fclose(temp);
  71. remove("record.txt");
  72. rename("temp.txt", "record.txt");
  73. getch();
  74. clrscr();
  75. menu();
  76. }
  77.  
  78.  
  79.  
  80.  
  81. /*Function 1b adding employee*/
  82.  
  83. void add_record(){
  84. int ID;
  85. int selection;
  86. FILE *rec, *temp;
  87.  
  88. if ((rec=fopen("record.txt","a+"))==NULL)
  89. ID = 1;
  90. else
  91. {
  92. do{
  93. fscanf(rec, "%i %*c %c %*c", &ID, &Name, &Gender, &Department);
  94. }while(!feof(rec));
  95. ID += 1;
  96. }
  97.  
  98. do {
  99. gotoxy(2, 15);
  100. 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]);
  101. fflush(stdin);
  102. scanf("%d", &selection);
  103. if((selection != 0) && (selection != 1) && (selection !=2) && (selection !=3)) {gotoxy(1, 24); printf(" >>invalid Department<< Retry!\a");}
  104. } while((selection != 0) && (selection != 1) && (selection !=2) && (selection !=3));
  105.  
  106.  
  107. clrscr();
  108. printf("The new ID for this employee: %04i", ID);
  109. printf("\n Employee Name : \n");
  110. fflush(stdin);
  111. gets(Name);
  112. printf("\n Employee Gender: \n");
  113. scanf("%c", &Gender);
  114. printf("\nThe New Employee Record have successfully Saved!");
  115. fprintf(rec, "%i %[^/]%*c %c %c", ID, Name, Gender, Department);
  116. printf("\n Employee Status Stored!");
  117. fclose(rec);
  118.  
  119. }
Last edited by alc6379; Oct 26th, 2004 at 7:38 pm. Reason: added [code] tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kimimaro is offline Offline
11 posts
since Oct 2004
Oct 25th, 2004
0

Re: I am a girl doing my 1st simple program

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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
epsos is offline Offline
63 posts
since Oct 2004
Oct 25th, 2004
0

Re: I am a girl doing my 1st simple program

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 26th, 2004
0

Re: I am a girl doing my 1st simple program

thats a bit harsh she only asking for help
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
epsos is offline Offline
63 posts
since Oct 2004
Oct 26th, 2004
0

Re: I am a girl doing my 1st simple program

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

  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. char Name[50];
  8. char Department [4] [50] = {"Accounting", "Administration", "Management" , "Others"};
  9. int ID[50];
  10. char Gender [2] [50] = {"Male", "Female"} ;
  11. char *stars="********================*********";
  12. char *lines="---------------------------------";
  13. char *empty=" ";
  14. int txt_inserted;
  15.  
  16. /*Functions*/
  17.  
  18. void modify_record();
  19. void add_record();
  20. void search_record();
  21. //void list_record();
  22. void delete_record();
  23. //void exit();
  24. void menu();
  25.  
  26. /* MAIN */
  27. main(){
  28. clrscr();
  29. menu();
  30. getch();
  31. return 0;
  32. }
  33.  
  34. /* FUNCTION: MENU */
  35. void menu(){
  36. int selection;
  37.  
  38. FILE *start;
  39. if((start = fopen("record.txt", "a+")) == NULL)
  40. printf(" File record.txt is not found! Previous saved employee records did not loaded\n\n");
  41. else {
  42. fscanf(start, "%i %*c %c %*c, &ID", &Name, &Gender, &Department);
  43. printf(" Values Uploaded: %i %*c %c %*c", &ID, &Name, &Gender, &Department);
  44. fclose(start);}
  45.  
  46. gotoxy(15,1);
  47. puts("\n -------* [Welcome To Net Communication Human Resource] *-------");
  48. printf("\t%s\n",stars);
  49. puts("\t#\t1.Staff Biodata Maintenance\t#");
  50. puts("\t#\t2.Search Employee Records via Emplyee ID\t#");
  51. puts("\t#\t3.Display all employee by department\t#");
  52. puts("\t#\t4.Employee Record deletion\t#");
  53. puts("\t#\t5.Exit\t#");
  54. printf("\t%s\n",stars);
  55. printf("\n Enter Your Selection: ");
  56. fflush(stdin);
  57. scanf("%d", &selection);
  58.  
  59. if (selection == 1) add_record();
  60. else if (selection == 2) search_record();
  61. //else if (selection == 3) list_record();
  62. else if (selection == 4) delete_record();
  63. //else if (selection == 5) exit();
  64. else{ printf(" Please only select from the above menu"); getch(); clrscr(); menu();}
  65. }
  66.  
  67. /*Function 1a*/
  68.  
  69. void modify_record(){
  70. char Name[50];
  71. int Target, ID, selection, Found=0;
  72. FILE *rec, *temp;
  73.  
  74. temp=fopen("temp.txt", "w+");
  75. if((rec=fopen("record.txt", "r+")) == NULL)
  76. printf("\nerror: file not found.\n");
  77. else {
  78. printf(" ENTER the employee ID to be modified: ");
  79. scanf("%d", &Target);
  80. while(!feof(rec)){
  81. fscanf(rec, "%i %*c %c %*c", &ID, &Name, &Gender, &Department);
  82. if(feof(rec))
  83. break;
  84. else if(Target==ID)
  85. fprintf(temp, "%i %*c %c %*c", ID, Name, Gender, Department);
  86.  
  87.  
  88. else
  89. {
  90. Found=1;
  91. printf("\n Record Found! Please re-enter details for the employee ::%s::", Name);
  92.  
  93. /*Modify employee details*/
  94.  
  95.  
  96. gotoxy(1,15);
  97. printf("\n Employee details edit ");
  98. do
  99. {
  100. gotoxy(1, 22);
  101. printf(" Name : ");
  102. gotoxy(10, 22);
  103. fflush(stdin);
  104. scanf("%s",&Name);
  105. printf(" Gender : ");
  106. fflush(stdin);
  107. scanf("%s",&Gender);
  108. printf("Please enter the department for this employee, [0] Administration [1] Management [2] Accounting [3] Others", empty, empty, empty, empty);
  109. fflush(stdin);
  110. scanf("%d",&selection);
  111. if(selection==0 ){
  112. printf("\n Your selection is %s\n\n%s%s\n ", Department[selection], empty, empty);
  113. fprintf(temp, "%i %*c %c %*c", ID, Name, Gender, Department[selection]);
  114. }
  115. else if(selection==1){
  116. printf("\n Your selection is %s\n\n%s%s\n ", Department[selection], empty, empty);
  117. fprintf(temp, "%i %*c %c %*c", ID, Name, Gender, Department[selection]);
  118. }
  119. else if(selection==2){
  120. printf("\n Your selection is %s\n\n%s%s\n ", Department[selection], empty, empty);
  121. fprintf(temp, "%i %*c %c %*c", ID, Name, Gender, Department[selection]);
  122. }
  123. else if(selection==3){
  124. printf("\n Your selection is %s\n\n%s%s\n ", Department[selection], empty, empty);
  125. fprintf(temp, "%i %*c %c %*c", ID, Name, Gender, Department[selection]);
  126. }
  127. else
  128. printf("\n\n INVALID CHOICE! PLEASE Re-Enter.\a");
  129. } while(selection < 0 || selection > 3);
  130. }
  131.  
  132. }
  133. if(!Found)
  134. printf("\n RECORD CANNOT BE FOUND!\n");
  135. add_record();
  136. }
  137. fclose(rec);
  138. fclose(temp);
  139. remove("record.txt");
  140. rename("temp.txt", "record.txt");
  141. getch();
  142. clrscr();
  143. menu();
  144. }
  145.  
  146.  
  147.  
  148.  
  149. /*Function 1b adding employee*/
  150.  
  151. void add_record(){
  152. int ID;
  153. int selection;
  154. FILE *rec, *temp;
  155.  
  156. if ((rec=fopen("record.txt","a+"))==NULL)
  157. ID = 1;
  158. else
  159. {
  160. do{
  161. fscanf(rec, "%i %*c %c %*c", &ID, &Name, &Gender, &Department);
  162. }while(!feof(rec));
  163. ID += 1;
  164. }
  165.  
  166. do {
  167. gotoxy(2, 15);
  168. 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]);
  169. fflush(stdin);
  170. scanf("%d", &selection);
  171. if((selection != 0) && (selection != 1) && (selection !=2) && (selection !=3)) {gotoxy(1, 24); printf(" >>invalid Department<< Retry!\a");}
  172. } while((selection != 0) && (selection != 1) && (selection !=2) && (selection !=3));
  173.  
  174.  
  175. clrscr();
  176. printf("The new ID for this employee: %04i", ID);
  177. printf("\n Employee Name : \n");
  178. fflush(stdin);
  179. fgets(Name, 49, stdin);
  180. printf("\n Employee Gender: \n");
  181. scanf("%c", &Gender);
  182. printf("\nThe New Employee Record have successfully Saved!");
  183. fprintf(rec, "%i %[^/]%*c %c %c", ID, Name, Gender, Department);
  184. printf("\n Employee Status Stored!");
  185. fclose(rec);
  186.  
  187. }

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.
Last edited by alc6379; Oct 26th, 2004 at 7:39 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kimimaro is offline Offline
11 posts
since Oct 2004
Oct 26th, 2004
0

Re: I am a girl doing my 1st simple program

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 26th, 2004
0

Re: I am a girl doing my 1st simple program

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kimimaro is offline Offline
11 posts
since Oct 2004
Oct 26th, 2004
0

Re: I am a girl doing my 1st simple program

Are you properly indexing the array?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 26th, 2004
0

Re: I am a girl doing my 1st simple program

Quote originally posted by epsos ...
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
Quote originally posted by epsos ...
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...
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Oct 26th, 2004
0

Re: I am a girl doing my 1st simple program

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: need help with Delete function
Next Thread in C Forum Timeline: Help with !feof





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC