0% found this document useful (0 votes)
2 views

Programming Fundamental C++

The document contains two programming tasks focused on a student record system using C++. Task 1 allows users to add and search records in a simple text file, while Task 2 expands functionality to include adding, displaying, searching, updating, and deleting student records. Both tasks utilize file handling for data persistence and provide a user-friendly console interface.

Uploaded by

zamrankhan49
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Programming Fundamental C++

The document contains two programming tasks focused on a student record system using C++. Task 1 allows users to add and search records in a simple text file, while Task 2 expands functionality to include adding, displaying, searching, updating, and deleting student records. Both tasks utilize file handling for data persistence and provide a user-friendly console interface.

Uploaded by

zamrankhan49
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

PROGRAMMING FUNFAMENTALS

NAME: ZAMRAN KHAN – L1F4BSCS0084


SUBMITTED TO: PROF. MUJTABA HASSAN

Task 1
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void addRecord () {
ofstream file("data.txt");
string id, name;

cout << "Enter ID: ";


cin >> id;
cout << "Enter Name: ";
cin >> name;

file << id << " " << name << endl;


file.close();
cout << "Record saved"<< endl;
}

void searchRecord() {
ifstream file("data.txt");
string id, name;
string keyword;
bool found = false;
int lineNum = 0;

cout << "Enter ID or Name to search: ";


cin >> keyword;

while (file >> id >> name) {


lineNum++;
if (id == keyword || name == keyword) {
cout << "Record found at line " << lineNum << endl;
cout << "ID: " << id << ", Name: " << name << endl;
found = true;
break;
}
}

if (!found) {
cout << "Record not found.\n";
}

file.close();
}

int main() {
int choice;

do {
cout << "1. Add Record"<<endl;
cout << "2. Search Record"<<endl;
cout << "3. Exit"<<endl;
cout << "Enter choice: ";
cin >> choice;

if (choice == 1)
addRecord();
else if (choice == 2)
searchRecord();
else if (choice != 3)
cout << "Invalid choice.\n";

} while (choice != 3);

return 0;
}
Output:

Task 2
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void addStudent() {
string id, name, age, dep, grade;

cout << "Enter ID: ";


cin >> id;
cout << "Enter Name: ";
cin >> name;
cout << "Enter Age: ";
cin >> age;
cout << "Enter Department: ";
cin >> dep;
cout << "Enter Grade: ";
cin >> grade;
ofstream file("students.txt", ios::app);
file << id << " " << name << " " << age << " " << dep << " " << grade << endl;
file.close();

cout << "Student added successfully." << endl;


}

void showStudents() {
ifstream file("students.txt");
string id, name, age, dep, grade;

cout << endl << "All Students:" << endl;


cout << "ID\tName\tAge\tDep\tGrade" << endl;

while (file >> id >> name >> age >> dep >> grade) {
cout << id << "\t" << name << "\t" << age << "\t" << dep << "\t" << grade << endl;
}

file.close();
}

void searchStudent() {
string searchId;
cout << "Enter Student ID to search: ";
cin >> searchId;

ifstream file("students.txt");
string id, name, age, dep, grade;
bool found = false;

while (file >> id >> name >> age >> dep >> grade) {
if (id == searchId) {
cout << endl << "Student Found:" << endl;
cout << "ID: " << id << endl;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Department: " << dep << endl;
cout << "Grade: " << grade << endl;
found = true;
break;
}
}

if (!found) {
cout << "Student ID not found." << endl;
}
file.close();
}

void deleteStudent() {
string delId;
cout << "Enter Student ID to delete: ";
cin >> delId;

ifstream file("students.txt");
ofstream temp("temp.txt");

string id, name, age, dep, grade;


bool found = false;

while (file >> id >> name >> age >> dep >> grade) {
if (id != delId) {
temp << id << " " << name << " " << age << " " << dep << " " << grade << endl;
} else {
found = true;
}
}

file.close();
temp.close();

remove("students.txt");
rename("temp.txt", "students.txt");

if (found)
cout << "Student deleted." << endl;
else
cout << "Student ID not found." << endl;
}

void updateStudent() {
string updateId;
cout << "Enter Student ID to update: ";
cin >> updateId;

ifstream file("students.txt");
ofstream temp("temp.txt");

string id, name, age, dep, grade;


bool found = false;
while (file >> id >> name >> age >> dep >> grade) {
if (id == updateId) {
cout << "Enter new Name: ";
cin >> name;
cout << "Enter new Age: ";
cin >> age;
cout << "Enter new Department: ";
cin >> dep;
cout << "Enter new Grade: ";
cin >> grade;
found = true;
}
temp << id << " " << name << " " << age << " " << dep << " " << grade << endl;
}

file.close();
temp.close();

remove("students.txt");
rename("temp.txt", "students.txt");

if (found)
cout << "Student updated." << endl;
else
cout << "Student ID not found." << endl;
}

int main() {
int choice;

do {
cout << endl << " Student Record System" << endl;
cout << "1. Add Student" << endl;
cout << "2. Show All Students" << endl;
cout << "3. Search Student by ID" << endl;
cout << "4. Update Student" << endl;
cout << "5. Delete Student" << endl;
cout << "6. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;

if (choice == 1)
addStudent();
else if (choice == 2)
showStudents();
else if (choice == 3)
searchStudent();
else if (choice == 4)
updateStudent();
else if (choice == 5)
deleteStudent();
else if (choice == 6)
cout << "Exiting..." << endl;
else
cout << "Invalid choice. Try again." << endl;

} while (choice != 6);

return 0;
}

Output:

You might also like