Answer to Question #201222 in C++ for kaushal

Question #201222

Create student.dat, which contains details like

       Roll number, name, age, address

Create a menu driven program to perform the following


1. Create file     

2. Read and display records  

3.  Add new records 

4. Modify a record

5. Delete a record

6.  Search for a record

7.  Exit


1
Expert's answer
2021-05-31T12:48:57-0400
The file "student.dat "

S15643131,Mike Clark,18, Main Road 15
S48465456,Mary Smith,19, Main Road 12
S48465456,Peter Willams,18, Main Road 11



C++ code:


#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>
#include <sstream>


using namespace std;


class Student{
private:
	string rollNumber;
	string name;
	int age;
	string address;
public:
	Student(){}


	Student(string rollNumber,string name,int age,string address){
		this->rollNumber=rollNumber;
		this->name=name;
		this->age=age;
		this->address=address;
	}
	string getRollNumber(){
		return this->rollNumber;
	}
	string getName(){
		return this->name;
	}
	int getAge(){
		return this->age;
	}
	string getAddress(){
		return this->address;
	}
};
const string FILE_NAME="student.dat";


void readStudentsFromFile(vector<Student>* students){
	if(students->size()==0){
		//Open the file
		ifstream ifstreamStudents;
		ifstreamStudents.open(FILE_NAME);
		string line;
		while (!ifstreamStudents.eof()){






			getline(ifstreamStudents,line);
			if(line!=""){
				stringstream ss(line);
				while(ss.good())
				{
					//split string by comma delimited
					string rollNumber;
					getline(ss, rollNumber,',');
					string name;
					getline(ss, name,',');
					string ageString;
					getline(ss, ageString,',');
					string address;
					getline(ss, address,',');
					students->push_back(Student(rollNumber,name,stoi(ageString), address));
				}
			}
		}
		//close files stream
		ifstreamStudents.close();
	}
}


void saveStudentsToFile(vector<Student> students){
	ofstream ofstreamStudents;
	ofstreamStudents.open(FILE_NAME);
	for(int i=0;i<students.size();i++){
		ofstreamStudents<<students[i].getRollNumber() << "," << students[i].getName()<< "," <<
			students[i].getAge()<< "," <<students[i].getAddress()<< "\n";	
	}
	ofstreamStudents.close();
}
int getStudentIndexByRollNumber(vector<Student> students,string rollNumber){
	int index=-1;
	for(int i=0;i<students.size();i++){
		if(students[i].getRollNumber()==rollNumber){
			return i;
		}
	}
	return -1;
}


int main(){
	vector<Student> students;


	int ch=-1;
	while(ch!=7){
		cout<<"1. Create file\n";
		cout<<"2. Read and display records\n"; 
		cout<<"3. Add new record\n";
		cout<<"4. Modify a record\n";
		cout<<"5. Delete a record\n";
		cout<<"6. Search for a record\n";
		cout<<"7. Exit\n";
		cout<<"> ";
		cin>>ch;
		if(ch==1){
			ofstream ofstreamStudents;
			ofstreamStudents.open(FILE_NAME);
			ofstreamStudents.close();
			cout<<"\nThe file \"student.dat\" has been created.\n\n";
		}else if(ch==2){
			readStudentsFromFile(&students);
			cout << setw(25)<< "Roll number" << setw(25) << "Name"<< setw(25) << "Age"<< setw(25) << "Address\n";
			for(int i=0;i<students.size();i++){
				cout<< setw(25)<<students[i].getRollNumber() << setw(25) << students[i].getName()<< setw(25) <<
					students[i].getAge()<< setw(25) <<students[i].getAddress()<< "\n";	
			}
			cout<<"\n\n";


		}else if(ch==3){
			string rollNumber;
			cout<<"Enter the roll number: ";
			getline(cin, rollNumber);
			getline(cin, rollNumber);
			string name;
			cout<<"Enter the name: ";
			getline(cin, name);
			int age;
			cout<<"Enter the age: ";
			cin>>age;
			string address;
			cout<<"Enter the address: ";
			getline(cin, address);
			getline(cin, address);
			students.push_back(Student(rollNumber,name,age, address));
			saveStudentsToFile(students);
			cout<<"\n\nA new student has been added.\n\n";
		}else if(ch==4){
			string rollNumber;
			cout<<"Enter the student's roll number to update: ";
			getline(cin, rollNumber);
			getline(cin, rollNumber);
			int index=getStudentIndexByRollNumber(students,rollNumber);
			if(index!=-1){
				string name;
				cout<<"Enter a new name: ";
				getline(cin, name);
				int age;
				cout<<"Enter a new age: ";
				cin>>age;
				string address;
				cout<<"Enter a new address: ";
				getline(cin, address);
				getline(cin, address);
				students[index]=Student(rollNumber,name,age, address);
				saveStudentsToFile(students);
				cout<<"\n\nThe student has been updated.\n\n";
			}else{
				cout<<"The student's roll number "<<rollNumber<<" does not exist.\n";
			}


		}else if(ch==5){
			string rollNumber;
			cout<<"Enter the student's roll number to delete: ";
			getline(cin, rollNumber);
			getline(cin, rollNumber);
			int index=getStudentIndexByRollNumber(students,rollNumber);
			if(index!=-1){
				students.erase(students.begin()+index);
				saveStudentsToFile(students);
				cout<<"\n\nThe student has been deleted.\n\n";
			}else{
				cout<<"The student's roll number "<<rollNumber<<" does not exist.\n";
			}
		}else if(ch==6){
			string rollNumber;
			cout<<"Enter the student's roll number to search: ";
			getline(cin, rollNumber);
			getline(cin, rollNumber);
			int index=getStudentIndexByRollNumber(students,rollNumber);
			if(index!=-1){
				cout << setw(25)<< "Roll number" << setw(25) << "Name"<< setw(25) << "Age"<< setw(25) << "Address\n";
				cout<< setw(25)<<students[index].getRollNumber() << setw(25) << students[index].getName()<< setw(25) <<
					students[index].getAge()<< setw(25) <<students[index].getAddress()<< "\n";	
			}else{
				cout<<"The student's roll number "<<rollNumber<<" does not exist.\n";
			}
		}else if(ch==7){
			//exit
		}else{
			cout<<"\nWrong menu item.\n";
		}
	}


	system("pause");
	return 0;
}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog