Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- struct Admin {
- string username;
- string password;
- };
- struct User {
- int rollNo;
- string name;
- string fatherName;
- bool gender;
- double cgpa;
- };
- Admin admin;
- User user[100];
- int num_users = 0;
- bool isLoggedIn = true;
- void writeUser(User user) {
- ofstream myFile("users.txt", ios::app);
- if (myFile.is_open()) {
- myFile << user.rollNo << endl;
- myFile << user.name << endl;
- myFile << user.fatherName << endl;
- myFile << user.gender << endl;
- myFile << user.cgpa << endl;
- myFile << endl;
- myFile.close();
- }
- }
- void readRecords() {
- ifstream myFile("users.txt");
- string line;
- if (myFile.is_open()) {
- num_users = 0;
- while (getline(myFile, line)) {
- user[num_users].rollNo = stoi(line);
- getline(myFile, line);
- user[num_users].name = line;
- getline(myFile, line);
- user[num_users].fatherName = line;
- getline(myFile, line);
- if (line == "1") {
- user[num_users].gender = true;
- } else {
- user[num_users].gender = false;
- }
- getline(myFile, line);
- user[num_users].cgpa = stod(line);
- num_users++;
- getline(myFile, line);
- }
- }
- cout<< endl;
- }
- void userAccount() {
- cout << "Enter User Details"<< endl;
- cout << "Roll-No : ";
- cin >> user[num_users].rollNo;
- cout << "Name : ";
- cin >> user[num_users].name;
- cout << "Father's Name : ";
- cin >> user[num_users].fatherName;
- cout << "Gender : "<<endl;
- cout << "1. Male"<< endl;
- cout << "2. Female"<< endl;
- int genderInput;
- cin >> genderInput;
- if (genderInput == 1) {
- user[num_users].gender = true;
- } else {
- user[num_users].gender = false;
- }
- cout << "CGPA : ";
- cin >> user[num_users].cgpa;
- writeUser(user[num_users]);
- num_users++;
- }
- void displayRecords() {
- cout <<"--------------------Records Display---------------------" <<endl << endl;
- for (int i = 0; i < num_users; i++)
- {
- cout << "Roll-No: " << user[i].rollNo << endl;
- cout << "Name: " << user[i].name << endl;
- cout << "Father's Name: " << user[i].fatherName << endl;
- cout << "Gender: " << user[i].gender << endl;
- cout << "CGPA: " << user[i].cgpa << endl;
- cout << endl;
- }
- system("pause");
- }
- void searchRecord() {
- int rollNo;
- cout << "Enter Roll-No: ";
- cin>> rollNo;
- for (int i = 0; i < num_users; i++)
- {
- if (user[i].rollNo == rollNo) {
- cout << "Roll-No: " << user[i].rollNo << endl;
- cout << "Name: " << user[i].name << endl;
- cout << "Father's Name: " << user[i].fatherName << endl;
- cout << "Gender: " << user[i].gender << endl;
- cout << "CGPA: " << user[i].cgpa << endl;
- cout << endl;
- break;
- }
- }
- system("pause");
- }
- void deleteRecord() {
- int rollNo;
- cout << "Enter Roll-No: ";
- cin>> rollNo;
- remove("users.txt");
- for (int i = 0; i < num_users; i++)
- {
- if (user[i].rollNo != rollNo) {
- writeUser(user[i]);
- }
- }
- readRecords();
- }
- void showNumberOfUsers() {
- cout<< "Number of records: " << num_users<< endl;
- system("pause");
- }
- void login() {
- string username;
- string password;
- cout << "Login"<< endl;
- cout << "User Name : ";
- cin >> username;
- cout << "Password : ";
- cin >> password;
- if (admin.username == username && admin.password == password) {
- cout << "Login Successful"<<endl;
- isLoggedIn = true;
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- int input;
- admin.username = "admin";
- admin.password = "admin";
- //login();
- readRecords();
- while(isLoggedIn) {
- system("cls");
- cout<<"**************Student Record Database"<<endl;
- cout << "1. Enter Record"<< endl;
- cout << "2. Display Records"<< endl;
- cout << "3. Search Record"<< endl;
- cout << "4. Delete Record"<< endl;
- cout << "5. Total number of students"<< endl;
- cout << "6. Exit"<< endl;
- cout << "Select An Option : ";
- cin >> input;
- switch (input)
- {
- case 1:
- userAccount();
- break;
- case 2:
- displayRecords();
- break;
- case 3:
- searchRecord();
- break;
- case 4:
- deleteRecord();
- break;
- case 5:
- showNumberOfUsers();
- break;
- case 6:
- isLoggedIn = false;
- default:
- break;
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment