By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,075 Members | 998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,075 IT Pros & Developers. It's quick & easy.

Need help in converting C++ to C

P: 10
hi i am kind of new to programming, i have managed to create a simple payroll database in C++, but now i am trying to convert in into C but i am keep getting error, can anyone help me convert this code to C. I really need this so thatr i can learn my mistakes in C. Below is the C++ code that i have have developed.
Thank you,
Expand|Select|Wrap|Line Numbers
  1. #include<fstream>
  2. #include<string>
  3. #include<iostream>
  4. #include<iomanip>
  5. using namespace std;
  6. struct employee{ string fname, lname;
  7. double hourlyrate, hoursworked, netpay; }; //employee
  8. employee e[100];
  9. int n=0;
  10. void load(){
  11. ifstream fin("employee.txt",ios::in);
  12. while(fin>>e[n].fname>>e[n].lname>>e[n].hourlyrate>>e[n].hoursworked){
  13. e[n].netpay=e[n].hourlyrate*e[n].hoursworked;
  14. n++;}//WHILE
  15. }//LOAD
  16. void store(){
  17. ofstream fout("employee.txt",ios::out);
  18. for(int i=0; i<n; i++){
  19. if(e[i].fname!=" ")
  20. fout<<e[i].fname<<" "<<e[i].lname<<" "<<e[i].hourlyrate<<" "<<e[i].hoursworked<<endl;
  21. }//FOR
  22. }//STORE
  23.  
  24. void insert(){
  25. cout<<"Enter employee's first name: "; cin>>e[n].fname;
  26. cout<<"Enter employee's last name: "; cin>>e[n].lname;
  27. cout<<"Enter employee's salary per hour: "; cin>>e[n].hourlyrate;
  28. if(cin.fail())
  29. {cout<<"Error\n"; exit(1);}
  30. cout<<"Enter employee's hours worked: ";
  31. cin>>e[n].hoursworked;
  32. e[n].netpay=e[n].hourlyrate*e[n].hoursworked;
  33. n++; }//INSERTS THE RECORD TO ARRAY
  34.  
  35. void display(){
  36. cout<<setiosflags(ios::left)<<setw(15)<<"First Name"<<setw(15)<<"Last Name";
  37. cout<<setw(10)<<"Hourly Rate"<<setw(10)<<"HrsWorked"<<setw(10)<<"Net Pay";
  38. cout<<endl<<endl;
  39. cout<<setiosflags(ios::fixed|ios::showpoint|ios::left);
  40. for(int i=0;i<n;i++){
  41. if(e[i].fname!=" "){ 
  42. cout<<setiosflags(ios::left)<<setw(15)<<e[i].fname<<setw(15)<<e[i].lname;
  43. cout<<setprecision(2)<<setiosflags(ios::left)<<setw(10)<<e[i].hourlyrate;
  44. cout<<setw(10)<<e[i].hoursworked<<setw(10)<<e[i].netpay;
  45. cout<<resetiosflags(ios::left)<<endl;}//IF
  46. }//FOR
  47. }//THIS FUNCTION DISPLAYS ALL RECORDS IN ARRAY
  48.  
  49. int search(string s){
  50. for(int i=0;i<n;i++)if(s==e[i].lname)return i;//FOR
  51. return -1;
  52. }//SEARCH BY NAME,-1 NOT FOUND
  53.  
  54. void update(){
  55. string searchn;
  56. cout<<"Enter the Last Name of the Employee to be modified: "; cin>>searchn;
  57. int i=search(searchn); 
  58. if(i==-1) cout<<"EMPLOYEE NOT LISTED"<<endl;
  59. else{
  60. cout<<endl<<"Please enter the updated information."<<endl;
  61. cout<<"Enter employee's first name: "; cin>>e[i].fname;
  62. cout<<"Enter employee's last name: "; cin>>e[i].lname;
  63. cout<<"Enter employee's rate per hour: "; cin>>e[i].hourlyrate;
  64. cout<<"Enter employee's hours worked: "; cin>>e[i].hoursworked;
  65. e[i].netpay=(e[i].hourlyrate*e[i].hoursworked);}
  66. }//UPDATE
  67.  
  68. void deleterec(){
  69. string searchn;
  70. cout<<"Enter the Last Name of the Employee to delete: "; cin>>searchn;
  71. int k=-1;
  72. for(int j=0; j<n;j++){
  73. if(searchn==e[j].lname)
  74. k=j;}
  75. if(k!=-1)
  76. { cout<<" Name: "<<e[k].fname<<" "<<e[k].lname<<endl;
  77. cout<<" Salary: "<<e[k].hourlyrate<<endl;
  78. cout<<" Hrs Worked: "<<e[k].hoursworked<<endl;
  79. cout<<" Net Pay: "<<e[k].netpay<<endl<<endl;}
  80.  
  81. char ans;
  82. cout<<endl<<"Do you want to delete this record? (Y/N): ";
  83. cin>>ans;
  84. if(ans=='Y'||ans=='y'){
  85. e[k].fname=" ";}//IF
  86. else cout<<"EMPLOYEE NOT FOUND"; }//DELETE
  87.  
  88. void report(){
  89. int i=0;
  90. double totalhrs=0.00, totalpay=0.00;
  91. for(i=0;i<n;i++){
  92. totalhrs+=e[i].hoursworked;
  93. totalpay+=e[i].netpay; }//FOR
  94. cout<<"Total number of employees: " <<n<<endl<<endl;
  95. cout<<"Total hours worked by all employees: "<<totalhrs<<endl<<endl;
  96. cout<<"Total pay of all employees: "<<totalpay<<endl<<endl; }//REPORT
  97.  
  98. void deletefile(){
  99. int pword;
  100. cout<<"Enter your administrative password: "; cin>>pword;
  101. if(pword==0000){
  102. ofstream record("employee.txt", ios::out);
  103. record<<endl;
  104. load();
  105. record.close();}
  106. else cout<<"Wrong password entered."<<endl; }//DELETE FILE
  107.  
  108. void backupfile(){
  109. system("copy employee.txt backup.txt");
  110. cout<<endl<<"Back-up file has been made."<<endl;
  111. }//BACKUPFILE use cp command for UNIX
  112.  
  113. void main(){
  114. load();
  115. char choice='0';
  116. while (choice!='e'&&choice!='E'){
  117. cout<<endl<<"Payroll Database"<<endl;
  118. cout<<"1. Insert Employee Record and Compute Net Pay"<<endl;
  119. cout<<"2. Display All Records"<<endl;
  120. cout<<"3. Search Database"<<endl;
  121. cout<<"4. Update Record"<<endl;
  122. cout<<"5. Delete Record"<<endl;
  123. cout<<"6. Generate Report"<<endl;
  124. cout<<"7. Delete Database File"<<endl;
  125. cout<<"8. Back-up Database File"<<endl;
  126. cout<<endl<<" Type e to exit"<<endl<<endl;
  127. cout<<"Enter the number of your choice:";
  128. cin>>choice; cout<<endl;
  129. switch (choice){
  130. case '1': insert(); break;
  131. case '2': display(); break;
  132. case '3': char s[100]; int index;
  133. cout<<"Enter the last name of the person you want: ";
  134. cin>>s;
  135. index=search(s);
  136. if(index!=-1) {
  137. cout<<"Name: "<<e[index].fname<<" "<<e[index].lname<<endl;
  138. cout<<"Hourly Rate: "<<e[index].hourlyrate<<endl;
  139. cout<<"Hrs Worked: "<<e[index].hoursworked<<endl;
  140. cout<<"Net Pay: "<<e[index].netpay<<endl<<endl;}//IF
  141. else cout<<"EMPLOYEE NOT FOUND"<<endl;
  142. break;
  143. case '4': update(); break;
  144. case '5': deleterec(); break;
  145. case '6': report(); break;
  146. case '7': deletefile(); break;
  147. case '8': backupfile(); break;
  148. case 'e': store(); break; //EXIT
  149. default: cout<<"Invalid Choice"<<endl; break; }//SWITCH
  150. }//WHILE
  151. }//MAIN
