Answer to Question #298707 in C++ for mak

Question #298707

Student information Window




The data should be saved in a file.


The student record may be deleted.


The student record may be searched based on the ID provided.


The student record may be updated in case if its phone no or house address changes.


1
Expert's answer
2022-02-16T16:32:45-0500
#include <iostream>
#include <vector>
#include <fstream>
int next_id = 0;
class Student
{
public:
	Student();
	~Student();


	int id=0;
	std::string name="";
	std::string surname="";
	std::string city="";
	std::string street="";
	std::string phone_number = "";
	int building = 0;
	int house = 0;


	void input_info()
	{
		std::cout << "Enter name : ";
		std::cin >> name;
		std::cout << "Enter surname : ";
		std::cin >> surname;
		std::cout << "Enter city : ";
		std::cin >> city;
		std::cout << "Enter street : ";
		std::cin >> street;
		std::cout << "Enter building : ";
		std::cin >> building;
		std::cout << "Enter house : ";
		std::cin >> house;
		std::cout << "Enter phone number : ";
		std::cin >> phone_number;
	}
	void output_info()
	{
		std::cout << id << "." << name << " " << surname << " , " << city << " " << " " << building << " " << house << " , " << phone_number << std::endl;
	}
};


Student::Student()
{
	id = next_id;
	next_id++;
}


Student::~Student()
{
}


void save_to_file(std::vector<Student> & students, std::string file_name)
{
	std::ofstream file(file_name);
	file << students.size() << '\n';
	for (auto&& it : students)
	{
		file << it.id << " " << it.name << " " << it.surname << " " << it.phone_number << " " << it.city << " " << it.street << " " << it.house << " " << it.building << " " << it.house << "\n";
	}
	file.close();
}
void load_from_file(std::vector<Student>& students, std::string file_name)
{
	std::ifstream file(file_name);
	Student tmp;
	int size = 0;
	file >> size;
	students.resize(size);
	for (int i=0;i<size;i++)
	{
		file >> tmp.id >> tmp.name >>  tmp.surname >>  tmp.phone_number >>  tmp.city >>  tmp.street >>  tmp.house >>  tmp.building >>  tmp.house;
		students[i] = tmp;
	}
	
	file.close();
}


int main() 
{
	std::vector<Student> students;
	load_from_file(students, "students.txt");
	int answer = 0;
	while (1)
	{
		answer = 0;
		std::cout << "1.Add student\n2.Update student\n3.Save info\n4.Print students\n5.Exit\nEnter value : ";
		std::cin >> answer;
		if (answer == 1)
		{
			Student tmp;
			tmp.input_info();
			students.push_back(tmp);
		}
		else if (answer == 2)
		{
			bool is_found = 0;
			Student tmp;
			int id = 0;
			std::cout << "Enter id : ";
			std::cin >> id;
			for (auto&& it : students)
			{
				if (it.id == id)
				{
					is_found = 1;
					tmp.input_info();
					it = tmp;
					break;
				}
			}
			if (!is_found)
			{
				std::cout << "There is no student with such id\n";
			}
		}
		else if (answer == 3)
		{
			save_to_file(students, "students.txt");
			std::cout << "Saved\n";
		}
		else if (answer == 4)
		{
			for (auto&& it : students)
			{
				it.output_info();
			}
		}
		else if (answer == 5)
		{
			exit(0);
		}
	}


	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