CS201 Introduction to Programming Assignment No. 03 Solution Spring 2013

Problem Statement:     

Write the C++ program having class name studentinfo, that is used to store the VU student information: This class should store your VUID, campus id, name and father name in the following character type data members:

 

Data members:

  • VUID
  • campusID
  • Studentname
  • Fathername

 

Studentinfo class should have the parameterized constructor that is used to initialize the data members of the studentinfo class,

 

Studentinfo class should have following member functions.

  • Storefile()
  • Display()

 

Storefile() member function should store the value of all data members in the text file named “record.txt”, on separate lines in the text file as follows:  

 

ms120400400

Vlhr02

Muhammad Hussain

Muhammad Ali.

 

Display() member function reads your VUID, campusID, name and father name from the file named “record.txt”, stores them in the respective data members and displays the record.

 

Hint: 

In main() function, create the object of the class by using parameterized constructor and then call the storefile() member function to store the record of all the data members in the text file. Finally call the display() function to read the data from text file and store it in the respective data members and display all the records.
Solution:

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class Studentinfo
{
private:
char ID[50];
char campus[50];
char name[50];
char Father[50];
public:
Studentinfo()
{
ID[50] = '';
campus[50] = '';
name[50]= '';
Father[50] = '';
}
Studentinfo(char id[50] ,char c[50] ,char nam[50], char fthr[50])
{
ID[50] = id[50];
campus[50] = c[50];
name[50] = nam[50];
Father[50] = fthr[50];
}

void in()
{
cout"Enter your Vu ID ";
cin>>ID;
cout"Enter your Campus ID: ";
cin>>campus;
cout"Enter your Name: ";
cin>>name;
cout"Enter your Father Name: ";
cin>>Father;
}

void Storefile()
{
cout"Al the Data members are stored in file n";
ofstream file("D:\test.txt");
if(!file)
{
cout"File opening error";
exit(1);
}
fileIDendlcampusendlnameendlFatherendl;
file.close();
}
void disply()
{
cout"M. Anil Kapoor nn";
cout"BC12301299nn";
cout"Following is your datann";
ifstream in("D:\record.txt");
if(!in)
{
cout"Error in opening file ";
exit(1);
}
in>>ID;
in>>campus;
in>>name;
in>>Father;
cout"VU ID: "IDendl;
cout"campusID: "campusendl;
cout"Name: "nameendl;
cout"Fathername: "Fatherendl;
in.close();
}

};
main()
{
Studentinfo obj;
obj.in();
obj.Storefile();
obj.disply();
system("pause");
getch();

}


DOWNLOAD SOLUTION HERE
loading...