Sep 25 '07 #1
Share this Question
Share on Google+
14 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Well, you're going to have a few issues, but those will mostly deal with I/O. I'd recommend going through and replacing all the couts first. All your control structures will be the same, so I'm pretty sure you'll just need to do the cin/couts and the file IO. There might be a few other things that I'm missing on the spot-check, but those should get you down to a level where the errors in compilation will show you any of the others.
Sep 25 '07 #2

P: 10
hi i tried to change the cout function to printf it can work perfectly, but when i use any stdin function's like scanf, i keep getting errors. I am also having trouble changing the stream functions. i have tried online tutorial's for the past 1 week but i am stuck in the scanf, and stream file functions. Please help me. Thank you
Sep 27 '07 #3

sicarie
Expert Mod 2.5K+
P: 4,677
hi i tried to change the cout function to printf it can work perfectly, but when i use any stdin function's like scanf, i keep getting errors. I am also having trouble changing the stream functions. i have tried online tutorial's for the past 1 week but i am stuck in the scanf, and stream file functions. Please help me. Thank you
Have you changed the libraries that handle those things? C and C++ use different headers...
Sep 27 '07 #4

P: 10
Have you changed the libraries that handle those things? C and C++ use different headers...
I have changed the the header file to <stdio.h>, actually now my scanf function is working but i am not sure how to change some part of the coding to C language especially the file processing section. Below is my coding that i have have modify. Can you help in how to change the file processing/database part.
I am also having trouble with the switch case, after i have changed it to printf statement the case"2" is not working, but i think this is because i have not yet finish changing the whole coding. Am I right about this or is there anything when wrong when i convert the switch statement?

Thank you for your help

