Kindly take look at this code, it is giving me an error in the main function.

#include<iostream.h>
#include<fstream.h>
#include<cstdlib>
#include<cstring>
ofstream re;
class studentinfo


    {


      private:
              char* VUID;
              char* campusID;
              char* Studentname;
              char* Fathername;

      public:
             void Storefile(char*, char*, char*, char*);
             char* Display() ;
             studentinfo(char*, char*, char*, char*);
             ~studentinfo();
             };
studentinfo::studentinfo(char* VUID, char* campusID, char* Studentname, char* Fathername)
{
VUID = "mc123456789";
campusID = "sdasdas";
Studentname = "xyxzy ayauu iausui";
Fathername = "sdas asdasd asdas";
cout<<"Contructor Called"<<endl;
}
studentinfo::~studentinfo()
{
cout<<"Destructor Called for destruction of the object"<<endl;
}
void studentinfo::Storefile(char* VUID, char* campusID, char* Studentname, char* Fathername)
{
              char* ID;
              char* camID;
              char* stdntnme;
              char* Fthrnme;
              re.open("record.txt");
              if(!re)
                  {
                         cout<<"Error Reading File"<<endl;
                  }
              re<<ID<<endl<<camID<<endl<<stdntnme<<endl<<Fthrnme<<endl;
              re.close();
}

char* studentinfo::Display()
{
              char* VUID;
              char* campusID;
              char* Studentname;
              char* Fathername;
              ifstream re;
              re.open("record.txt");
              if(!re)
                  {
                         cout<<"Error Reading File"<<endl;
                  }
              re>>VUID>>campusID>>Studentname>>Fathername;
              cout<<VUID<<endl<<campusID<<endl<<Studentname<<endl<<Fathername<<endl;
              re.close();
}



main()
{
      studentinfo std1;
      std1.Storefile();
      std1.Display();

}

Recommended Answers

All 3 Replies

The function Storefile takes four char* parameters. You're trying to call it with none.

If you had posted the exact errors you were getting from the compiler we could pinpoint problems more quickly!

As you haven't posted any error messages; other than what Moschops has spotted, I can see only one other thing that could cause the compiler to throw an error:

At line 72 you are relying on a default constructor when you instantiate your studentinfo class, but because you have already defined a non-default constructor, no default constructor will be defined by the compiler - At least, this would be the case in a modern compiler. Not sure if that is the case with your old Borland one.
wikipedia backs me up on this! :)

If some constructors are defined, but they are all non-default, the compiler will not implicitly define a default constructor. Hence, a default constructor may not exist for a class.

So line 72 could also be problematic!
If you are getting an error about no default constructor, you either need to create a default constructor yourself, or pass the constructor some parameters at line 72!

Other than that, there are a few other errors in your code. Not syntax errors that will cause compilation to fail. More like logical/conceptual errors really:

  1. Your existing constructor takes four parameters, yet uses none of them. Instead you are using string literals to initialise the members of the class. Perhaps remove the arguments and make this the default-no args constructor until you are ready to implement further functionality. Otherwise, use the passed-in parameters to initialise your class correctly and use the parameterised constructor at line 72, remembering to pass it some appropriate values!

  2. You don't need to pass any parameters to your Storefile function.
    Storefile is a member function of your class and has direct access to its data member variables, you can simply write out the data members directly. So you can remove the parameter list from this function!

  3. There is no need for the global ofstream object declared at line 5. It should be declared locally where it is used (in the Storefile function)

  4. In your Display function you don't need to redeclare the char* variables.
    Yet again, Display is a member function of the class and can directly access its member variables. You can simply read them directly from the file into the member variables.

  5. The signature of your Display function states that it should return a char*, but it does not return anything. This function should either return a char* or have it's signature changed to return void!

  6. Each instance of the class writes to and reads from a file called record.txt. And each time the Display function is called, the file record.txt will be completely overwritten with a single record.... So what are you going to do when/if you have several instances of the class and you want to save their details?

I think that's about it!
:)

JasonHippy, thanks for the help buddy, i am at very initial stages but i am learnng rapidly. :)

Now see this code, after posting the first code, i removed some errors and added some things in my code and program was executed properly. please take a look and tell me that if i can make this code more efficient or not and what more i lack in programming.

#include<iostream.h>
#include<fstream.h>

/* Making Global Pointer Variables to use anywhere in the program*/
             const char* ID =       "VUID          = mc130202398";
             const char* camID =    "Campus ID     = PMTN08"; 
             const char* stdntnme = "Name          = Rehan Shahzad Siddiqui";
             const char* Fthrnme =  "Father's Name = Rizwan Ali Siddiqui";
ofstream re;
class studentinfo
{
      private:/*Creating Private Data Members */
              char* VUID;
              char* campusID;
              char* Studentname;
              char* Fathername;

      public:
             void Storefile();/* Function to Store Data in the File*/
             char Display();/*Function to Read and then Display Data from the File*/
             studentinfo(char*, char*, char*, char*);/*Constructor to initialize Data Members*/
             ~studentinfo();
             };
/* Constructor Defined Here*/
studentinfo::studentinfo(char* VUID, char* campusID, char* Studentname, char* Fathername)
{
cout<<"Parameterized Contructor is Called"<<endl<<endl;
}
/*Destructor Defined Here*/
studentinfo::~studentinfo()
{
cout<<"Destructor Called for destruction of the object"<<endl;
system("pause");
}
/*Function to Store Data in the File Defined here*/
void studentinfo::Storefile()
{

              re.open("record.txt");
              if(!re)/*Error Checking Mechanism*/
                  {
                         cout<<"Error Reading File"<<endl;
                  }
              re<<ID<<endl<<camID<<endl<<stdntnme<<endl<<Fthrnme<<endl;/*Using Global Pointer Variables to Store data in the File*/
              cout<<"All the Data Members are Stored in a File"<<endl<<endl;
              re.close();
}
/*Function to Read and then Display the data in the File is definde here */              
char studentinfo::Display()
{
              char output[100];/*Array to store and display the data*/
              ifstream reh;
              reh.open("record.txt");
              if(!reh)
                  {
                         cout<<"Error Reading File"<<endl;
                  }

              cout<<"Following is My Data"<<endl<<endl;
              while(!reh.eof()){
              reh.getline(output, 100, '\n');/*Reading the data and storing it in the 'output' array line by line*/
              cout<<output<<endl;
              }

              reh.close();
}


/*Main Function starting here*/                  
main()
{
      studentinfo s1("mc130202398", "PMTN08", "Rehan Shahzad Siddiqui","Rizwan Ali Siddiqui");/*Object Created and Initialized by constructor calling*/
      s1.Storefile();/*Function Call*/
      s1.Display();/*Function Call*/
      system("pause");
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.