Question #200834

Developed as a console application without GUI, this student database system project

uses file to store the students’ information mentioned in the features below

Add Records

Search Records

Modify Records

Delete Records


Expert's answer

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


using namespace std;


class Student
{
public:
	Student(string name) { this->name = name;	}
	string GetName() { return name; }
	void SetName(string name) { this->name = name; }
private:
	string name;
};


int main()
{
	int choice = 0;
	string temp;
	while (true)
	{
		cout << "*****Menu*****" << endl;
		cout << "1. Add Record." << endl;
		cout << "2. Search Records." << endl;
		cout << "3. Modify Records." << endl;
		cout << "4. Delete Records." << endl;
		cout << "5. Exit." << endl;
		cout << "Enter your choice: ";
		cin >> choice;
		if (choice == 5) break;
		switch (choice)
		{
		case 1:
		{
			cout << "Enter name of student: ";
			cin >> temp;
			ofstream fout;
			fout.open("data.txt", ios::app);
			fout << endl << temp;
			fout.close();
			break;
		}
		case 2:
		{
			cout << "Enter student name which you want to find: ";
			cin >> temp;
			ifstream fin;
			fin.open("data.txt");
			string temp2;
			int count = 0;
			while (!fin.eof())
			{
				fin >> temp2;
				if (temp2 == temp) count++;
			}
			cout << "Find " << count << " Records in file" << endl;
			fin.close();
			break;
		}
		case 3:
		{
			ifstream fin;
			fin.open("data.txt");
			cout << "Enter which student you want to modify: ";
			cin >> temp;
			cout << "Enter new name: ";
			string temp3;
			cin >> temp3;
			string temp2;
			vector<string> Students;
			while (!fin.eof())
			{
				fin >> temp2;
				Students.push_back(temp2);
				if (Students[Students.size() - 1] == temp) Students[Students.size() - 1] = temp3;
			}
			fin.close();
			ofstream fout;
			fout.open("data.txt");
			for (int i = 0; i < Students.size(); i++)
			{
				if (i == Students.size() - 1)
				{
					fout << Students[i];
					break;
				}
				fout << Students[i] << endl;
			}
			break;
		}
		case 4:
		{
			ifstream fin;
			fin.open("data.txt");
			cout << "Enter which student you want to delete: ";
			cin >> temp;
			string temp2;
			vector<string> Students;
			while (!fin.eof())
			{
				fin >> temp2;
				if (temp2 != temp) Students.push_back(temp2);
			}
			fin.close();
			ofstream fout;
			fout.open("data.txt");
			for (int i = 0; i < Students.size(); i++)
			{
				if (i == Students.size() - 1)
				{
					fout << Students[i];
					break;
				}
				fout << Students[i] << endl;
			}
			break;
		}


		default:
			cout << "Wrong choice!" << endl;
			break;
		}
	}
	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!

LATEST TUTORIALS
APPROVED BY CLIENTS