Thread: Weirdest Problem i've ever encountered!Please help me

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    Weirdest Problem i've ever encountered!Please help me

    Here's the code and the problem is described below

    Code:
    #include <fstream.h>
    #include <string.h>
    #include <iomanip.h>
    
    class emp
    {
     char eno[4];
     char ename[30];
     float salary;
     fstream f1;
     public:
    	emp()
    	{
    		strcpy(eno,NULL);
    		strcpy(ename,NULL);
    		salary=0;
    		f1.open("d:\\exam.txt",ios::in | ios::out |ios::ate);
    	}
    	friend istream & operator>>(istream &din,emp &a);
    	friend ostream & operator<<(ostream &dout,emp &a);
    };
    ostream &operator<<(ostream &dout,emp &a)
    {
     a.f1.seekg(0,ios::beg);
     cout<<"\n\n==================================================================";
     cout<<"\n\nEmployee File Contains";
     cout<<"\n\n==================================================================\n";
     cout<<"Employee No\t\tEmployee Name\t\tSalary";
     cout<<"\n\n==================================================================\n";
    
     while(a.f1>>a.eno>>a.ename>>a.salary)
    	cout<<a.eno<<"\t\t\t"<<a.ename<<"\t\t\t"<<a.salary<<endl;
     a.f1.clear();
     return dout;
    }
    istream &operator>>(istream &din,emp &a)
    {
     a.f1.seekp(0,ios::end);
     a.f1.seekg(0,ios::beg);
     char eno[4];
     int t=0;
     cout<<"\n\nEnter Employee Number :";
     din>>a.eno;
     while(a.f1>>eno)
     {
      if(strcmpi(eno,a.eno)==0)
      {
    	cout<<"\n\nDuplicate Record";
    	t=1;
    	break;
      }
     }
     if(t==0)
     {
    	cout<<"\n\nEnter Employee Name :";
    	din>>a.ename;
    	cout<<"\n\nEnter Salary :";
    	din>>a.salary;
    	a.f1<<a.eno<<" "<<a.ename<<" "<<a.salary<<"\n";
                a.f1.clear();
    
     }
     return din;
    }
    
    int main(void)
    {
    emp e;
    int ch;
    do
    {
    
     cout<<"\n\nEmployee Management System";
     cout<<"\n\n1 :Add Record";
     cout<<"\n\n2 :Edit Record";
     cout<<"\n\n3 :Delete Record";
     cout<<"\n\n4 :Display Records";
     cout<<"\n\n5 :Exit";
     cout<<"\n\nEnter Your Choice :";
     cin>>ch;
     switch(ch)
     {
    	case 1:
    		cin>>e;
    	break;
    	case 2:
    	break;
    	case 3:
    	break;
    	case 4:
    		cout<<e;
    	break;
    	case 5:
    		cout<<"\n\nBye-Bye";
    	break;
    	default:
    		cout<<"\n\nWrong Choice";
     }
    }while(ch!=5);
     return 0;
    }
    kindly see the overloaded operator >>. I've tried to debug it but i can't trace the error. For some reason the input stream gets corrupted. Kindly tell me what's the problem behind that. The loop that's for duplicate record checking, that is creating problem. Please explain me why this problem is coming?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The code is full of errors.
    First and foremost, there are no headers as fstream.h and iomanip.h. They are named fstream and iomanip respectively.
    And you've forgotten to include iostream.
    Your indentation is pretty poor. You're going to have to work on that.

    Code:
    		strcpy(eno,NULL);
    		strcpy(ename,NULL);
    Never pass NULL to either strcpy, strcat or any other string function. You shouldn't be using them anyway. You should be using std::string.

    You also do not seem to understand the essence of the class. The class should not ask for the data. It is the object which represents the data, so your main function should ask for the data and save it into the class. The operator >> should only write the data to the file.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I suppose the idea of operator>> and operator<< is to move data into and out of the class.

    Example usage:
    Code:
    vector<emp> v;
    ifstream fin("exams.txt");
    emp e;
    
    //load from file
    while (fin >> e) {
        v.push_back(e);
    }
    
    //display in console
    for (unsigned i = 0; i != v.size(); ++i) {
        std::cout << v[i] << '\n';
    }
    etc...
    That is, the class shouldn't attempt to read data internally.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Elysia try the code first !!
    iostream should not be included as fstream is inherited from iostream only!!

    So for your kind information when fstream is included there's no need to include iostream

    http://www.cplusplus.com/reference/iostream/fstream/

    Secondly do you mean to say!!if i have 20 members in class i should take input of 20 members in main 1 by 1??and then write a function which takes 20 arguments and copies 1 by 1 to class members?!!!??? That's pretty funny isn't it?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by chottachatri View Post
    Elysia try the code first !!
    I did and it did not compile.

    iostream should not be included as fstream is inherited from iostream only!!
    So for your kind information when fstream is included there's no need to include iostream
    That is irrelevant. If you use something defined in the iostream header, then include it. It did not compile until I included the iostream header, which tells us a lot. Do it.

    Secondly do you mean to say!!if i have 20 members in class i should take input of 20 members in main 1 by 1??and then write a function which takes 20 arguments and copies 1 by 1 to class members?!!!??? That's pretty funny isn't it?
    The code will be the same, in or outside the class.
    The class represents the data, it is the essence of the data, it does not ask FOR data. If it does, then you have the concept of classes wrong.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    iostream should not be included as fstream is inherited from iostream only!!

    So for your kind information when fstream is included there's no need to include iostream
    So this should work?
    Code:
    #include <fstream>
    
    int main()
    {
        std::cout << "Hello";
    }
    But it doesn't (at least not with all compilers). Include what you need to and don't rely on implementation specific details, such as <iostream> would pull in <string> etc.

    Secondly do you mean to say!!if i have 20 members in class i should take input of 20 members in main 1 by 1??and then write a function which takes 20 arguments and copies 1 by 1 to class members?!!!??? That's pretty funny isn't it?
    You should probably overload operators << and >> to do something sane, so that it would support the example usage I posted.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Secondly, Anon you are right a class must not attempt to read data internally in that case i would have to declare fstream object in main() and pass it to overloaded >> which is also obviously not correct since overloaded >> can take only 2 arguments (in friend function) which means i would have to declare fstream object globally which is dont want!!
    so can you tell me some other way of doing it?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, you don't. Operator >> takes the left-hand side object, which is your class (object) and a right-hand side object (which is the file stream).
    Whether the operator is global or not is irrelevant. The object does not need to be global. You are calling operator >> correctly.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    well, fstream is inherited from iostream itself and yes the whole program that i wrote compiles and works correctly(syntactically)

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For YOU maybe, but not for ME and maybe not for OTHERS, because you are relying on something that is not guaranteed.
    Include ALL headers for ALL objects/functions you use.

    The standard says that if you want to use cout, you must include iostream. And if you don't, it will work on some compilers, but break on others.
    So NO, it is NOT correct.
    Fix your headers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    The standard says fstream is inherited from iostream therefore you must include either iostream or fstream since derived class can access base class members (if they are protected or public)

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    chottachatri, you're confusing headers and classes.

    basic_fstream is inherited from basic_iostream, that's true. But that doesn't mean that including <fstream> gets you the entire content of <iostream>. In particular, you will not get std::cin, std::cout and friends on many compilers.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, the <iostream> header needn't contain much more than the declarations of the global streams:
    Code:
      extern istream cin;		///< Linked to standard input
      extern ostream cout;		///< Linked to standard output
      extern ostream cerr;		///< Linked to standard error (unbuffered)
      extern ostream clog;		///< Linked to standard error (buffered)
    The classes istream and ostream are implemented in some other headers (<istream> and <ostream>).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM