Programming Fundamental C++
Programming Fundamental C++
Task 1
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void addRecord () {
ofstream file("data.txt");
string id, name;
void searchRecord() {
ifstream file("data.txt");
string id, name;
string keyword;
bool found = false;
int lineNum = 0;
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";
return 0;
}
Output:
Task 2
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void addStudent() {
string id, name, age, dep, grade;
void showStudents() {
ifstream file("students.txt");
string id, name, age, dep, grade;
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");
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");
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;
return 0;
}
Output: