\\\\\\\\\\ PRACTCE 1 ||||||||||||
#include <iostream>
#include <string>
#include <fstream>
using namespace std ;
class Student
{
private:
string name ;
int age ;
string reg ;
public:
// Default Constructor
Student()
{
name = "" ;
age = 0 ;
reg = "" ;
}
// Constructor
Student(string n, int a, string r)
{
name = n ;
age = a ;
reg = r ;
}
// Setter
void setter(string n, int a, string r)
{
name = n ;
age = a ;
reg = r ;
}
// Display
void display()
{
cout << "Name: " << name << endl ;
cout << "Age: " << age << endl ;
cout << "Reg: " << reg << endl ;
cout << endl ;
}
// Write
void writeToFile()
{
ofstream theFile ;
[Link]("[Link]", ios::app) ;
if (!theFile)
{
cout << "Error Opening File" << endl ;
}
else
{
theFile << name << endl ;
theFile << age << endl ;
theFile << reg << endl ;
cout << "Data Written To File!" << endl << endl ;
}
[Link]() ;
}
} ;
// Menu
char menu()
{
char choice = '0' ;
cout << "MENU" << endl ;
cout << "0. Exit" << endl ;
cout << "1. Write" << endl ;
cout << "2. Read" << endl ;
cout << "Select: " ;
cin >> choice ;
cout << endl ;
return choice ;
}
// Search Menu
char searchMenu()
{
char choice = '0' ;
cout << "SEARCH MENU" << endl ;
cout << "1. Search By Name" << endl ;
cout << "2. Search By Age" << endl ;
cout << "3. Search By Reg" << endl ;
cout << "Select: " ;
cin >> choice ;
cout << endl ;
return choice ;
}
// Get Data
void getData(Student& x)
{
string n = "" ;
int a = 0 ;
string r = "" ;
cout << "ENTRY" << endl ;
cout << "Enter The Name: " ;
cin >> n ;
cout << "Enter Age: " ;
cin >> a ;
cout << "Enter Registration No: " ;
cin >> r ;
cout << endl ;
[Link](n, a, r) ;
}
// Read
void readFromFile()
{
ifstream theFile ;
[Link]("[Link]", ios::in) ;
if (!theFile)
{
cout << "Error Opening File" << endl ;
}
else
{
// Variables
int count = 0 ;
string n[5] ;
int a[5] ;
string r[5] ;
// Read From File
while (![Link]())
{
theFile >> n[count] ;
theFile >> a[count] ;
theFile >> r[count] ;
count++ ;
}
// Searching
switch (searchMenu())
{
case '0':
break ;
case '1':
{
string name ;
cout << "Enter Name To Search: " ;
cin >> name ;
for (int i = 0; i < 5; i++)
{
if (name == n[i])
{
cout << endl << "STUDENT:" << endl ;
cout << "Name: " << n[i] << endl ;
cout << "Age: " << a[i] << endl ;
cout << "Reg: " << r[i] << endl ;
}
}
break ;
}
case '2':
{
int age ;
cout << "Enter Age To Search: " ;
cin >> age ;
for (int i = 0; i < 5; i++)
{
if (age == a[i])
{
cout << endl << "STUDENT:" << endl ;
cout << "Name: " << n[i] << endl ;
cout << "Age: " << a[i] << endl ;
cout << "Reg: " << r[i] << endl ;
}
}
break ;
}
case '3':
{
string reg ;
cout << "Enter Reg To Search: " ;
cin >> reg ;
for (int i = 0; i < 5; i++)
{
if (reg == r[i])
{
cout << endl << "STUDENT:" << endl ;
cout << "Name: " << n[i] << endl ;
cout << "Age: " << a[i] << endl ;
cout << "Reg: " << r[i] << endl ;
}
}
break ;
}
default:
{
cout << "Invalid Input!" << endl ;
break ;
}
}
}
cout << endl ;
[Link]() ;
}
int main()
{
Student x[5] ;
char choice = '0' ;
do
{
choice = menu() ;
switch (choice)
{
case '0':
cout << "Thank You!" << endl ;
break ;
case '1':
for (int i = 0; i < 5; i++)
{
getData(x[i]) ;
x[i].writeToFile() ;
}
break ;
case '2':
readFromFile() ;
break ;
default:
cout << "Invalid Input!" << endl ;
break ;
}
} while(choice != '0') ;
return 0 ;
}
\\\\\\\\\\\\\\\\\ practice 2 \\\\\\\\\\\\\\\\\\\\
#include <iostream>
#include <string>
#include <fstream>
using namespace std ;
class Student
{
private:
string name ;
int age ;
string reg ;
public:
// Default Constructor
Student()
{
name = "" ;
age = 0 ;
reg = "" ;
}
// Constructor
Student(string n, int a, string r)
{
name = n ;
age = a ;
reg = r ;
}
// Setter
void setter(string n, int a, string r)
{
name = n ;
age = a ;
reg = r ;
}
// Display
void display()
{
cout << "Name: " << name << endl ;
cout << "Age: " << age << endl ;
cout << "Reg: " << reg << endl ;
cout << endl ;
}
// Write
void writeToFile()
{
ofstream theFile ;
[Link]("[Link]", ios::app) ;
if (!theFile)
{
cout << "Error Opening File" << endl ;
}
else
{
theFile << name << endl ;
theFile << age << endl ;
theFile << reg << endl ;
cout << "Data Written To File!" << endl << endl ;
}
[Link]() ;
}
} ;
// Menu
char menu()
{
char choice = '0' ;
cout << "MENU" << endl ;
cout << "0. Exit" << endl ;
cout << "1. Write" << endl ;
cout << "2. Read" << endl ;
cout << "Select: " ;
cin >> choice ;
cout << endl ;
return choice ;
}
// Search Menu
char searchMenu()
{
char choice = '0' ;
cout << "SEARCH MENU" << endl ;
cout << "1. Search By Name" << endl ;
cout << "2. Search By Age" << endl ;
cout << "3. Search By Reg" << endl ;
cout << "Select: " ;
cin >> choice ;
cout << endl ;
return choice ;
}
// Get Data
void getData(Student& x)
{
string n = "" ;
int a = 0 ;
string r = "" ;
cout << "ENTRY" << endl ;
cout << "Enter The Name: " ;
cin >> n ;
cout << "Enter Age: " ;
cin >> a ;
cout << "Enter Registration No: " ;
cin >> r ;
cout << endl ;
[Link](n, a, r) ;
}
// Read
void readFromFile()
{
ifstream theFile ;
[Link]("[Link]", ios::in) ;
if (!theFile)
{
cout << "Error Opening File" << endl ;
}
else
{
// Variables
int count = 0 ;
string n[5] ;
int a[5] ;
string r[5] ;
// Read From File
while (![Link]())
{
theFile >> n[count] ;
theFile >> a[count] ;
theFile >> r[count] ;
count++ ;
}
// Searching
switch (searchMenu())
{
case '0':
break ;
case '1':
{
string name ;
cout << "Enter Name To Search: " ;
cin >> name ;
for (int i = 0; i < 5; i++)
{
if (name == n[i])
{
cout << endl << "STUDENT:" << endl ;
cout << "Name: " << n[i] << endl ;
cout << "Age: " << a[i] << endl ;
cout << "Reg: " << r[i] << endl ;
}
}
break ;
}
case '2':
{
int age ;
cout << "Enter Age To Search: " ;
cin >> age ;
for (int i = 0; i < 5; i++)
{
if (age == a[i])
{
cout << endl << "STUDENT:" << endl ;
cout << "Name: " << n[i] << endl ;
cout << "Age: " << a[i] << endl ;
cout << "Reg: " << r[i] << endl ;
}
}
break ;
}
case '3':
{
string reg ;
cout << "Enter Reg To Search: " ;
cin >> reg ;
for (int i = 0; i < 5; i++)
{
if (reg == r[i])
{
cout << endl << "STUDENT:" << endl ;
cout << "Name: " << n[i] << endl ;
cout << "Age: " << a[i] << endl ;
cout << "Reg: " << r[i] << endl ;
}
}
break ;
}
default:
{
cout << "Invalid Input!" << endl ;
break ;
}
}
}
cout << endl ;
[Link]() ;
}
int main()
{
Student x[5] ;
char choice = '0' ;
do
{
choice = menu() ;
switch (choice)
{
case '0':
cout << "Thank You!" << endl ;
break ;
case '1':
for (int i = 0; i < 5; i++)
{
getData(x[i]) ;
x[i].writeToFile() ;
}
break ;
case '2':
readFromFile() ;
break ;
default:
cout << "Invalid Input!" << endl ;
break ;
}
} while(choice != '0') ;
return 0 ;
}