Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   C/C++(File handling related Problem) (http://www.go4expert.com/forums/c-cpp-file-handling-related-t24327/)

Shayaan_Mustafa 31Dec2010 06:23

C/C++(File handling related Problem)
 
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.

jimblumberg 31Dec2010 20:27

Re: C/C++(File handling related Problem)
 
When I compile your code I get the following warnings:

Quote:

main.cpp|16|warning: ISO C++ forbids declaration of ‘main’ with no type|
main.cpp||In function ‘int main()’:|
main.cpp|29|warning: ISO C++ forbids taking address of function ‘::main’|
main.cpp||In member function ‘void employee::add()’:|
main.cpp|67|warning: ISO C++ forbids taking address of function ‘::main’|
main.cpp||In member function ‘void employee::dis()’:|
main.cpp|86|warning: ISO C++ forbids taking address of function ‘::main’|
obj/Debug/main.o||In function `employee::add()':|
main.cpp|47|warning: the `gets' function is dangerous and should not be used.|
||=== Build finished: 0 errors, 5 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

Shayaan_Mustafa 31Dec2010 21:57

Re: C/C++(File handling related Problem)
 
Quote:

Originally Posted by jimblumberg (Post 76697)
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

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.

jimblumberg 31Dec2010 22:30

Re: C/C++(File handling related Problem)
 
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

kyle 1Jan2011 18:59

Re: C/C++(File handling related Problem)
 
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

virxen 1Jan2011 20:46

Re: C/C++(File handling related Problem)
 
try this

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.
};
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>>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";
    cout<<"Enter Another Record? "<<endl;
    cout<<"1->Yes | 2->No"<<endl;cin>>choice;
    }while(choice==1);

}
void employee::dis(){
    clrscr();
    ifstream fptr;
    fptr.open("payroll.dat",ios::in);

    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();
}


Shayaan_Mustafa 8Jan2011 18:03

Re: C/C++(File handling related Problem)
 
Quote:

To Virxen
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?

virxen 8Jan2011 18:31

Re: C/C++(File handling related Problem)
 
Code:


...........
if (e.e_no==numberToShow){
      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;
      break;
}

..........

numberToShow--->the employee number to show.
you can use scanf to get the number from user



Shayaan_Mustafa 9Jan2011 14:56

Re: C/C++(File handling related Problem)
 
Quote:

To Virxen
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.
Quote:

My 1st change to code:
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.
Quote:

My 2nd change to code:
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.

virxen 9Jan2011 17:36

Re: C/C++(File handling related Problem)
 
Quote:

Code:

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;


it should be enclosed in { and }
if(e.e_no==numberToshow) {
........
cout<<"\nNet Paid :\t"<<e.basic+e.allowence-e.deduction;
}


All times are GMT +5.5. The time now is 08:43.