Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
30 views4 pages

Practical 11

The document is a C++ program for a Student Information System that allows users to add, delete, and display student records stored in a text file. It defines a 'Student' structure and provides functions to handle user input and file operations. The main function presents a menu for user interaction until the exit option is selected.

Uploaded by

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

Practical 11

The document is a C++ program for a Student Information System that allows users to add, delete, and display student records stored in a text file. It defines a 'Student' structure and provides functions to handle user input and file operations. The main function presents a menu for user interaction until the exit option is selected.

Uploaded by

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

#include <iostream>

#include <fstream>
#include <string>
using namespace std;

struct Student {
string roll;
string name;
string division;
string address;
};

void addStudent() {
Student s;
cout << "Enter Roll Number: ";
cin >> s.roll;
cout << "Enter Name: ";
cin.ignore();
getline(cin, s.name);
cout << "Enter Division: ";
getline(cin, s.division);
cout << "Enter Address: ";
getline(cin, s.address);

ofstream outFile("students.txt", ios::app);


outFile << s.roll << "," << s.name << "," <<
s.division << "," << s.address << "\n";
outFile.close();

cout << "Student added successfully.\n\n";


}

void deleteStudent() {
string rollToDelete;
bool found = false;
cout << "Enter Roll Number to delete: ";
cin >> rollToDelete;
ifstream inFile("students.txt");
ofstream tempFile("temp.txt");

string line;
while (getline(inFile, line)) {
if (line.substr(0, line.find(',')) !=
rollToDelete) {
tempFile << line << "\n";
} else {
found = true;
}
}

inFile.close();
tempFile.close();

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

if (found)
cout << "Student deleted successfully.\n\n";
else
cout << "Student record not found.\n\n";
}

void displayStudent() {
string rollToSearch;
bool found = false;
cout << "Enter Roll Number to display: ";
cin >> rollToSearch;

ifstream inFile("students.txt");
string line;

while (getline(inFile, line)) {


if (line.substr(0, line.find(',')) ==
rollToSearch) {
found = true;
size_t pos1 = line.find(',');
size_t pos2 = line.find(',', pos1 + 1);
size_t pos3 = line.find(',', pos2 + 1);

cout << "\nStudent Found:\n";


cout << "Roll No: " << line.substr(0, pos1)
<< "\n";
cout << "Name: " << line.substr(pos1 + 1,
pos2 - pos1 - 1) << "\n";
cout << "Division: " << line.substr(pos2 +
1, pos3 - pos2 - 1) << "\n";
cout << "Address: " << line.substr(pos3 +
1) << "\n\n";
break;
}
}

inFile.close();
if (!found)
cout << "Student record not found.\n\n";
}

int main() {
int choice;

do {
cout << "----- Student Information System -----
\n";
cout << "1. Add Student\n";
cout << "2. Delete Student\n";
cout << "3. Display Student\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
displayStudent();
break;
case 4:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice. Please try
again.\n";
}
} while (choice != 4);

return 0;
}

You might also like