[
Expand|Select|Wrap|Line Numbers
  1. #include<fstream>
  2. #include<string>
  3. #include<iostream>
  4. #include<iomanip>
  5. #include<stdio.h>
  6. using namespace std;
  7. struct employee{   string fname, lname;
  8.          double hourlyrate, hoursworked, netpay; 
  9. }emp; //employee
  10. employee e[100];
  11. int n=0;
  12. void load(){
  13.     ifstream fin("employee.txt",ios::in);
  14.     while(fin>>e[n].fname>>e[n].lname>>e[n].hourlyrate>>e[n].hoursworked){
  15.     e[n].netpay=e[n].hourlyrate*e[n].hoursworked;
  16.     n++;}//WHILE
  17.     }//LOAD
  18. void store(){
  19.     ofstream fout("employee.txt",ios::out);
  20.     for(int i=0; i<n; i++){
  21.     if(e[i].fname!=" ")
  22.     fout<<e[i].fname<<" "<<e[i].lname<<" "<<e[i].hourlyrate<<" "<<e[i].hoursworked<<endl;
  23. }//FOR
  24.     }//STORE
  25.  
  26. void insert(){
  27.      printf("Enter employee's first name: ");
  28.      scanf("s",& emp.fname);
  29.      printf("Enter employee's last name: ");    
  30.     scanf("s",& emp.lname);
  31.      cout<<"Enter employee's salary per hour: ";     cin>>e[n].hourlyrate;
  32.      if(cin.fail())
  33.           {cout<<"Error\n"; exit(1);}
  34.      cout<<"Enter employee's hours worked: ";
  35.      cin>>e[n].hoursworked;
  36.     e[n].netpay=e[n].hourlyrate*e[n].hoursworked;
  37.     n++;    }//INSERTS THE RECORD TO ARRAY
  38. void display(){
  39.  
  40.     cout<<setiosflags(ios::left)<<setw(15)<<"First Name"<<setw(15)<<"Last Name";
  41.     cout<<setw(10)<<"Hourly Rate"<<setw(10)<<"HrsWorked"<<setw(10)<<"Net Pay";
  42.      cout<<endl<<endl;
  43.      cout<<setiosflags(ios::fixed|ios::showpoint|ios::left);
  44. for(int i=0;i<n;i++){
  45.      if(e[i].fname!=" "){        
  46. cout<<setiosflags(ios::left)<<setw(15)<<e[i].fname<<setw(15)<<e[i].lname;
  47. cout<<setprecision(2)<<setiosflags(ios::left)<<setw(10)<<e[i].hourlyrate;
  48.      cout<<setw(10)<<e[i].hoursworked<<setw(10)<<e[i].netpay;
  49.      cout<<resetiosflags(ios::left)<<endl;}//IF
  50.     }//FOR
  51. }//THIS FUNCTION DISPLAYS ALL RECORDS IN ARRAY
  52.  
  53. int search(string s){
  54.     for(int i=0;i<n;i++)if(s==e[i].lname)return i;//FOR
  55.      return -1;
  56.     }//SEARCH BY NAME,-1 NOT FOUND
  57. void update(){
  58.      string searchn;
  59.      cout<<"Enter the Last Name of the Employee to be modified: ";     cin>>searchn;
  60.     int i=search(searchn); 
  61.     if(i==-1) cout<<"EMPLOYEE NOT LISTED"<<endl;
  62.     else{
  63.      cout<<endl<<"Please enter the updated information."<<endl;
  64.      cout<<"Enter employee's first name: ";     cin>>e[i].fname;
  65.      cout<<"Enter employee's last name: ";     cin>>e[i].lname;
  66.      cout<<"Enter employee's rate per hour: ";      cin>>e[i].hourlyrate;
  67.      cout<<"Enter employee's hours worked: ";       cin>>e[i].hoursworked;
  68.      e[i].netpay=(e[i].hourlyrate*e[i].hoursworked);}
  69.     }//UPDATE
  70.  
  71. void deleterec(){
  72.  string searchn;
  73.  cout<<"Enter the Last Name of the Employee to delete: ";  cin>>searchn;
  74.  int k=-1;
  75.  for(int j=0; j<n;j++){
  76.   if(searchn==e[j].lname)
  77.      k=j;}
  78.  if(k!=-1)
  79.  { cout<<" Name: "<<e[k].fname<<" "<<e[k].lname<<endl;
  80.    cout<<" Salary: "<<e[k].hourlyrate<<endl;
  81.    cout<<" Hrs Worked: "<<e[k].hoursworked<<endl;
  82.    cout<<" Net Pay: "<<e[k].netpay<<endl<<endl;}
  83.      
  84.  char ans;
  85.  cout<<endl<<"Do you want to delete this record? (Y/N): ";
  86.  cin>>ans;
  87.  if(ans=='Y'||ans=='y'){
  88.     e[k].fname=" ";}//IF
  89. else cout<<"EMPLOYEE NOT FOUND";     }//DELETE
  90.  
  91. void report(){
  92.  int i=0;
  93.  double totalhrs=0.00, totalpay=0.00;
  94.  for(i=0;i<n;i++){
  95.  totalhrs+=e[i].hoursworked;
  96.  totalpay+=e[i].netpay; }//FOR
  97.  cout<<"Total number of employees: " <<n<<endl<<endl;
  98.  cout<<"Total hours worked by all employees: "<<totalhrs<<endl<<endl;
  99.  cout<<"Total pay of all employees: "<<totalpay<<endl<<endl;   }//REPORT
  100.  
  101. void deletefile(){
  102.  int pword;
  103.  cout<<"Enter your administrative password: ";  cin>>pword;
  104.  if(pword==5438){
  105.  ofstream record("employee.txt", ios::out);
  106.  record<<endl;
  107.  load();
  108.  record.close();}
  109.  else cout<<"Wrong password entered."<<endl; }//DELETE FILE
  110.  
  111. void backupfile(){
  112.   system("copy employee.txt backup.txt");
  113.   cout<<endl<<"Back-up file has been made."<<endl;
  114. }
  115.  
  116. void main(){
  117.     load();
  118.     char choice='0';
  119.     while (choice!='e'&&choice!='E'){
  120.     printf("\nPayroll Database\n\n");
  121.     printf("1. Insert Employee Record and Compute Net Pay\n");
  122.     printf("2. Display All Records\n");
  123.     printf("3. Search Database\n");
  124.     printf("4. Update Record\n");
  125.     printf("5. Delete Record\n");
  126.     printf("6. Generate Report\n");
  127.     printf("7. Delete Database File\n");
  128.     printf("8. Back-up Database File\n");
  129.     printf("\nType e to exit\n\n");
  130.     printf("Enter the number of your choice:\n");
  131.     scanf("%c",&choice);
  132.     switch (choice){
  133.  
  134.        case '1': insert(); break;
  135.      case '2': display(); break;
  136.      case '3': char s[100]; int index;
  137.           cout<<"Enter the last name of the person you want: ";
  138.           cin>>s;
  139.           index=search(s);
  140.         if(index!=-1) {
  141.         cout<<"Name: "<<e[index].fname<<" "<<e[index].lname<<endl;
  142.         cout<<"Hourly Rate: "<<e[index].hourlyrate<<endl;
  143.                 cout<<"Hrs Worked: "<<e[index].hoursworked<<endl;
  144.                 cout<<"Net Pay: "<<e[index].netpay<<endl<<endl;}//IF
  145.         else cout<<"EMPLOYEE NOT FOUND"<<endl;
  146.           break;
  147.      case '4': update(); break;
  148.      case '5': deleterec(); break;
  149.      case '6': report(); break;
  150.      case '7': deletefile(); break;
  151.      case '8': backupfile(); break;
  152.      case 'e': store(); break; //EXIT
  153.      default: cout<<"Invalid Choice"<<endl; break; }//SWITCH
  154. }//WHILE
  155. }//MAIN
  156.  

The bold section's are the part i am having problem to convert to C language. Sorry for the trouble.
Sep 29 '07 #5

RRick
Expert 100+
P: 463
There's another way to convert the C++ code to C and you don't have to get rid of the C++ objects. You do this by using the extern C declaration. This will allow the function signature to look like C, and be called like a C function, but the function internals are still C++. Try this link http://www.parashift.com/c++-faq-lit....html#faq-32.1 and refer to section 32.6.

There are a couple of caveats

  • All the function parameters must be C style variables. No C++ objects (like string) are permitted.
  • When you link your C program with these routines, you will have to explictly add the C++ libraries. One way around this problem is to use the C++ compiler to compile your C program. The C++ compiler knows where the C and C++ libraries reside and the C++ compiler is a "better" C compiler.
Sep 29 '07 #6

P: 10
I am sorry, i am trying to convert this c++ coding to c without mixing both the language, please help with some idea in the bolded section's in my coding.
Sep 30 '07 #7

RRick
Expert 100+
P: 463
Most of your issues are with converting C++ print and file mechanisms to C.

Instead of opening a file with ofstream use fopen. Its parameters are almost the same as ofstream. Use "r" and "w" instead ios::in and out.

C style printf and fprintf are going to become very good friends. Their format is different from <<, but they have very good formating capabilities. Instead of setw, you can use "%15s" for printing a string with a width of 15, and "%-15s" reverses the justification. A google search will find the various options for (f)printf.
Sep 30 '07 #8

P: 10
Can anyone give show me a example how to use my fopen in this part of my coding:

Expand|Select|Wrap|Line Numbers
  1. fout<<e[i].fname<<" "<<e[i].lname<<" "<<e[i].hourlyrate<<" "<<e[i].hoursworked<<endl;
How can i this part i am confused with the "::" parts:

Expand|Select|Wrap|Line Numbers
  1. cout<<setiosflags(ios::left)<<setw(15)<<"First Name"<<setw(15)<<"Last Name";
  2.     cout<<setw(10)<<"Hourly Rate"<<setw(10)<<"HrsWorked"<<setw(10)<<"Net Pay";
  3.     cout<<endl<<endl;
  4.     cout<<setiosflags(ios::fixed|ios::showpoint|ios::left);
  5. for(int i=0;i<n;i++){
  6.     if(e[i].fname!=" "){      
  7. cout<<setiosflags(ios::left)<<setw(15)<<e[i].fname<<setw(15)<<e[i].lname;
  8. cout<<setprecision(2)<<setiosflags(ios::left)<<setw(10)<<e[i].hourlyrate;
  9.     cout<<setw(10)<<e[i].hoursworked<<setw(10)<<e[i].netpay;
  10.     cout<<resetiosflags(ios::left)<<endl;}//IF
  11.     }//FOR
Oct 1 '07 #9

RRick
Expert 100+
P: 463
fout is used to print something, so you can't use fopen at that part of the program. Fopen is only used to "open" files, not print them.

Instead, you use fprintf. Your fout printing would translate into C as:
Expand|Select|Wrap|Line Numbers
  1. //  C++ print
  2. fout<<e[i].fname<<" "<<e[i].lname<<" "<<e[i].hourlyrate<<" "<<e[i].hoursworked<<endl;
  3.  
  4. /*  C print  */
  5. FILE * fhandle = fopen( ....);
  6. fprintf( fhandle, "%s %s %d %d\n", e[i].fname, e[i].lname, e[i].hourlyrate, e[i].hoursworked);
  7.  
Oct 1 '07 #10

RRick
Expert 100+
P: 463
The above fprintf format statement assumes that the last two values are integers. If they are doubles, use "%lf" instead of "%d".
Oct 1 '07 #11

P: 10
Hi I am sorry for not updating my progress for the past 2 weeks, i have tried all those ideas given but i am having errors (almost 300), please someone help me translate the whole coding for me, i am feel very bad for requesting this, but i cant do it its really hard for me as i am still new, please someone help me i will need this to as a reference for my studies.
Oct 16 '07 #12

sicarie
Expert Mod 2.5K+
P: 4,677
Sorry dude, nobody here can (see our Posting Guidelines) or will (because it's your assignment) do your work for you. If you post the errors you are having trouble with, we can point you in the right direction. Or if you post a generic error, we can show you how to break it down (usually there is a line number around which the issue is - or something on that line caused the issue because it was instantiated/modified improperly, etc..), but we won't do it for you.

Also, cutting and pasting errors into Google usually will return someone who has had the same issue as you...
Oct 17 '07 #13

P: 10
I have develop a new coding using C, with some problem where the search and update functions are working but when I enter a record which is not available it getting me to another record, my delete module also is notworking properly, and how to make my time tick. Below is my coding, thankyou for any help.

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<conio.h>
  4. #include<ctype.h>
  5. #include<string.h>
  6. #include <time.h>
  7. void ins(void);
  8. void oldf(void);
  9. void newf(void);
  10. void add(void);
  11. void search(void);
  12. void upd(void);
  13. void del(void);
  14. struct    payroll
  15.     { char no[5];
  16.       char name[25];
  17.       double salary;
  18.       double rate;
  19.       double net;
  20.       } payroll_rec;
  21.  
  22. void main()
  23. {
  24. char i;
  25. time_t now;
  26. time(&now);
  27.  
  28.  
  29. do
  30.   {
  31.     system("cls");
  32.  
  33.     printf("\n\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\7");
  34.     printf("\n\n Welcome to Optimum Technologies' Payroll Department\t\t\t\t\t  %.24s.\n",ctime(&now));
  35.     printf("\t\t   Main Menu\n\n");
  36.     printf("\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\n");
  37.     printf("\nWhat do you wish to do?\n");
  38.     printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n\n");
  39.     printf("1. Insert Employee Record And Compute Weekly Pay\n");
  40.     printf("2. Display All Records\n");
  41.     printf("3. Compute Monthly Pay Slip\n");
  42.     printf("4. Add Employee Record\n");
  43.     printf("5. Search and View Record\n");
  44.     printf("6. Update Record\n");
  45.     printf("7. Delete Record\n");
  46.     printf("8. Exit\n");
  47.     printf("\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
  48.     printf("You choose number :");
  49.     i=getche();
  50.  
  51.       switch(tolower(i))
  52.         {
  53.          case '1': ins();  break;
  54.          case '2': oldf();  break;
  55.          case '3': newf();  break;
  56.          case '4': add();  break;
  57.          case '5': search();  break;
  58.          case '6': upd();  break;
  59.          case '7': del();  break;
  60.          case '8': printf("\7\n\tPlease any key to exit...");
  61.                   getch();
  62.                   exit(0);
  63.          break;
  64.          default : printf("\n\tChoice error !  Please try again.");
  65.          getch();
  66.         }
  67.   }while(i!='7');
  68.  
  69. }
  70.  
  71. void ins(void)
  72. {
  73. FILE *fp;
  74. if((fp=fopen("payroll.txt","wb"))==NULL)
  75.    {
  76.     printf("Error in opening file");
  77.      exit(1);
  78.    }
  79. struct payroll
  80. {
  81.   char no[5];
  82.   char name[25];
  83.   double salary;
  84.   double rate;
  85.   double net;
  86.   } payroll_rec;
  87. char numstr[50];
  88.  system("cls");
  89.  printf("\n\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\7");
  90.  printf("\n Insert Employee Record And Compute Weekly Pay Module\n");
  91.  printf("\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3");
  92. do
  93.    {
  94.  
  95.      printf("\n\nEnter Employee No           : ");
  96.      gets(payroll_rec.no);
  97.      printf("Enter Employee Name         : ");
  98.      gets(payroll_rec.name);
  99.     printf("Enter Hours Worked Per Week : ");
  100.      payroll_rec.salary=atof(gets(numstr));
  101.      printf("Enter Hourly Wage           : ");
  102.      payroll_rec.rate=atof(gets(numstr));
  103.      payroll_rec.net=payroll_rec.salary*payroll_rec.rate;
  104.      printf("Your Weekly Pay is RM %.2f\n", payroll_rec.net);
  105.      fwrite(&payroll_rec,sizeof(payroll_rec),1,fp);
  106.      printf("Add another person(y/n)? :");
  107.    }
  108.    while(tolower(getche())=='y');
  109. fclose(fp);
  110. printf("\nPress any key to return to main menu");
  111. getch();
  112. }
  113.  
  114. void oldf(void)
  115. {
  116. FILE *fp;
  117. if((fp=fopen("payroll.txt","rb"))==NULL)
  118.     {
  119.      printf("Error in open file");
  120.      printf("\007");
  121.      getch();  exit(1);
  122.          } system("cls");
  123.  printf("\n \5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\7");
  124.  printf("\n\t\t\t Display Record Module\n ");
  125.  printf("\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\n\n");
  126.    printf("\n\n**********************************************************************\n");
  127.    printf(" Employee ID  Employee Name     Hours Worked   Hourly Wage   Weekly Pay\n");
  128.    printf("***********************************************************************\n");
  129.   while(fread(&payroll_rec,sizeof(payroll_rec),1,fp)==1)
  130.   {
  131.       printf(" %s\t\t%s\t\t%.2f\t%.2f\t\t%.2f\t\t\n",payroll_rec.no,payroll_rec.name,payroll_rec.salary,payroll_rec.rate,payroll_rec.net);
  132.  
  133.     }
  134. fclose(fp);
  135.  printf("Press any key to return to main menu\n");
  136. getch();
  137. }
  138.  
  139. void newf(void)
  140. {
  141. double net;
  142. double total;
  143. FILE *fp;
  144. if((fp=fopen("payroll.txt","rb"))==NULL)
  145.     {
  146.      printf("Error in open file");
  147.      printf("\007");
  148.      getch();  exit(1);
  149.     } system("cls");
  150.  printf("\n\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\7");
  151.  printf("\n\t\t   Compute Monthly Pay Slip Module\n");
  152.  printf("\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\n\n");
  153. while(fread(&payroll_rec,sizeof(payroll_rec),1,fp)==1)
  154.     {
  155.  
  156.     net=payroll_rec.salary*payroll_rec.rate*4;
  157.     total=payroll_rec.salary*4;
  158.     printf("\n\n||======================================================================||\n");
  159.     printf("||\t\t\tOptimum Technologies Pay Vaucher                ||\n");
  160.     printf("||                                                                      ||\n");
  161.     printf("||Employee Name : %s\t\t\t\t\t\t||\n",payroll_rec.name);
  162.     printf("||Empolyee ID   : %s\t\t\t\t\t\t\t||\n",payroll_rec.no);
  163.     printf("||Hours Worked  : %.2f\t\t\t\t\t\t\t||\n",total);
  164.     printf("||Hourly Wage   : RM %.2f\t\t\t\t\t\t\t||",payroll_rec.rate);
  165.     printf("||Net Salary    : RM %.2f\t\t\t\t\t\t||\n",net);
  166.     printf("||                                                                      ||\n");
  167.     printf("||This is a digitally signed voucher it does not require signature\t||\n");
  168.     printf("||                                                                      ||\n");
  169.     printf("||======================================================================||\n");
  170.   }
  171.   fclose(fp);
  172.   printf("Press any key to return to main menu");
  173.   getch();
  174. }
  175.  
  176. void add(void)
  177. {
  178. FILE *fp;
  179. if((fp=fopen("payroll.txt","ab"))==NULL)
  180.     {
  181.      printf("Error in open file");
  182.      printf("\007");
  183.      getch();  exit(1);
  184.     }
  185. char str[10];
  186.  system("cls");
  187.  printf("\n \5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\7");
  188.  printf("\n\t\t Add Record Module\n ");
  189.  printf("\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\n\n");
  190.   do{
  191.       printf("\nEnter Employee No :");
  192.       gets(payroll_rec.no);
  193.       printf("Enter Employee Name :");
  194.       gets(payroll_rec.name);
  195.       printf("Enter Hours Worked Per Week :");
  196.       payroll_rec.salary=atof(gets(str));
  197.       printf("Enter Hourly Wage:");
  198.       payroll_rec.rate=atof(gets(str));
  199.       payroll_rec.net=payroll_rec.salary*payroll_rec.rate;
  200.       printf("Your Weekly Pay is RM %.2f\n", payroll_rec.net);
  201.       fwrite(&payroll_rec,sizeof(payroll_rec),1,fp);
  202.       printf("Add another person(y/n)? :");
  203.      } while(tolower(getche())=='y');
  204. fclose(fp);
  205.  
  206. }
  207.  
  208.  
  209. void search(void)
  210. {
  211. FILE *fp;
  212. int i=0;
  213. int rec;
  214. char ch;
  215. int offset;
  216. system("cls");
  217. printf("\n\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\7");
  218.  printf("\n\t\t   Serach and View Record Module\n");
  219.  printf("\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\n\n");
  220.  
  221.   if((fp=fopen("payroll.txt","rb"))==NULL)
  222.     {
  223.      printf("\nError in open file");
  224.      printf("\007");
  225.      getch(); 
  226.      exit(1);
  227.     }
  228.  do
  229. {
  230.     printf("\n\nEnter record number to search :");
  231.     scanf("%d",&rec);  ch=getchar();
  232.     offset=(rec-1)*sizeof(payroll_rec);
  233.      if(fseek(fp,offset,0)!=0)
  234.      {
  235.       printf("Can't not find.\n");
  236.       getch(); 
  237.      exit(1);
  238.  
  239.      }
  240.      fread(&payroll_rec,sizeof(payroll_rec),1,fp);
  241.       printf("\nEmployee ID   : %s\n",payroll_rec.no);
  242.       printf("Employee Name : %s\n",payroll_rec.name);
  243.       printf("Hours Worked  : %.2f\n",payroll_rec.salary);
  244.       printf("Hourly Wage   : %.2f\n",payroll_rec.rate);
  245.       printf("Weekly Salary : %.2f\n",payroll_rec.net);
  246.       printf("Do you wish to continue(y/n)? :");
  247.  
  248.  
  249. }
  250.    while(tolower(getche())=='y');
  251. fclose(fp);
  252. printf("\nPress any key to return to main menu");
  253. getch();
  254. }    
  255.  
  256.  
  257. void upd(void)
  258. {
  259. FILE *fp;
  260. int i=0;
  261. int rec;
  262. char ch,str[20];
  263. char id[5];
  264. int offset;
  265. system("cls");
  266. printf("\n\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\7");
  267.  printf("\n\t\t   Update Record Module\n");
  268.  printf("\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\n\n");
  269.  
  270.   if((fp=fopen("payroll.txt","rb"))==NULL)
  271.     {
  272.      printf("\nError in open file");
  273.      printf("\007");
  274.      getch();  exit(1);
  275.     }
  276.  
  277.     printf("\nEnter record number :");
  278.     scanf("%d",&rec);  ch=getchar();
  279.     offset=(rec-1)*sizeof(payroll_rec);
  280.      if(fseek(fp,offset,0)!=0)
  281.      {
  282.       printf("Can't not find.\n");
  283.       getch();
  284.       exit(1);
  285.      }
  286.      fread(&payroll_rec,sizeof(payroll_rec),1,fp);
  287.     strcpy(id,payroll_rec.no);
  288.      getch();
  289.   fp=fopen("payroll.txt","r+b");
  290.   offset=(rec-1)*sizeof(payroll_rec);
  291.     if(fseek(fp,offset,0)!=0)
  292.     {
  293.      printf("Can't not find.\n");
  294.      getch();
  295.      exit(1);
  296.     }
  297.       printf("\nEnter Employee ID :%s\n",id);
  298.       strcpy(payroll_rec.no,id);
  299.      printf("Enter Employee Name :");
  300.      gets(payroll_rec.name);
  301.       printf("Enter Hours Worked :");
  302.       payroll_rec.salary=atof(gets(str));
  303.       printf("Enter Hourly Wage:");
  304.       payroll_rec.rate=atof(gets(str));
  305.       payroll_rec.net=payroll_rec.salary*payroll_rec.rate;
  306.       fwrite(&payroll_rec,sizeof(payroll_rec),1,fp);
  307.  
  308. fclose(fp);
  309. printf("Press any key to return to main menu");
  310. }
  311.  
  312. void del(void)
  313. {
  314. FILE*fp;
  315. int i=0;
  316. char ans;
  317. char rec[7];
  318. system("cls");
  319. printf("\n \5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\7");
  320. printf("\n\t\t Delete Record Module\n ");
  321. printf("\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\n\n");
  322. if((fp=fopen("payroll.txt","rb+"))==NULL)
  323. {
  324. printf("Error in open file");
  325. printf("\007");
  326. getch();
  327. exit(1);
  328. }
  329. printf("\n\nThis is data for delete\n");
  330. do{
  331. while(1){
  332. fread(&payroll_rec,sizeof(payroll_rec),1,fp);
  333. if(ferror(fp)){
  334.      printf("\nError read of delete file");
  335.      getch();  exit(1); }
  336.      if(feof(fp))
  337.      break;
  338.      ++i;
  339.      printf("%d. %s\t\t",i,payroll_rec.no);
  340.      }
  341.      printf("\nDo you want to delete data(y or n) :");
  342.      ans=getche();
  343.      if(ans=='y') {
  344.      printf("\nEnter record number :");
  345.      gets(rec);
  346.      fseek(fp,((atoi(rec)-1)*sizeof(payroll_rec)),SEEK_SET);
  347.      strcpy(payroll_rec.no,"\0");
  348.      strcpy(payroll_rec.name,"\0");
  349.      payroll_rec.salary=NULL;
  350.      payroll_rec.rate=NULL;
  351.      payroll_rec.net=NULL;
  352.      fwrite(&payroll_rec,sizeof(payroll_rec),1,fp);
  353.      if(ferror(fp)) {
  354.         printf("Error for delete data\n");
  355.         getch();
  356.         exit(1);
  357.         }
  358.         printf("Delete data complete\n");
  359.         }
  360.         printf("\nContinue(press c) or Exit(press e) :");
  361.         ans=getche();
  362.         printf("\n");
  363.         }while(ans!='e');
  364.         fclose(fp);
Oct 19 '07 #14

P: 10
I have develop a new coding using C, with some problem where the search and update functions are working but when I enter a record which is not available it getting me to another record, my delete module also is notworking properly, and how to make my time tick. Below is my coding, thankyou for any help.

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<conio.h>
  4. #include<ctype.h>
  5. #include<string.h>
  6. #include <time.h>
  7. void ins(void);
  8. void oldf(void);
  9. void newf(void);
  10. void add(void);
  11. void search(void);
  12. void upd(void);
  13. void del(void);
  14. struct    payroll
  15.     { char no[5];
  16.       char name[25];
  17.       double salary;
  18.       double rate;
  19.       double net;
  20.       } payroll_rec;
  21.  
  22. void main()
  23. {
  24. char i;
  25. time_t now;
  26. time(&now);
  27.  
  28.  
  29. do
  30.   {
  31.     system("cls");
  32.  
  33.     printf("\n\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\7");
  34.     printf("\n\n Welcome to Optimum Technologies' Payroll Department\t\t\t\t\t  %.24s.\n",ctime(&now));
  35.     printf("\t\t   Main Menu\n\n");
  36.     printf("\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\n");
  37.     printf("\nWhat do you wish to do?\n");
  38.     printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n\n");
  39.     printf("1. Insert Employee Record And Compute Weekly Pay\n");
  40.     printf("2. Display All Records\n");
  41.     printf("3. Compute Monthly Pay Slip\n");
  42.     printf("4. Add Employee Record\n");
  43.     printf("5. Search and View Record\n");
  44.     printf("6. Update Record\n");
  45.     printf("7. Delete Record\n");
  46.     printf("8. Exit\n");
  47.     printf("\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
  48.     printf("You choose number :");
  49.     i=getche();
  50.  
  51.       switch(tolower(i))
  52.         {
  53.          case '1': ins();  break;
  54.          case '2': oldf();  break;
  55.          case '3': newf();  break;
  56.          case '4': add();  break;
  57.          case '5': search();  break;
  58.          case '6': upd();  break;
  59.          case '7': del();  break;
  60.          case '8': printf("\7\n\tPlease any key to exit...");
  61.                   getch();
  62.                   exit(0);
  63.          break;
  64.          default : printf("\n\tChoice error !  Please try again.");
  65.          getch();
  66.         }
  67.   }while(i!='7');
  68.  
  69. }
  70.  
  71. void ins(void)
  72. {
  73. FILE *fp;
  74. if((fp=fopen("payroll.txt","wb"))==NULL)
  75.    {
  76.     printf("Error in opening file");
  77.      exit(1);
  78.    }
  79. struct payroll
  80. {
  81.   char no[5];
  82.   char name[25];
  83.   double salary;
  84.   double rate;
  85.   double net;
  86.   } payroll_rec;
  87. char numstr[50];
  88.  system("cls");
  89.  printf("\n\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\7");
  90.  printf("\n Insert Employee Record And Compute Weekly Pay Module\n");
  91.  printf("\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3");
  92. do
  93.    {
  94.  
  95.      printf("\n\nEnter Employee No           : ");
  96.      gets(payroll_rec.no);
  97.      printf("Enter Employee Name         : ");
  98.      gets(payroll_rec.name);
  99.     printf("Enter Hours Worked Per Week : ");
  100.      payroll_rec.salary=atof(gets(numstr));
  101.      printf("Enter Hourly Wage           : ");
  102.      payroll_rec.rate=atof(gets(numstr));
  103.      payroll_rec.net=payroll_rec.salary*payroll_rec.rate;
  104.      printf("Your Weekly Pay is RM %.2f\n", payroll_rec.net);
  105.      fwrite(&payroll_rec,sizeof(payroll_rec),1,fp);
  106.      printf("Add another person(y/n)? :");
  107.    }
  108.    while(tolower(getche())=='y');
  109. fclose(fp);
  110. printf("\nPress any key to return to main menu");
  111. getch();
  112. }
  113.  
  114. void oldf(void)
  115. {
  116. FILE *fp;
  117. if((fp=fopen("payroll.txt","rb"))==NULL)
  118.     {
  119.      printf("Error in open file");
  120.      printf("\007");
  121.      getch();  exit(1);
  122.          } system("cls");
  123.  printf("\n \5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\7");
  124.  printf("\n\t\t\t Display Record Module\n ");
  125.  printf("\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\n\n");
  126.    printf("\n\n**********************************************************************\n");
  127.    printf(" Employee ID  Employee Name     Hours Worked   Hourly Wage   Weekly Pay\n");
  128.    printf("***********************************************************************\n");
  129.   while(fread(&payroll_rec,sizeof(payroll_rec),1,fp)==1)
  130.   {
  131.       printf(" %s\t\t%s\t\t%.2f\t%.2f\t\t%.2f\t\t\n",payroll_rec.no,payroll_rec.name,payroll_rec.salary,payroll_rec.rate,payroll_rec.net);
  132.  
  133.     }
  134. fclose(fp);
  135.  printf("Press any key to return to main menu\n");
  136. getch();
  137. }
  138.  
  139. void newf(void)
  140. {
  141. double net;
  142. double total;
  143. FILE *fp;
  144. if((fp=fopen("payroll.txt","rb"))==NULL)
  145.     {
  146.      printf("Error in open file");
  147.      printf("\007");
  148.      getch();  exit(1);
  149.     } system("cls");
  150.  printf("\n\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\7");
  151.  printf("\n\t\t   Compute Monthly Pay Slip Module\n");
  152.  printf("\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\n\n");
  153. while(fread(&payroll_rec,sizeof(payroll_rec),1,fp)==1)
  154.     {
  155.  
  156.     net=payroll_rec.salary*payroll_rec.rate*4;
  157.     total=payroll_rec.salary*4;
  158.     printf("\n\n||======================================================================||\n");
  159.     printf("||\t\t\tOptimum Technologies Pay Vaucher                ||\n");
  160.     printf("||                                                                      ||\n");
  161.     printf("||Employee Name : %s\t\t\t\t\t\t||\n",payroll_rec.name);
  162.     printf("||Empolyee ID   : %s\t\t\t\t\t\t\t||\n",payroll_rec.no);
  163.     printf("||Hours Worked  : %.2f\t\t\t\t\t\t\t||\n",total);
  164.     printf("||Hourly Wage   : RM %.2f\t\t\t\t\t\t\t||",payroll_rec.rate);
  165.     printf("||Net Salary    : RM %.2f\t\t\t\t\t\t||\n",net);
  166.     printf("||                                                                      ||\n");
  167.     printf("||This is a digitally signed voucher it does not require signature\t||\n");
  168.     printf("||                                                                      ||\n");
  169.     printf("||======================================================================||\n");
  170.   }
  171.   fclose(fp);
  172.   printf("Press any key to return to main menu");
  173.   getch();
  174. }
  175.  
  176. void add(void)
  177. {
  178. FILE *fp;
  179. if((fp=fopen("payroll.txt","ab"))==NULL)
  180.     {
  181.      printf("Error in open file");
  182.      printf("\007");
  183.      getch();  exit(1);
  184.     }
  185. char str[10];
  186.  system("cls");
  187.  printf("\n \5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\7");
  188.  printf("\n\t\t Add Record Module\n ");
  189.  printf("\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\n\n");
  190.   do{
  191.       printf("\nEnter Employee No :");
  192.       gets(payroll_rec.no);
  193.       printf("Enter Employee Name :");
  194.       gets(payroll_rec.name);
  195.       printf("Enter Hours Worked Per Week :");
  196.       payroll_rec.salary=atof(gets(str));
  197.       printf("Enter Hourly Wage:");
  198.       payroll_rec.rate=atof(gets(str));
  199.       payroll_rec.net=payroll_rec.salary*payroll_rec.rate;
  200.       printf("Your Weekly Pay is RM %.2f\n", payroll_rec.net);
  201.       fwrite(&payroll_rec,sizeof(payroll_rec),1,fp);
  202.       printf("Add another person(y/n)? :");
  203.      } while(tolower(getche())=='y');
  204. fclose(fp);
  205.  
  206. }
  207.  
  208.  
  209. void search(void)
  210. {
  211. FILE *fp;
  212. int i=0;
  213. int rec;
  214. char ch;
  215. int offset;
  216. system("cls");
  217. printf("\n\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\7");
  218.  printf("\n\t\t   Serach and View Record Module\n");
  219.  printf("\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\n\n");
  220.  
  221.   if((fp=fopen("payroll.txt","rb"))==NULL)
  222.     {
  223.      printf("\nError in open file");
  224.      printf("\007");
  225.      getch(); 
  226.      exit(1);
  227.     }
  228.  do
  229. {
  230.     printf("\n\nEnter record number to search :");
  231.     scanf("%d",&rec);  ch=getchar();
  232.     offset=(rec-1)*sizeof(payroll_rec);
  233.      if(fseek(fp,offset,0)!=0)
  234.      {
  235.       printf("Can't not find.\n");
  236.       getch(); 
  237.      exit(1);
  238.  
  239.      }
  240.      fread(&payroll_rec,sizeof(payroll_rec),1,fp);
  241.       printf("\nEmployee ID   : %s\n",payroll_rec.no);
  242.       printf("Employee Name : %s\n",payroll_rec.name);
  243.       printf("Hours Worked  : %.2f\n",payroll_rec.salary);
  244.       printf("Hourly Wage   : %.2f\n",payroll_rec.rate);
  245.       printf("Weekly Salary : %.2f\n",payroll_rec.net);
  246.       printf("Do you wish to continue(y/n)? :");
  247.  
  248.  
  249. }
  250.    while(tolower(getche())=='y');
  251. fclose(fp);
  252. printf("\nPress any key to return to main menu");
  253. getch();
  254. }    
  255.  
  256.  
  257. void upd(void)
  258. {
  259. FILE *fp;
  260. int i=0;
  261. int rec;
  262. char ch,str[20];
  263. char id[5];
  264. int offset;
  265. system("cls");
  266. printf("\n\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\7");
  267.  printf("\n\t\t   Update Record Module\n");
  268.  printf("\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\n\n");
  269.  
  270.   if((fp=fopen("payroll.txt","rb"))==NULL)
  271.     {
  272.      printf("\nError in open file");
  273.      printf("\007");
  274.      getch();  exit(1);
  275.     }
  276.  
  277.     printf("\nEnter record number :");
  278.     scanf("%d",&rec);  ch=getchar();
  279.     offset=(rec-1)*sizeof(payroll_rec);
  280.      if(fseek(fp,offset,0)!=0)
  281.      {
  282.       printf("Can't not find.\n");
  283.       getch();
  284.       exit(1);
  285.      }
  286.      fread(&payroll_rec,sizeof(payroll_rec),1,fp);
  287.     strcpy(id,payroll_rec.no);
  288.      getch();
  289.   fp=fopen("payroll.txt","r+b");
  290.   offset=(rec-1)*sizeof(payroll_rec);
  291.     if(fseek(fp,offset,0)!=0)
  292.     {
  293.      printf("Can't not find.\n");
  294.      getch();
  295.      exit(1);
  296.     }
  297.       printf("\nEnter Employee ID :%s\n",id);
  298.       strcpy(payroll_rec.no,id);
  299.      printf("Enter Employee Name :");
  300.      gets(payroll_rec.name);
  301.       printf("Enter Hours Worked :");
  302.       payroll_rec.salary=atof(gets(str));
  303.       printf("Enter Hourly Wage:");
  304.       payroll_rec.rate=atof(gets(str));
  305.       payroll_rec.net=payroll_rec.salary*payroll_rec.rate;
  306.       fwrite(&payroll_rec,sizeof(payroll_rec),1,fp);
  307.  
  308. fclose(fp);
  309. printf("Press any key to return to main menu");
  310. }
  311.  
  312. void del(void)
  313. {
  314. FILE*fp;
  315. int i=0;
  316. char ans;
  317. char rec[7];
  318. system("cls");
  319. printf("\n \5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\7");
  320. printf("\n\t\t Delete Record Module\n ");
  321. printf("\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\5\n\n");
  322. if((fp=fopen("payroll.txt","rb+"))==NULL)
  323. {
  324. printf("Error in open file");
  325. printf("\007");
  326. getch();
  327. exit(1);
  328. }
  329. printf("\n\nThis is data for delete\n");
  330. do{
  331. while(1){
  332. fread(&payroll_rec,sizeof(payroll_rec),1,fp);
  333. if(ferror(fp)){
  334.      printf("\nError read of delete file");
  335.      getch();  exit(1); }
  336.      if(feof(fp))
  337.      break;
  338.      ++i;
  339.      printf("%d. %s\t\t",i,payroll_rec.no);
  340.      }
  341.      printf("\nDo you want to delete data(y or n) :");
  342.      ans=getche();
  343.      if(ans=='y') {
  344.      printf("\nEnter record number :");
  345.      gets(rec);
  346.      fseek(fp,((atoi(rec)-1)*sizeof(payroll_rec)),SEEK_SET);
  347.      strcpy(payroll_rec.no,"\0");
  348.      strcpy(payroll_rec.name,"\0");
  349.      payroll_rec.salary=NULL;
  350.      payroll_rec.rate=NULL;
  351.      payroll_rec.net=NULL;
  352.      fwrite(&payroll_rec,sizeof(payroll_rec),1,fp);
  353.      if(ferror(fp)) {
  354.         printf("Error for delete data\n");
  355.         getch();
  356.         exit(1);
  357.         }
  358.         printf("Delete data complete\n");
  359.         }
  360.         printf("\nContinue(press c) or Exit(press e) :");
  361.         ans=getche();
  362.         printf("\n");
  363.         }while(ans!='e');
  364.         fclose(fp);
  365.         }
Oct 19 '07 #15

Post your reply

Sign in to post your reply or Sign up for a free account.