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

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()
{
              ofstream re;
              re.open("record.txt");
              if(!re)/*Error Checking Mechanism*/
                  {
                         cout<<"Error Reading File"<<endl;
                  }
              re<<VUID<<endl<<campusID<<endl<<Studentname<<endl<<Fathername<<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("mc123456789", "PMMK08", "xxxx xxxxx xxxxx","xxxx xxxxxx xxxxxx");/*Object Created and Initialized by constructor calling*/
      s1.Storefile();/*Function Call*/
      s1.Display();/*Function Call*/

      system("pause");
}

Recommended Answers

All 5 Replies

You never set the values VUID campusID Studentname Fathername in the student object when you create it.

While I am here, I would be remiss if I did not point out that you are using mutant pre-standard C++ from about twenty years ago. Since 1998, we have used namespaces, C++ header files do not end with .h, main has always ALWAYS returned an int. Your code will not compile on a C++ compiler from 1998 or later.

Yup, as Moschops has said, it's because your constructor is not initialising anything. Which is what I said in my post in your other thread. You are taking in the four parameters, but you still aren't doing anything with them. You need to use the passed-in parameters to initialise the data members of your class.
Like this:

studentinfo::studentinfo(char* vuid, char* campus_id, char* student_name, char* father_name)
: VUID(vuid),campusID(campus_id), Studentname(student_name), Fathername(father_name) 
{
    cout<<"Parameterized Constructor is Called"<<endl<<endl;
}

Note: The above uses an initialiser list to initialise all of the member variables for the class.
If your compiler does not support using initialiser lists, you could use this instead:

studentinfo::studentinfo(char* vuid, char* campus_id, char* student_name, char* father_name) 
{
    VUID = vuid;
    campusID = campus_id;
    Studentname = student_name;
    Fathername = father_name;

    cout<<"Parameterized Constructor is Called"<<endl<<endl;
}

Note: The code for both of the above versions of the constructor makes me a little twitchy/uncomfortable. Whilst you are using string literals, it's ok to just assign the pointer to point to the string. But if you plan to start getting input from a user to populate the data members of your class, you're going to have to deal with things a little differently. It's been a long while since I did anything with low-level C-style strings in C++ (and even longer since I've used C). I've been using std::string and the other std::library container classes in C++ for so long now, I'm more than a little rusty on this particular topic! I think I need to take a look back at some of my old notes on C from way back when. Heh heh, looks like I need to revisit and relearn what I seem to have forgotten! Namely C! :/

Anyway, this being Daniweb; I'm sure somebody else will chip in and tell you how to best use the C-style strings in your code. It will almost certainly involve strcpy and a bit of dynamic memory management somewhere I expect!

A couple of other things: As mentioned in your other thread, your Display functions return type should be void because it is not returning anything.

And as Moschops has mentioned - the C++ standard clearly states that the return type of main should always be int. It would be good practice to stick with the standard as far as you can, even if the compiler you are using is antiquated/obsolete and pre-dates the standardisation of C++! The old Borland compilers support several non-standard signatures for main, but the standard int main() signature is also supported, so you should really use it! :)

Thank you very much for your help! I searched for string copy functions and got assign() function and it worked very well. :) how is it?

studentinfo::studentinfo(string vUID, string CampusID, string studentname, string fathername)
{
cout<<"Parameterized Contructor is Called"<<endl<<endl;
    VUID.assign(vUID);/*Assigning values to Data Member*/
    campusID.assign(CampusID);/*Assigning values to Data Member*/
    Studentname.assign(studentname);/*Assigning values to Data Member*/
    Fathername.assign(fathername);/*Assigning values to Data Member*/

}

If you're going to use std::strings then that's absolutely fine! :)

Sorry, when I saw your original code I assumed this was a homework exercise where you were restricted to using C style char arrays. Also I really couldn't remember what parts of the std::library are available in the older Borland compilers!

But if you aren't restricted to using C style strings, then yes; std::string would make far more sense to use if it is available to you! When it comes to C++, the standard library container classes and IO streams etc. should be preferred over their older C equivalents. They can save you from having to write and debug a lot of code and offer a lot of advantages over their C equivalents!

With std::strings you can also copy and/or assign strings directly using the = operator (also called the assignment operator). In other words, you can initialise std::strings in exactly the same way you'd initialise primitive types (bool,float, int etc).
So kinda like this:

studentinfo::studentinfo(string vUID, string CampusID, string studentname, string fathername)
{
    cout<<"Parameterized Contructor is Called"<<endl<<endl;
    VUID = vUID;/*Assigning values to Data Member*/
    campusID = CampusID;/*Assigning values to Data Member*/
    Studentname = studentname;/*Assigning values to Data Member*/
    Fathername = fathername;/*Assigning values to Data Member*/

}

It does exactly the same thing as your code, but it looks a little more natural and expresses what is going on more succinctly than using .assign()!

So, is this part of a homework thing as part of a course you are on? Or are you just studying C++ in your own time, on your own?

well, its really a homework assignment, but we are not restricted to anything. we had to do this through a class, doesn't matter which functions or variables we use. i used pointers, arrays and then ultimately strings worked. :)

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.