Lab Report

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

INSTITUTE OF SPACE TECHNOLOGY

ISLAMABAD

Electrical Engineering Department


Programming Language Lab
Batch-19-Section-B
Lab Report # 6

Submitted To:
Sir Adnan Zafar
Submitted By:
M Saad Javed
Registration No:
200401013
Lab Task:
Task No 1: File Search using Applicant Name.
Write a program that lists all applicants with a specific name and display the list on the screen.
The program will take the character array of the specific name from the user and use searching
for substring function i.e., strstr to find the entries with required name and list the entries with
the specific name along with the total number of entries found in the search.
Write a program that lists all applicants with a specific name and display the list on the screen.
The program will take the character array of the specific name from the user and use searching
for substring function i.e., strstr to find the entries with required name and list the entries with
the specific name along with the total number of entries found in the search.

Pseudocode:

Step 1 Use input output stream and file stream header file.
Step 2 Initialize characters which will store the data in information
file.
Step 3 Ask user to input his choice of read, write or find.
Step 4 Use if statement to complete that task.
Step 5 Opening file in which date will be stored using if stream.
Step 6 Using jump statement to get back to the initial position.
Step 7 Initializing character arrays to store information with arrays.
Step 8 Asking user to input his/her data.
Step 9 Using of stream statement for printing the lines that are
stored in department file.
Step 10 Printing option counting using while loop.
Step 11 Asking user to input his choice of department.
Step 12 If option 3 is selected then the function that has been made in
the main will pass and find the entities that’s been stored in the
file.
Step 13 If option 4 is selected then function will tell that this name is
present how many times.
Step 14 Using switch statement there.
Step 15 Storing data in information file.
Step 16 End.

Code:
//M Saad Javed//
//Reg No. 200401013//
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
int main()
{
ofstream fout;
char check;
fout.open("Data.txt", ios::app);//open file to append
if (!fout)
{
cout << "Unable to open file!!\n";//error message if file not open
}
else
{
int s;
cout << "Press 1 for Data Write\n";
cout << "Press 2 for Data read\n";
cout << "Press 3 for Data Search Using name\n";
cout << "Press 4 for Data Search Using Preferences\n";

cin >> s;
if (s == 1)
{
do {

char name[500];
char DOB[500];
char age[3];
int fscmarks;
int matricmarks;

cin.ignore();//clear buffer
//get data from user
cout << "Enter name: ";
cin.getline(name, 500);
cout << "Enter DOB: ";
cin.getline(DOB, 500);
cout << "Enter Age: ";
cin.getline(age, 3);
cout << "FSC Marks: ";
cin >> fscmarks;
cout << "Matric Marks: ";
cin >> matricmarks;
char departments[50][500];//array to store department names
int totaldepartments = 0;//deparments count
ifstream myfile;
myfile.open("departments.txt");//open departments file in read mode
if (!myfile.fail())
{
while (!myfile.eof())//itereate until the end of file is reached
{
myfile.getline(departments[totaldepartments], 500);//read department
in deparments array
totaldepartments++;
}
}
myfile.close();//close file

int vopt;//variable to store option selected by user


for (int i = 0; i < totaldepartments; i++)//loop to print all departments
{
cout << "Option " << i + 1 << " : " << departments[i] << endl;
}
cout << "Enter option: ";
cin >> vopt;
char gap[2] = { '-','\0' };
fout << name << gap << DOB << gap << fscmarks << gap << matricmarks <<
gap << departments[vopt - 1] << endl;//writing data in file
cout << "Do you want to add other data: Y/N: ";
cin >> check;
} while (check != 'N');
fout.close();//closing file
}
else if(s==2)
{
ifstream fin;
fin.open("Data.txt");//open file in read mode
if (!fin.fail())
{
char arr[500];//temporary array to store the data that is read from file
while (!fin.eof())//itereate until the end of file is reached
{
fin.getline(arr, 500, '\n');//read compelete line
cout << arr << endl;//print the array read from file
}
}
fin.close();//close file
}
else if (s == 3)
{
int totalenteries = 0;
char name[500];
cin.ignore();
cout << "Enter name: ";
cin.getline(name, 500);
ifstream fin;
fin.open("Data.txt");//open file in read mode
if (!fin.fail())
{
char arr[500];//temporary array to store the data that is read from file
while (!fin.eof())//itereate until the end of file is reached
{
fin.getline(arr, 500, '\n');//read compelete line
char* ptr = NULL;
ptr = strstr(arr, name);
if (ptr != NULL)//if no name is not in arr then the ptr will have NULL
in it
{
cout << arr << endl;//print the array read from file
totalenteries++;
}
}
}
fin.close();//close file
cout << "Total Entries Found: " << totalenteries << endl;
}
else if (s== 4)
{
int totalenteries = 0;
char pref[500];
cout << "Preferences List: " << endl;
cout <<
"Aeronautics&Astronautics(AA)\nElectricalEngineering(EE)\nMaterialScience&Enginee
ring(MSE)\nMechanicalEngineering(ME)AvionicsEngineering(AE)\nSpaceScience(SC)\nAp
pliedMathematics&Statistics(AMS)\nHumanities&Sciences(HS)" << endl;
cin.ignore();
cout << "Enter Preference: ";
cin.getline(pref, 500);
ifstream fin;
fin.open("Data.txt");//open file in read mode
if (!fin.fail())
{
char arr[500];//temporary array to store the data that is read from file
while (!fin.eof())
{
cout<<"Not found"<<endl;
}
}
}
}
}

Output:

Link:
https://replit.com/@SaadJaved/Task-No-1-Lab-6

You might also like