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, 14 2415
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.
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
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...
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
[ - #include<fstream>
-
#include<string>
-
#include<iostream>
-
#include<iomanip>
-
#include<stdio.h>
-
using namespace std;
-
struct employee{ string fname, lname;
-
double hourlyrate, hoursworked, netpay;
-
}emp; //employee
-
employee e[100];
-
int n=0;
-
void load(){
-
ifstream fin("employee.txt",ios::in);
-
while(fin>>e[n].fname>>e[n].lname>>e[n].hourlyrate>>e[n].hoursworked){
-
e[n].netpay=e[n].hourlyrate*e[n].hoursworked;
-
n++;}//WHILE
-
}//LOAD
-
void store(){
-
ofstream fout("employee.txt",ios::out);
-
for(int i=0; i<n; i++){
-
if(e[i].fname!=" ")
-
fout<<e[i].fname<<" "<<e[i].lname<<" "<<e[i].hourlyrate<<" "<<e[i].hoursworked<<endl;
-
}//FOR
-
}//STORE
-
-
void insert(){
-
printf("Enter employee's first name: ");
-
scanf("s",& emp.fname);
-
printf("Enter employee's last name: ");
-
scanf("s",& emp.lname);
-
cout<<"Enter employee's salary per hour: "; cin>>e[n].hourlyrate;
-
if(cin.fail())
-
{cout<<"Error\n"; exit(1);}
-
cout<<"Enter employee's hours worked: ";
-
cin>>e[n].hoursworked;
-
e[n].netpay=e[n].hourlyrate*e[n].hoursworked;
-
n++; }//INSERTS THE RECORD TO ARRAY
-
-
void display(){
-
-
cout<<setiosflags(ios::left)<<setw(15)<<"First Name"<<setw(15)<<"Last Name";
-
cout<<setw(10)<<"Hourly Rate"<<setw(10)<<"HrsWorked"<<setw(10)<<"Net Pay";
-
cout<<endl<<endl;
-
cout<<setiosflags(ios::fixed|ios::showpoint|ios::left);
-
for(int i=0;i<n;i++){
-
if(e[i].fname!=" "){
-
cout<<setiosflags(ios::left)<<setw(15)<<e[i].fname<<setw(15)<<e[i].lname;
-
cout<<setprecision(2)<<setiosflags(ios::left)<<setw(10)<<e[i].hourlyrate;
-
cout<<setw(10)<<e[i].hoursworked<<setw(10)<<e[i].netpay;
-
cout<<resetiosflags(ios::left)<<endl;}//IF
-
}//FOR
-
}//THIS FUNCTION DISPLAYS ALL RECORDS IN ARRAY
-
-
int search(string s){
-
for(int i=0;i<n;i++)if(s==e[i].lname)return i;//FOR
-
return -1;
-
}//SEARCH BY NAME,-1 NOT FOUND
-
void update(){
-
string searchn;
-
cout<<"Enter the Last Name of the Employee to be modified: "; cin>>searchn;
-
int i=search(searchn);
-
if(i==-1) cout<<"EMPLOYEE NOT LISTED"<<endl;
-
else{
-
cout<<endl<<"Please enter the updated information."<<endl;
-
cout<<"Enter employee's first name: "; cin>>e[i].fname;
-
cout<<"Enter employee's last name: "; cin>>e[i].lname;
-
cout<<"Enter employee's rate per hour: "; cin>>e[i].hourlyrate;
-
cout<<"Enter employee's hours worked: "; cin>>e[i].hoursworked;
-
e[i].netpay=(e[i].hourlyrate*e[i].hoursworked);}
-
}//UPDATE
-
-
void deleterec(){
-
string searchn;
-
cout<<"Enter the Last Name of the Employee to delete: "; cin>>searchn;
-
int k=-1;
-
for(int j=0; j<n;j++){
-
if(searchn==e[j].lname)
-
k=j;}
-
if(k!=-1)
-
{ cout<<" Name: "<<e[k].fname<<" "<<e[k].lname<<endl;
-
cout<<" Salary: "<<e[k].hourlyrate<<endl;
-
cout<<" Hrs Worked: "<<e[k].hoursworked<<endl;
-
cout<<" Net Pay: "<<e[k].netpay<<endl<<endl;}
-
-
char ans;
-
cout<<endl<<"Do you want to delete this record? (Y/N): ";
-
cin>>ans;
-
if(ans=='Y'||ans=='y'){
-
e[k].fname=" ";}//IF
-
else cout<<"EMPLOYEE NOT FOUND"; }//DELETE
-
-
void report(){
-
int i=0;
-
double totalhrs=0.00, totalpay=0.00;
-
for(i=0;i<n;i++){
-
totalhrs+=e[i].hoursworked;
-
totalpay+=e[i].netpay; }//FOR
-
cout<<"Total number of employees: " <<n<<endl<<endl;
-
cout<<"Total hours worked by all employees: "<<totalhrs<<endl<<endl;
-
cout<<"Total pay of all employees: "<<totalpay<<endl<<endl; }//REPORT
-
-
void deletefile(){
-
int pword;
-
cout<<"Enter your administrative password: "; cin>>pword;
-
if(pword==5438){
-
ofstream record("employee.txt", ios::out);
-
record<<endl;
-
load();
-
record.close();}
-
else cout<<"Wrong password entered."<<endl; }//DELETE FILE
-
- void backupfile(){
-
system("copy employee.txt backup.txt");
-
cout<<endl<<"Back-up file has been made."<<endl;
-
}
-
-
void main(){
-
load();
-
char choice='0';
-
while (choice!='e'&&choice!='E'){
-
printf("\nPayroll Database\n\n");
-
printf("1. Insert Employee Record and Compute Net Pay\n");
-
printf("2. Display All Records\n");
-
printf("3. Search Database\n");
-
printf("4. Update Record\n");
-
printf("5. Delete Record\n");
-
printf("6. Generate Report\n");
-
printf("7. Delete Database File\n");
-
printf("8. Back-up Database File\n");
-
printf("\nType e to exit\n\n");
-
printf("Enter the number of your choice:\n");
-
scanf("%c",&choice);
-
switch (choice){
-
-
case '1': insert(); break;
-
case '2': display(); break;
-
case '3': char s[100]; int index;
-
cout<<"Enter the last name of the person you want: ";
-
cin>>s;
-
index=search(s);
-
if(index!=-1) {
-
cout<<"Name: "<<e[index].fname<<" "<<e[index].lname<<endl;
-
cout<<"Hourly Rate: "<<e[index].hourlyrate<<endl;
-
cout<<"Hrs Worked: "<<e[index].hoursworked<<endl;
-
cout<<"Net Pay: "<<e[index].netpay<<endl<<endl;}//IF
-
else cout<<"EMPLOYEE NOT FOUND"<<endl;
-
break;
-
case '4': update(); break;
-
case '5': deleterec(); break;
-
case '6': report(); break;
-
case '7': deletefile(); break;
-
case '8': backupfile(); break;
-
case 'e': store(); break; //EXIT
-
default: cout<<"Invalid Choice"<<endl; break; }//SWITCH
-
}//WHILE
-
}//MAIN
-
The bold section's are the part i am having problem to convert to C language. Sorry for the trouble.
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.
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.
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.
Can anyone give show me a example how to use my fopen in this part of my coding: - 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: - cout<<setiosflags(ios::left)<<setw(15)<<"First Name"<<setw(15)<<"Last Name";
-
cout<<setw(10)<<"Hourly Rate"<<setw(10)<<"HrsWorked"<<setw(10)<<"Net Pay";
-
cout<<endl<<endl;
-
cout<<setiosflags(ios::fixed|ios::showpoint|ios::left);
-
for(int i=0;i<n;i++){
-
if(e[i].fname!=" "){
-
cout<<setiosflags(ios::left)<<setw(15)<<e[i].fname<<setw(15)<<e[i].lname;
-
cout<<setprecision(2)<<setiosflags(ios::left)<<setw(10)<<e[i].hourlyrate;
-
cout<<setw(10)<<e[i].hoursworked<<setw(10)<<e[i].netpay;
-
cout<<resetiosflags(ios::left)<<endl;}//IF
-
}//FOR
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: -
// C++ print
-
fout<<e[i].fname<<" "<<e[i].lname<<" "<<e[i].hourlyrate<<" "<<e[i].hoursworked<<endl;
-
-
/* C print */
-
FILE * fhandle = fopen( ....);
-
fprintf( fhandle, "%s %s %d %d\n", e[i].fname, e[i].lname, e[i].hourlyrate, e[i].hoursworked);
-
The above fprintf format statement assumes that the last two values are integers. If they are doubles, use "%lf" instead of "%d".
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.
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...
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. - #include<stdio.h>
-
#include<stdlib.h>
-
#include<conio.h>
-
#include<ctype.h>
-
#include<string.h>
-
#include <time.h>
-
void ins(void);
-
void oldf(void);
-
void newf(void);
-
void add(void);
-
void search(void);
-
void upd(void);
-
void del(void);
-
struct payroll
-
{ char no[5];
-
char name[25];
-
double salary;
-
double rate;
-
double net;
-
} payroll_rec;
-
-
void main()
-
{
-
char i;
-
time_t now;
-
time(&now);
-
-
-
do
-
{
-
system("cls");
-
-
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");
-
printf("\n\n Welcome to Optimum Technologies' Payroll Department\t\t\t\t\t %.24s.\n",ctime(&now));
-
printf("\t\t Main Menu\n\n");
-
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");
-
printf("\nWhat do you wish to do?\n");
-
printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n\n");
-
printf("1. Insert Employee Record And Compute Weekly Pay\n");
-
printf("2. Display All Records\n");
-
printf("3. Compute Monthly Pay Slip\n");
-
printf("4. Add Employee Record\n");
-
printf("5. Search and View Record\n");
-
printf("6. Update Record\n");
-
printf("7. Delete Record\n");
-
printf("8. Exit\n");
-
printf("\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
-
printf("You choose number :");
-
i=getche();
-
-
switch(tolower(i))
-
{
-
case '1': ins(); break;
-
case '2': oldf(); break;
-
case '3': newf(); break;
-
case '4': add(); break;
-
case '5': search(); break;
-
case '6': upd(); break;
-
case '7': del(); break;
-
case '8': printf("\7\n\tPlease any key to exit...");
-
getch();
-
exit(0);
-
break;
-
default : printf("\n\tChoice error ! Please try again.");
-
getch();
-
}
-
}while(i!='7');
-
-
}
-
-
void ins(void)
-
{
-
FILE *fp;
-
if((fp=fopen("payroll.txt","wb"))==NULL)
-
{
-
printf("Error in opening file");
-
exit(1);
-
}
-
struct payroll
-
{
-
char no[5];
-
char name[25];
-
double salary;
-
double rate;
-
double net;
-
} payroll_rec;
-
char numstr[50];
-
system("cls");
-
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");
-
printf("\n Insert Employee Record And Compute Weekly Pay Module\n");
-
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");
-
do
-
{
-
-
printf("\n\nEnter Employee No : ");
-
gets(payroll_rec.no);
-
printf("Enter Employee Name : ");
-
gets(payroll_rec.name);
-
printf("Enter Hours Worked Per Week : ");
-
payroll_rec.salary=atof(gets(numstr));
-
printf("Enter Hourly Wage : ");
-
payroll_rec.rate=atof(gets(numstr));
-
payroll_rec.net=payroll_rec.salary*payroll_rec.rate;
-
printf("Your Weekly Pay is RM %.2f\n", payroll_rec.net);
-
fwrite(&payroll_rec,sizeof(payroll_rec),1,fp);
-
printf("Add another person(y/n)? :");
-
}
-
while(tolower(getche())=='y');
-
fclose(fp);
-
printf("\nPress any key to return to main menu");
-
getch();
-
}
-
-
void oldf(void)
-
{
-
FILE *fp;
-
if((fp=fopen("payroll.txt","rb"))==NULL)
-
{
-
printf("Error in open file");
-
printf("\007");
-
getch(); exit(1);
-
} system("cls");
-
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");
-
printf("\n\t\t\t Display Record Module\n ");
-
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");
-
printf("\n\n**********************************************************************\n");
-
printf(" Employee ID Employee Name Hours Worked Hourly Wage Weekly Pay\n");
-
printf("***********************************************************************\n");
-
while(fread(&payroll_rec,sizeof(payroll_rec),1,fp)==1)
-
{
-
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);
-
-
}
-
fclose(fp);
-
printf("Press any key to return to main menu\n");
-
getch();
-
}
-
-
void newf(void)
-
{
-
double net;
-
double total;
-
FILE *fp;
-
if((fp=fopen("payroll.txt","rb"))==NULL)
-
{
-
printf("Error in open file");
-
printf("\007");
-
getch(); exit(1);
-
} system("cls");
-
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");
-
printf("\n\t\t Compute Monthly Pay Slip Module\n");
-
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");
-
while(fread(&payroll_rec,sizeof(payroll_rec),1,fp)==1)
-
{
-
-
net=payroll_rec.salary*payroll_rec.rate*4;
-
total=payroll_rec.salary*4;
-
printf("\n\n||======================================================================||\n");
-
printf("||\t\t\tOptimum Technologies Pay Vaucher ||\n");
-
printf("|| ||\n");
-
printf("||Employee Name : %s\t\t\t\t\t\t||\n",payroll_rec.name);
-
printf("||Empolyee ID : %s\t\t\t\t\t\t\t||\n",payroll_rec.no);
-
printf("||Hours Worked : %.2f\t\t\t\t\t\t\t||\n",total);
-
printf("||Hourly Wage : RM %.2f\t\t\t\t\t\t\t||",payroll_rec.rate);
-
printf("||Net Salary : RM %.2f\t\t\t\t\t\t||\n",net);
-
printf("|| ||\n");
-
printf("||This is a digitally signed voucher it does not require signature\t||\n");
-
printf("|| ||\n");
-
printf("||======================================================================||\n");
-
}
-
fclose(fp);
-
printf("Press any key to return to main menu");
-
getch();
-
}
-
-
void add(void)
-
{
-
FILE *fp;
-
if((fp=fopen("payroll.txt","ab"))==NULL)
-
{
-
printf("Error in open file");
-
printf("\007");
-
getch(); exit(1);
-
}
-
char str[10];
-
system("cls");
-
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");
-
printf("\n\t\t Add Record Module\n ");
-
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");
-
do{
-
printf("\nEnter Employee No :");
-
gets(payroll_rec.no);
-
printf("Enter Employee Name :");
-
gets(payroll_rec.name);
-
printf("Enter Hours Worked Per Week :");
-
payroll_rec.salary=atof(gets(str));
-
printf("Enter Hourly Wage:");
-
payroll_rec.rate=atof(gets(str));
-
payroll_rec.net=payroll_rec.salary*payroll_rec.rate;
-
printf("Your Weekly Pay is RM %.2f\n", payroll_rec.net);
-
fwrite(&payroll_rec,sizeof(payroll_rec),1,fp);
-
printf("Add another person(y/n)? :");
-
} while(tolower(getche())=='y');
-
fclose(fp);
-
-
}
-
-
-
void search(void)
-
{
-
FILE *fp;
-
int i=0;
-
int rec;
-
char ch;
-
int offset;
-
system("cls");
-
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");
-
printf("\n\t\t Serach and View Record Module\n");
-
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");
-
-
if((fp=fopen("payroll.txt","rb"))==NULL)
-
{
-
printf("\nError in open file");
-
printf("\007");
-
getch();
-
exit(1);
-
}
-
do
-
{
-
printf("\n\nEnter record number to search :");
-
scanf("%d",&rec); ch=getchar();
-
offset=(rec-1)*sizeof(payroll_rec);
-
if(fseek(fp,offset,0)!=0)
-
{
-
printf("Can't not find.\n");
-
getch();
-
exit(1);
-
-
}
-
fread(&payroll_rec,sizeof(payroll_rec),1,fp);
-
printf("\nEmployee ID : %s\n",payroll_rec.no);
-
printf("Employee Name : %s\n",payroll_rec.name);
-
printf("Hours Worked : %.2f\n",payroll_rec.salary);
-
printf("Hourly Wage : %.2f\n",payroll_rec.rate);
-
printf("Weekly Salary : %.2f\n",payroll_rec.net);
-
printf("Do you wish to continue(y/n)? :");
-
-
-
}
-
while(tolower(getche())=='y');
-
fclose(fp);
-
printf("\nPress any key to return to main menu");
-
getch();
-
}
-
-
-
void upd(void)
-
{
-
FILE *fp;
-
int i=0;
-
int rec;
-
char ch,str[20];
-
char id[5];
-
int offset;
-
system("cls");
-
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");
-
printf("\n\t\t Update Record Module\n");
-
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");
-
-
if((fp=fopen("payroll.txt","rb"))==NULL)
-
{
-
printf("\nError in open file");
-
printf("\007");
-
getch(); exit(1);
-
}
-
-
printf("\nEnter record number :");
-
scanf("%d",&rec); ch=getchar();
-
offset=(rec-1)*sizeof(payroll_rec);
-
if(fseek(fp,offset,0)!=0)
-
{
-
printf("Can't not find.\n");
-
getch();
-
exit(1);
-
}
-
fread(&payroll_rec,sizeof(payroll_rec),1,fp);
-
strcpy(id,payroll_rec.no);
-
getch();
-
fp=fopen("payroll.txt","r+b");
-
offset=(rec-1)*sizeof(payroll_rec);
-
if(fseek(fp,offset,0)!=0)
-
{
-
printf("Can't not find.\n");
-
getch();
-
exit(1);
-
}
-
printf("\nEnter Employee ID :%s\n",id);
-
strcpy(payroll_rec.no,id);
-
printf("Enter Employee Name :");
-
gets(payroll_rec.name);
-
printf("Enter Hours Worked :");
-
payroll_rec.salary=atof(gets(str));
-
printf("Enter Hourly Wage:");
-
payroll_rec.rate=atof(gets(str));
-
payroll_rec.net=payroll_rec.salary*payroll_rec.rate;
-
fwrite(&payroll_rec,sizeof(payroll_rec),1,fp);
-
-
fclose(fp);
-
printf("Press any key to return to main menu");
-
}
-
-
void del(void)
-
{
-
FILE*fp;
-
int i=0;
-
char ans;
-
char rec[7];
-
system("cls");
-
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");
-
printf("\n\t\t Delete Record Module\n ");
-
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");
-
if((fp=fopen("payroll.txt","rb+"))==NULL)
-
{
-
printf("Error in open file");
-
printf("\007");
-
getch();
-
exit(1);
-
}
-
printf("\n\nThis is data for delete\n");
-
do{
-
while(1){
-
fread(&payroll_rec,sizeof(payroll_rec),1,fp);
-
if(ferror(fp)){
-
printf("\nError read of delete file");
-
getch(); exit(1); }
-
if(feof(fp))
-
break;
-
++i;
-
printf("%d. %s\t\t",i,payroll_rec.no);
-
}
-
printf("\nDo you want to delete data(y or n) :");
-
ans=getche();
-
if(ans=='y') {
-
printf("\nEnter record number :");
-
gets(rec);
-
fseek(fp,((atoi(rec)-1)*sizeof(payroll_rec)),SEEK_SET);
-
strcpy(payroll_rec.no,"\0");
-
strcpy(payroll_rec.name,"\0");
-
payroll_rec.salary=NULL;
-
payroll_rec.rate=NULL;
-
payroll_rec.net=NULL;
-
fwrite(&payroll_rec,sizeof(payroll_rec),1,fp);
-
if(ferror(fp)) {
-
printf("Error for delete data\n");
-
getch();
-
exit(1);
-
}
-
printf("Delete data complete\n");
-
}
-
printf("\nContinue(press c) or Exit(press e) :");
-
ans=getche();
-
printf("\n");
-
}while(ans!='e');
-
fclose(fp);
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. - #include<stdio.h>
-
#include<stdlib.h>
-
#include<conio.h>
-
#include<ctype.h>
-
#include<string.h>
-
#include <time.h>
-
void ins(void);
-
void oldf(void);
-
void newf(void);
-
void add(void);
-
void search(void);
-
void upd(void);
-
void del(void);
-
struct payroll
-
{ char no[5];
-
char name[25];
-
double salary;
-
double rate;
-
double net;
-
} payroll_rec;
-
-
void main()
-
{
-
char i;
-
time_t now;
-
time(&now);
-
-
-
do
-
{
-
system("cls");
-
-
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");
-
printf("\n\n Welcome to Optimum Technologies' Payroll Department\t\t\t\t\t %.24s.\n",ctime(&now));
-
printf("\t\t Main Menu\n\n");
-
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");
-
printf("\nWhat do you wish to do?\n");
-
printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n\n");
-
printf("1. Insert Employee Record And Compute Weekly Pay\n");
-
printf("2. Display All Records\n");
-
printf("3. Compute Monthly Pay Slip\n");
-
printf("4. Add Employee Record\n");
-
printf("5. Search and View Record\n");
-
printf("6. Update Record\n");
-
printf("7. Delete Record\n");
-
printf("8. Exit\n");
-
printf("\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
-
printf("You choose number :");
-
i=getche();
-
-
switch(tolower(i))
-
{
-
case '1': ins(); break;
-
case '2': oldf(); break;
-
case '3': newf(); break;
-
case '4': add(); break;
-
case '5': search(); break;
-
case '6': upd(); break;
-
case '7': del(); break;
-
case '8': printf("\7\n\tPlease any key to exit...");
-
getch();
-
exit(0);
-
break;
-
default : printf("\n\tChoice error ! Please try again.");
-
getch();
-
}
-
}while(i!='7');
-
-
}
-
-
void ins(void)
-
{
-
FILE *fp;
-
if((fp=fopen("payroll.txt","wb"))==NULL)
-
{
-
printf("Error in opening file");
-
exit(1);
-
}
-
struct payroll
-
{
-
char no[5];
-
char name[25];
-
double salary;
-
double rate;
-
double net;
-
} payroll_rec;
-
char numstr[50];
-
system("cls");
-
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");
-
printf("\n Insert Employee Record And Compute Weekly Pay Module\n");
-
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");
-
do
-
{
-
-
printf("\n\nEnter Employee No : ");
-
gets(payroll_rec.no);
-
printf("Enter Employee Name : ");
-
gets(payroll_rec.name);
-
printf("Enter Hours Worked Per Week : ");
-
payroll_rec.salary=atof(gets(numstr));
-
printf("Enter Hourly Wage : ");
-
payroll_rec.rate=atof(gets(numstr));
-
payroll_rec.net=payroll_rec.salary*payroll_rec.rate;
-
printf("Your Weekly Pay is RM %.2f\n", payroll_rec.net);
-
fwrite(&payroll_rec,sizeof(payroll_rec),1,fp);
-
printf("Add another person(y/n)? :");
-
}
-
while(tolower(getche())=='y');
-
fclose(fp);
-
printf("\nPress any key to return to main menu");
-
getch();
-
}
-
-
void oldf(void)
-
{
-
FILE *fp;
-
if((fp=fopen("payroll.txt","rb"))==NULL)
-
{
-
printf("Error in open file");
-
printf("\007");
-
getch(); exit(1);
-
} system("cls");
-
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");
-
printf("\n\t\t\t Display Record Module\n ");
-
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");
-
printf("\n\n**********************************************************************\n");
-
printf(" Employee ID Employee Name Hours Worked Hourly Wage Weekly Pay\n");
-
printf("***********************************************************************\n");
-
while(fread(&payroll_rec,sizeof(payroll_rec),1,fp)==1)
-
{
-
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);
-
-
}
-
fclose(fp);
-
printf("Press any key to return to main menu\n");
-
getch();
-
}
-
-
void newf(void)
-
{
-
double net;
-
double total;
-
FILE *fp;
-
if((fp=fopen("payroll.txt","rb"))==NULL)
-
{
-
printf("Error in open file");
-
printf("\007");
-
getch(); exit(1);
-
} system("cls");
-
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");
-
printf("\n\t\t Compute Monthly Pay Slip Module\n");
-
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");
-
while(fread(&payroll_rec,sizeof(payroll_rec),1,fp)==1)
-
{
-
-
net=payroll_rec.salary*payroll_rec.rate*4;
-
total=payroll_rec.salary*4;
-
printf("\n\n||======================================================================||\n");
-
printf("||\t\t\tOptimum Technologies Pay Vaucher ||\n");
-
printf("|| ||\n");
-
printf("||Employee Name : %s\t\t\t\t\t\t||\n",payroll_rec.name);
-
printf("||Empolyee ID : %s\t\t\t\t\t\t\t||\n",payroll_rec.no);
-
printf("||Hours Worked : %.2f\t\t\t\t\t\t\t||\n",total);
-
printf("||Hourly Wage : RM %.2f\t\t\t\t\t\t\t||",payroll_rec.rate);
-
printf("||Net Salary : RM %.2f\t\t\t\t\t\t||\n",net);
-
printf("|| ||\n");
-
printf("||This is a digitally signed voucher it does not require signature\t||\n");
-
printf("|| ||\n");
-
printf("||======================================================================||\n");
-
}
-
fclose(fp);
-
printf("Press any key to return to main menu");
-
getch();
-
}
-
-
void add(void)
-
{
-
FILE *fp;
-
if((fp=fopen("payroll.txt","ab"))==NULL)
-
{
-
printf("Error in open file");
-
printf("\007");
-
getch(); exit(1);
-
}
-
char str[10];
-
system("cls");
-
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");
-
printf("\n\t\t Add Record Module\n ");
-
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");
-
do{
-
printf("\nEnter Employee No :");
-
gets(payroll_rec.no);
-
printf("Enter Employee Name :");
-
gets(payroll_rec.name);
-
printf("Enter Hours Worked Per Week :");
-
payroll_rec.salary=atof(gets(str));
-
printf("Enter Hourly Wage:");
-
payroll_rec.rate=atof(gets(str));
-
payroll_rec.net=payroll_rec.salary*payroll_rec.rate;
-
printf("Your Weekly Pay is RM %.2f\n", payroll_rec.net);
-
fwrite(&payroll_rec,sizeof(payroll_rec),1,fp);
-
printf("Add another person(y/n)? :");
-
} while(tolower(getche())=='y');
-
fclose(fp);
-
-
}
-
-
-
void search(void)
-
{
-
FILE *fp;
-
int i=0;
-
int rec;
-
char ch;
-
int offset;
-
system("cls");
-
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");
-
printf("\n\t\t Serach and View Record Module\n");
-
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");
-
-
if((fp=fopen("payroll.txt","rb"))==NULL)
-
{
-
printf("\nError in open file");
-
printf("\007");
-
getch();
-
exit(1);
-
}
-
do
-
{
-
printf("\n\nEnter record number to search :");
-
scanf("%d",&rec); ch=getchar();
-
offset=(rec-1)*sizeof(payroll_rec);
-
if(fseek(fp,offset,0)!=0)
-
{
-
printf("Can't not find.\n");
-
getch();
-
exit(1);
-
-
}
-
fread(&payroll_rec,sizeof(payroll_rec),1,fp);
-
printf("\nEmployee ID : %s\n",payroll_rec.no);
-
printf("Employee Name : %s\n",payroll_rec.name);
-
printf("Hours Worked : %.2f\n",payroll_rec.salary);
-
printf("Hourly Wage : %.2f\n",payroll_rec.rate);
-
printf("Weekly Salary : %.2f\n",payroll_rec.net);
-
printf("Do you wish to continue(y/n)? :");
-
-
-
}
-
while(tolower(getche())=='y');
-
fclose(fp);
-
printf("\nPress any key to return to main menu");
-
getch();
-
}
-
-
-
void upd(void)
-
{
-
FILE *fp;
-
int i=0;
-
int rec;
-
char ch,str[20];
-
char id[5];
-
int offset;
-
system("cls");
-
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");
-
printf("\n\t\t Update Record Module\n");
-
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");
-
-
if((fp=fopen("payroll.txt","rb"))==NULL)
-
{
-
printf("\nError in open file");
-
printf("\007");
-
getch(); exit(1);
-
}
-
-
printf("\nEnter record number :");
-
scanf("%d",&rec); ch=getchar();
-
offset=(rec-1)*sizeof(payroll_rec);
-
if(fseek(fp,offset,0)!=0)
-
{
-
printf("Can't not find.\n");
-
getch();
-
exit(1);
-
}
-
fread(&payroll_rec,sizeof(payroll_rec),1,fp);
-
strcpy(id,payroll_rec.no);
-
getch();
-
fp=fopen("payroll.txt","r+b");
-
offset=(rec-1)*sizeof(payroll_rec);
-
if(fseek(fp,offset,0)!=0)
-
{
-
printf("Can't not find.\n");
-
getch();
-
exit(1);
-
}
-
printf("\nEnter Employee ID :%s\n",id);
-
strcpy(payroll_rec.no,id);
-
printf("Enter Employee Name :");
-
gets(payroll_rec.name);
-
printf("Enter Hours Worked :");
-
payroll_rec.salary=atof(gets(str));
-
printf("Enter Hourly Wage:");
-
payroll_rec.rate=atof(gets(str));
-
payroll_rec.net=payroll_rec.salary*payroll_rec.rate;
-
fwrite(&payroll_rec,sizeof(payroll_rec),1,fp);
-
-
fclose(fp);
-
printf("Press any key to return to main menu");
-
}
-
-
void del(void)
-
{
-
FILE*fp;
-
int i=0;
-
char ans;
-
char rec[7];
-
system("cls");
-
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");
-
printf("\n\t\t Delete Record Module\n ");
-
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");
-
if((fp=fopen("payroll.txt","rb+"))==NULL)
-
{
-
printf("Error in open file");
-
printf("\007");
-
getch();
-
exit(1);
-
}
-
printf("\n\nThis is data for delete\n");
-
do{
-
while(1){
-
fread(&payroll_rec,sizeof(payroll_rec),1,fp);
-
if(ferror(fp)){
-
printf("\nError read of delete file");
-
getch(); exit(1); }
-
if(feof(fp))
-
break;
-
++i;
-
printf("%d. %s\t\t",i,payroll_rec.no);
-
}
-
printf("\nDo you want to delete data(y or n) :");
-
ans=getche();
-
if(ans=='y') {
-
printf("\nEnter record number :");
-
gets(rec);
-
fseek(fp,((atoi(rec)-1)*sizeof(payroll_rec)),SEEK_SET);
-
strcpy(payroll_rec.no,"\0");
-
strcpy(payroll_rec.name,"\0");
-
payroll_rec.salary=NULL;
-
payroll_rec.rate=NULL;
-
payroll_rec.net=NULL;
-
fwrite(&payroll_rec,sizeof(payroll_rec),1,fp);
-
if(ferror(fp)) {
-
printf("Error for delete data\n");
-
getch();
-
exit(1);
-
}
-
printf("Delete data complete\n");
-
}
-
printf("\nContinue(press c) or Exit(press e) :");
-
ans=getche();
-
printf("\n");
-
}while(ans!='e');
-
fclose(fp);
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Newbee Adam |
last post by:
I have been reading in help how I need to use decimal becuase currency does
not exist like I used in vb6.
I had a difficult time on google and msdn finding how or if I can take the
value of text...
|
by: Megat |
last post by:
I'm trying to create a conversion program, that convert a simple
proprietry programming language to an international standard
languange, using Visual C++. Need some help from those who has...
|
by: prabha |
last post by:
Hello Everybody,
I have to conert the word doc to multiple html files,according to the templates in the word doc.
I had converted the word to xml.Also through Exsl ,had finished the multiple...
|
by: Jake Jessup |
last post by:
A client wants me to do a conversion from Access 2.0 (yeah, I know, unreal).
The problem is, she doesn't have the install disks and I haven't had a copy
of 2.0 in years.
If some kind should...
|
by: Robert |
last post by:
I have a series of web applications (configured as separate applications) on
a server.
There is a main application at the root and then several virtual directories
that are independant...
|
by: sam_cit |
last post by:
Hi,
I needed help in converting a character to the correspoding
hexadecimal values, like the following example,
ASCII value : ABC
Hex Code Value : %41%42%43...
whats the logic of conversion...
|
by: manmit.walia |
last post by:
Hello All,
I have tried multiple online tools to convert an VB6 (bas) file to
VB.NET file and no luck. I was hoping that someone could help me covert
this. I am new to the .NET world and still...
|
by: Mhel |
last post by:
Hi everyone! I need some help in Converting Infix to postfix.I need a program that would do it.
Please, I really need help.
|
by: Hank Stalica |
last post by:
I'm having this weird problem where my code does the following
conversion from string to float:
27000000.0 -27000000.00
2973999.99 -29740000.00
2989999.13 -2989999.25
The number on the left...
|
by: helpwithcode |
last post by:
Hi people,
I am just learning java.I have been creating a project which involves JDBC Connectivity.I find that the statements,
String string_dob=text_dob.getText();
//Converting string to...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |