Sponsored links

Tuesday 25 June 2013

CS201 Introduction to Programming Assignment 3 Solution Spring 25th June 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:
//CS201 Introduction to Programming
//Assignment No. 03 Solution
//Spring 2013
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
class StudentInfo
{
char* vuId;
char* campusId;
char* studentName;
char* fatherName;
public:
StudentInfo(char* vu_Id, char* campus_Id, char* student_Name, char* father_Name)
{
cout “Parameterized constructor is called.” endl;
setVuId(vu_Id);
setCampusId(campus_Id);
setStudentName(student_Name);
setFatherName(father_Name);
}
void setVuId(char* vu_Id)
{
vuId = new char[strlen(vu_Id) + 1];
strcpy(vuId, vu_Id);
}
void setCampusId(char* campus_Id)
{
campusId = new char[strlen(campus_Id) + 1];
strcpy(campusId, campus_Id);
}
void setStudentName(char* student_Name)
{
studentName = new char[strlen(student_Name) + 1];
strcpy(studentName, student_Name);
}
void setFatherName(char* father_Name)
{
fatherName = new char[strlen(father_Name) + 1];
strcpy(fatherName, father_Name);
}
void storeFile()
{
cout “All the data members are stored in file.” endl;
ofstream outFile;
char* outputFileName = “record.txt”;
outFile.open(outputFileName, ios::out);
if(!outFile)
{
cout “\n Unable to open file.” endl;
}
else
{
outFile vuId;
outFile endl;
outFile campusId;
outFile endl;
outFile studentName;
outFile endl;
outFile fatherName;
}
}
void display()
{
ifstream inputStream;
char* fileName = “record.txt”;
inputStream.open(fileName);
if(!inputStream)
{
cout “\nCan’t Open the file for read”;
}
else
{
char line[100];
inputStream.getline(line, 100);
setVuId(line);
inputStream.getline(line, 100);
setCampusId(line);
inputStream.getline(line, 100);
setStudentName(line);
inputStream.getline(line, 100);
setFatherName(line);
cout “Following is your data:” endl;
cout vuId endl;
cout campusId endl;
cout studentName endl;
cout fatherName;
cout endl;
}
}
};
int main()
{

StudentInfo studentInfo(“ms120xxxx0″, “Vxx02″, “Mud sain”, “Muhammad Ali.”);
studentInfo.storeFile();
studentInfo.display();
system(“pause”);
return 0;
}

No comments:

Post a Comment