C/C++(File handling related Problem)

Discussion in 'C++' started by Shayaan_Mustafa, Dec 31, 2010.

  1. Shayaan_Mustafa

    Shayaan_Mustafa New Member

    Joined:
    Dec 25, 2010
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Still a student.
    Location:
    Karachi
    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()*********
    main(){
         clrscr();
         int choice;
         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;
         if(choice<0 || choice>2){
        clrscr();
        main();           //return main function
         }
         switch(choice){
        case 1:payroll.add(); break;
        case 2:payroll.dis(); break;
        case 0:exit(0);
         }
    getch();
    return 0;
    }
    void employee::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;
        cout<<"1->Save | 2->Cancel"<<endl;cin>>choice;
          }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
           main();
    }
    void employee::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: ";cin>>e.e_no;//asks employee no to display information
          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
         main();
    }

    Here is my code.
    When I enter record of an employee then I press 1 to save data file. But I don't know it is saved or not.
    When I want to display that record then I get nothing. Means this program doesn't show any record. Why?
    Can you help me?
    Can you tell me where am I wrong?
    And I am using Turbo C++ IDE. Therefore I have included header files with extension(.h).
    Kindly answer me.
     
    Last edited: Dec 31, 2010
  2. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    When I compile your code I get the following warnings:

    In a C++ program you can not call main from any function and main must return an int, must be "int main()" or "int main(int argc char **argv)". Please see this link: Program Structure. And the following three links Control Structures, Functions I, and Functions II.

    Never use gets use fgets.

    Jim
     
  3. Shayaan_Mustafa

    Shayaan_Mustafa New Member

    Joined:
    Dec 25, 2010
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Still a student.
    Location:
    Karachi
    I know that your compiler surely detects some errors in my program. Therefore I have told you that I am using "Turbo C++ IDE".
    I am using probably traditional language. And you are telling me about new language therefore your compiler produced error messages.
    One thing more that new C++ language says that it should be "int main()" not only "main()". But my call is all perfect. My compiler don't produce any error. But my problem is that I am not able to display my saved information.
    You download "Turbo C++ IDE" is just in kbs. Please do help me. I know you can.
    Its a humble request.
     
  4. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    Please read the links provided.

    Nowhere in my post will you find any mention of a particular compiler. The problems stated are your problems whether your OBSOLETE compiler complains or not.

    You can not call main from any function in C++, period end of story.

    The use of gets() is bad no matter what compiler you are using.

    As far a me downloading "Turbo C++", it wont work on my system since I don't do Windows.

    Jim
     
  5. kyle

    kyle Member

    Joined:
    Dec 28, 2010
    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    6
    Occupation:
    don't have 1
    Location:
    i live in Italy,Milano
    i tried compiling it
    and according to my compiler at line 42 you have sth wrong
    i am not an expert so i can't find out which is the prob.
    there was only 1 prob
    but follow my advice
    look every line carefully 'cause 1 line is depended by the lines before.
    good luck
     
  6. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    try this

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<iostream.h>
    [COLOR=Red]#include <fstream.h>[/COLOR]
    
    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()*********
    main(){
         clrscr();
         int choice;
         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;
         if(choice<0 || choice>2){
        clrscr();
        main();           //return main function
         }
         switch(choice){
        case 1:payroll.add(); break;
        case 2:payroll.dis(); break;
        case 0:exit(0);
         }
    getch();
    return 0;
    }
    void employee::add(){
    int choice;
    do{
         clrscr();
         employee e;//variable of class
         do{
        cout<<"========Enter New Record========"<<endl<<endl;
        cout<<"Enter Employee Number: ";cin>>[COLOR=Red]e.[/COLOR]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){
           [COLOR=Red]ofstream fptr;
           fptr.open("payroll.dat",ios::app);         //open file for writting
           fptr.write((char *)&e,sizeof(employee));        //write to file
           fptr.close();[/COLOR]
           cout<<"Record Saved"<<endl<<endl;
        }else
           cout<<"Record Cancelled";
         cout<<"Enter Another Record? "<<endl;
         cout<<"1->Yes | 2->No"<<endl;cin>>choice;
        }while(choice==1);
    
    }
    void employee::dis(){
         clrscr();
         [COLOR=Red]ifstream fptr;
         fptr.open("payroll.dat",ios::in);[/COLOR]
         employee e;
         while(fptr.read((char *)&e,sizeof(employee))){    //read file
          cout<<"\n\nEmployee Number To Display Record: "<<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();
    }
    
    
    
     
  7. Shayaan_Mustafa

    Shayaan_Mustafa New Member

    Joined:
    Dec 25, 2010
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Still a student.
    Location:
    Karachi
    Great. You are a genius person. Thanks.
    Now my problem is that when I press 2 to display employee's record then every information is shown to me. On the other hand I want like it as when I enter a specific employee's number then just only of that employee's information is shown not others until I don't enter any other number to show.
    I hope that you would understand my problem. So what should I do for this?
     
  8. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    Code:
     
    ...........
    [COLOR=Red]if (e.e_no==numberToShow){[/COLOR]
          cout<<"\n\nEmployee Number To Display Record: "<<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;
    [COLOR=Red]      break;
    }[/COLOR]
    ..........
    
    
    numberToShow--->the employee number to show.
    you can use scanf to get the number from user


     
    shabbir likes this.
  9. Shayaan_Mustafa

    Shayaan_Mustafa New Member

    Joined:
    Dec 25, 2010
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Still a student.
    Location:
    Karachi
    Thank you very much sir that you are helping me. I have change my code according to your saying.
    I have made following changes and then run my program.
    I have already saved 3 employee's record having employee numbers 6,5 & 1 respectively.
    Code:
    ......................
    void employee::dis(){
         clrscr();
         ifstream fptr;
         int numberToshow;
         fptr.open("payroll.dat",ios::in);
         employee e;
         while(fptr.read((char *)&e,sizeof(employee))){    //read file
          cout<<"\nEnter employee number To Display: ";
          scanf("%d",&numberToshow);
          if(e.e_no==numberToshow){
          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();
    }
    OUTPUT due to this change is:
    Code:
    Enter employee number To Display: 1
    Enter employee number To Display: 1
    Enter employee number To Display: 1
    Now information is shown for employee#1,
    .................
    Now I again run my code again want to display my recorded information, I found,
    Code:
    Enter employee number To Display: 5
    Enter employee number To Display: 5
    Now information is shown for employee#5,
    ................
    Now I again run my code and observe that,
    Code:
    Enter employee number To Display: 6
    Information is shown for employee#6,
    ...............
    Now its my last turn to check output and observes,
    Code:
    Enter employee number To Display: 6
    Information is shown for employee#6
    ..............
    Enter employee number To Display: 5
    Information is shown for employee#5
    ..............
    Enter employee number To Display: 1
    Information is shown for employee#1
    ..............
    From above all results as you see that and also I conclude that my program shows saved record only in sequence whatever number I enter first but I shows that in the sequence in which I have saved it.
    Code:
    .....................
    void employee::dis(){
         clrscr();
         ifstream fptr;
         int numberToshow;
         fptr.open("payroll.dat",ios::in);
         employee e;
         while(fptr.read((char *)&e,sizeof(employee))){    //read file
         cout<<"\nEnter employee number To Display: ";
         scanf("%d",&numberToshow);
         if(e.e_no==numberToshow)
             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();
    }
    On the run the OUTPUT is:
    Code:
    Enter employee number To Display: 7
    Information is shown for employee#6
    ...............
    Enter employee number To Display: 8
    Information is shown for employee#5
    ...............
    Enter employee number To Display: 3
    Information is shown for employee#1
    ...............
    As you see if I am entering wrong employee number that I have never used to any record it then still it shows saved information in a sequence.
    Now please sir tell me what should I do?
    I hope you could understand my problem.
    You can help me.
     
  10. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    it should be enclosed in { and }
    if(e.e_no==numberToshow) {
    ........
    cout<<"\nNet Paid :\t"<<e.basic+e.allowence-e.deduction;
    }
     
    shabbir likes this.
  11. Shayaan_Mustafa

    Shayaan_Mustafa New Member

    Joined:
    Dec 25, 2010
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Still a student.
    Location:
    Karachi
    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.
     
  12. lincy

    lincy New Member

    Joined:
    Jan 7, 2011
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.keralatourpackage.org
    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 a moderator: Jan 10, 2011
  13. Shayaan_Mustafa

    Shayaan_Mustafa New Member

    Joined:
    Dec 25, 2010
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Still a student.
    Location:
    Karachi
    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.
     
  14. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    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.
     
  15. Shayaan_Mustafa

    Shayaan_Mustafa New Member

    Joined:
    Dec 25, 2010
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Still a student.
    Location:
    Karachi
    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.
     
  16. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    it is located inside the folder where your source code and executable files are.


    p.s. you can always use windows search!
     
  17. Shayaan_Mustafa

    Shayaan_Mustafa New Member

    Joined:
    Dec 25, 2010
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Still a student.
    Location:
    Karachi
    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.
     
  18. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    just re-enter them
     
  19. Shayaan_Mustafa

    Shayaan_Mustafa New Member

    Joined:
    Dec 25, 2010
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Still a student.
    Location:
    Karachi
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice