Answer to Question #334201 in C++ for saen

Question #334201

Write a C++ program which takes 5 students’ information (records) as inputs from the user and


save them on a file. Your program should have two options: writing and searching.



When a user selects writing option, you are required to take three inputs from the user: student


name (string type), student age (int type). After reading these data-items write to the file named “student.txt”. Please note: write data using append mode otherwise previous information will be lost.



Your program should have a search function. The search function should provide three options


for searching: by name, by age, or by registration number. After selection of the search option


by the user, please take input (name, age, or registration number) and search that student from the


file and display its record on the screen.


1
Expert's answer
2022-04-27T05:17:18-0400
#include <iostream>
#include <string>
#include <fstream>


using namespace std;


struct Student
{
	string name;
	int regNo;
	int age;
	Student(){}
	Student(string _name, int _regNo, int _age)
		:name(_name), regNo(_regNo), age(_age) {}
};


void Show()
{
	ifstream ifs("student2.txt");
	if (!ifs.is_open())
		cout << "File isn`t opened!";
	else
	{
		string line;
		while (getline(ifs, line, '\n'))
		{
			cout << line << endl;
		}
		ifs.close();
	}
}


void Search()
{
	string input;
	cout << "Please, take input (name, age, or registration number): ";
	cin >> input;
	ifstream ifs("student2.txt");
	if (!ifs.is_open())
		cout << "File isn`t opened!";
	else
	{
		string line;
		bool found = false;
		while (getline(ifs, line, '\n'))
		{
			if (line.find(input) != string::npos)
			{
				cout << line << endl;
				found = true;
				break;
			}
		}
		if (found == false)
			cout << input << " is not found!";
		ifs.close();
	}
}


void Write()
{
	ofstream of;
	of.open("student2.txt", ofstream::out | ofstream::app);
	if (!of.is_open())
		cout << "File isn`t opened!";
	else
	{
		Student tmp;
		cout << "Please, enter a name of student: ";
		cin >> tmp.name;
		cout << "Please, enter a regNo of student: ";
		cin >> tmp.regNo;
		cout << "Please, enter an age of student: ";
		cin >> tmp.age;
		of << tmp.name << " " << tmp.regNo << " "
			<< tmp.age << endl;
		of.close();
	}
	
}


void Menu()
{
	cout << "\nSelect some of the following: "
		<< "\n1 - Read file with students"
		<< "\n2 - Search data"
		<<"\n3 - Write data"
		<< "\n0 - Exit program"
		<< "\nMake a select: ";
}


int main()
{
	Student arrSt[5] = { Student("Andy",3,18),Student("Jack",8,19),
		Student("Any",2,20),Student("Andrew",1,20),Student("Paula",9,20) };
	ofstream of;
	of.open("student2.txt",ofstream::out|ofstream::app);
	if (!of.is_open())
		cout << "File isn`t opened!";
	else
	{
		for (int i = 0; i < 5; i++)
		{
			of << arrSt[i].name << " " << arrSt[i].regNo << " "
				<< arrSt[i].age << endl;
		}
	}
	of.close();
	char choice;
	do
	{
		Menu();
		cin >> choice;
		switch (choice)
		{
			case '1':
			{
				Show();
				break;
			}
			case '2':
			{
				Search();
				break;
			}
			case '3':
			{
				Write();
				break;
			}
		}
	} while (choice != '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
APPROVED BY CLIENTS