0
Shayaan_Mustafa's Avatar, Join Date: Dec 2010
Go4Expert Member
Quote:
Originally Posted by virxen View Post
it should be enclosed in { and }
if(e.e_no==numberToshow) {
........
cout<<"\nNet Paid :\t"<<e.basic+e.allowence-e.deduction;
}
Hello sir!
I have already done look in my 1st example.
I have enclosed if statements in braces.
But I am still at that point from where I was getting trouble.
Sir please run my code on "Turbo C++ IDE" then you will observe what I mean to say. Your help is much appreciable.
0
lincy's Avatar
Banned
Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#include<iostream.h>
class employee{
      public:
        int e_no;
        float basic,deduction,allowence;
        char name[50],address[50];
        void add();         //add new records.
        void dis();         //display records.
}employee payroll;              //class variable


//********main()*********


void main(){

     clrscr();
     int choice;
     cout<<"==========Main Menu=========="<<endl;
     cout<<endl<<endl;
     cout<<"1-> Add Record"<<endl;
     cout<<"2-> Display Record"<<endl;
     cout<<endl;
     
     cout<<"Ur choice =";cin>>choice;
     if(choice<0 || choice>2)

   {      
    switch(choice)
{
    case 1:cout<<"Add record:";
           cin>>payroll.add();
            break;
    case 2:cout<<"Record display";
           payroll.dis();
            break;
    default:cout<<"Exit"; 
 
}
getch();

}
void employee:piblic add()
{
     clrscr();
     FILE*fptr; //Declaring FILE type
     int choice;
     employee e;//variable of class
     do
{
    cout<<"========Enter New Record========"<<endl<<endl;
    cout<<"Enter Employee Number: ";cin>>e_no;
    cout<<"Enter Employee Name  : ";gets(e.name);
    cout<<"Enter Address        : ";gets(e.address);
    cout<<"Enter Basic Salary   : ";cin>>e.basic;
    cout<<"Enter Allowence      : ";cin>>e.allowence;
    cout<<"Enter Deduction      : ";cin>>e.deduction;
    cout<<endl<<endl;
    
      
}   while(!(choice==1 || choice==2));
    
    if(choice==1)
    {
       fptr=fopen("payroll.dat","w");         //open file for writting
       fwrite(&e,sizeof(employee),1,fptr);        //write to file
       fclose(fptr);                         //closes file
       cout<<"Record Saved"<<endl<<endl;
    } else

     cout<<"Record Cancelled";
     cout<<"Enter Another Record? "<<endl;
     cout<<"1->Yes | 2->No"<<endl;cin>>choice;
    
   if(choice==1)
       add();               //return add function
    else
       return();
}
void employee:public dis()
{
     clrscr();
     FILE*fptr;
     employee e;
     fptr=fopen("payroll.dat","r+");             //open file to read
     while(fread(&e,sizeof(employee),1,fptr))    //read file
{    
      cout<<"Enter Employee Number To Display Record: ";//asks employee no to display information
      cin>>disp;
      cout<<"\nName         : "<<e.name;
      cout<<"\nAddress      : "<<e.address;
      cout<<"\nBasic Salary : "<<e.basic;
      cout<<"\nAllowence    : "<<e.allowence;
      cout<<"\nDeductions   : "<<e.deduction<<endl;
      cout<<endl;
      cout<<"\nTotal Salary :\t"<<e.basic+e.allowence;
      cout<<"\nNet Paid     :\t"<<e.basic+e.allowence-e.deduction;
     }
     fclose(fptr);  //closes file
    return();
}
may be like this the program is written ,not sure

Last edited by shabbir; 10Jan2011 at 13:50.. Reason: Code blocks
0
Shayaan_Mustafa's Avatar, Join Date: Dec 2010
Go4Expert Member
Quote:
To Lincy
I am using turbo C++ compiler. So when I compiled your given code. It has 6 errors.
But I appreciate your help for me. Thanks.
If read my thread then try to understand what I am trying to say and please help me.
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#include<iostream.h>
#include <fstream.h>

class employee{
      public:
        int e_no;
        float basic,deduction,allowence;
        char name[50],address[50];
        void add();         //add new records.
        void dis();         //display records.
};
void employee::add(){
    int choice;
    do{
             clrscr();
             employee e;//variable of class
             do{
                cout<<"========Enter New Record========"<<endl<<endl;
                cout<<"Enter Employee Number: ";cin>>e.e_no;
                cout<<"Enter Employee Name  : ";gets(e.name);
                cout<<"Enter Address        : ";gets(e.address);
                cout<<"Enter Basic Salary   : ";cin>>e.basic;
                cout<<"Enter Allowence      : ";cin>>e.allowence;
                cout<<"Enter Deduction      : ";cin>>e.deduction;
                cout<<endl<<endl;
                cout<<"1->Save | 2->Cancel"<<endl;cin>>choice;
              }while(!(choice==1 || choice==2));
            if(choice==1){
                   ofstream fptr;
                   fptr.open("payroll.dat",ios::app);         //open file for writting
                  fptr.write((char *)&e,sizeof(employee));        //write to file
                   fptr.close();
                   cout<<"Record Saved"<<endl<<endl;
            }else
                   cout<<"Record Cancelled";
        do{
            cout<<"Enter Another Record? "<<endl;
            cout<<"1->Yes | 2->No"<<endl;cin>>choice;
        }while(choice<1 || choice>2);
    }while(choice==1);
}

void employee::dis(){
    clrscr();
    ifstream fptr;
    fptr.open("payroll.dat",ios::in);
    employee e;
    int numberToShow;
    cout<<"ENTER EMPLOYEE NUMBER TO SHOW:"; cin>>numberToShow;
    while(fptr.read((char *)&e,sizeof(employee))){    //read file
         if (numberToShow==e.e_no){
            cout<<"\n\nEmployee Number: "<<e.e_no;
              cout<<"\nName         : "<<e.name;
              cout<<"\nAddress      : "<<e.address;
              cout<<"\nBasic Salary : "<<e.basic;
              cout<<"\nAllowence    : "<<e.allowence;
              cout<<"\nDeductions   : "<<e.deduction<<endl;
              cout<<endl;
              cout<<"\nTotal Salary :\t"<<e.basic+e.allowence;
            cout<<"\nNet Paid     :\t"<<e.basic+e.allowence-e.deduction;
         }
    }
    fptr.close();
    getch();
}


//********main()*********
int main(){
     employee payroll;           //class variable
     int choice;
     while(1==1){
        choice=-1;
         while(choice<0 || choice>2){
            clrscr();
             cout<<"==========Main Menu=========="<<endl;
             cout<<endl<<endl;
             cout<<"1-> Add Record"<<endl;
             cout<<"2-> Display Record"<<endl;
             cout<<endl;
             cout<<"\n0-> Exit"<<endl;
             cout<<endl<<endl;
             cout<<"selection: ";cin>>choice;
         }
        switch(choice){
                 case 1:
                        payroll.add();
                        break;
                 case 2:
                        payroll.dis();
                        break;
                 case 0: 
                        getch();
                        return 0;
        }
      }
}

delete the old payroll.dat file before you test it.
0
Shayaan_Mustafa's Avatar, Join Date: Dec 2010
Go4Expert Member
Quote:
TO VIRXEN
delete the old payroll.dat file before you test it.
Hello sir.
Kindly tell me where is my old payroll.dat file is saved?
As you can see I have not given it any path so where it is?
Thank you.
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
it is located inside the folder where your source code and executable files are.


p.s. you can always use windows search!
0
Shayaan_Mustafa's Avatar, Join Date: Dec 2010
Go4Expert Member
Quote:
Originally Posted by virxen View Post
it is located inside the folder where your source code and executable files are.


p.s. you can always use windows search!
Hello sir. How are you?
Sorry for late response.
I tried your given code this time. But now it is not showing any record.what should I do?

I am definitely sure that you could understood my problem because until you don't understand it, you can't help me.
I know you would understand and can help me.
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
just re-enter them
0
Shayaan_Mustafa's Avatar, Join Date: Dec 2010
Go4Expert Member
Quote:
Originally Posted by virxen View Post
just re-enter them
Hello sir!
I did. I re-enter them but failed. No success.
I will try it again and then inform you with my report.
I really really really great full for your help and appreciate it.
Thanks a lot.