Untitled
unknown
c_cpp
3 years ago
5.0 kB
24
Indexable
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct stuRec {
int id;
float cgpa;
string name;
}stu[10];
int index = -1;
bool isEmpty() {
if (index == -1) {
return true;
}
else {
return false;
}
}
bool isFull() {
if (index == 9) {
return true;
}
else {
return false;
}
}
//inputting function
void input() {
if(isFull() == true) {
cout << "array overflow.";
}
else {
index++;
cout << "Enter ID of " << index << " STUDENT " << endl;
cin >> stu[index].id;
cout << "Enter name of " << index << " STUDENT" << endl;
getline(cin >> ws, stu[index].name);
cout << "Enter CGPA OF " << index << " STUDENT" << endl;
cin >> stu[index].cgpa;
fstream putValues;
putValues.open("data.txt",ios::app);
putValues << stu[index].id << endl << stu[index].name << endl << stu[index].cgpa << endl;
putValues.close();
// changing the index value at index.txt
fstream putIndex;
putIndex.open("index.txt",ios::out);
putIndex << index;
putIndex.close();
}
}
void print() {
system("cls");
cout << "PRINT RECORDS \n \n";
for(int i = 0; i <= index; i++) {
cout << "STUDENT ID : " << stu[i].id << endl << "STUDENT NAME : " << stu[i].name << endl << "STUDENT CGPA : " << stu[i].cgpa << endl << endl;
}
}
void search() {
system("cls");
if (isEmpty() == true) {
cout << "array underflow";
}
else {
int x;
bool found = false;
cout << "SEARCH RECORDS \n \n BY ID \n \n";
cout << "enter which id you want to search of a student \n \n";
cin >> x;
for(int i = 0; i <= index; i++) {
if (stu[i].id == x) {
found = true;
cout << "FOUND AT " << i << endl;
}
}
if (found == false) {
cout << "not found anywhere. \n\n";
}
}
}
void update() {
system("CLS");
if (isEmpty() == true) {
cout << "array underflow.";
}
else {
int x;
cout << "UPDATE A RECORD \n \n";
cout << "---------- \n";
cout << "\n \n Enter the id of student you want to update. \n \n";
cin >> x;
for (int i = 0; i<=index; i++) {
if (stu[i].id == x) {
cout << "\n \n enter new name of this student \n \n";
cin >> stu[i].name;
cout << "\n \n enter new cgpa of this student \n\n";
cin >> stu[i].cgpa;
}
}
remove("data.txt");
fstream remakeData;
remakeData.open("data.txt",ios::out);
for(int i = 0; i <= index; i++) {
remakeData << stu[i].id << endl << stu[i].name << endl << stu[i].cgpa << endl;
}
remakeData.close();
}
}
void delRec() {
system("cls");
int x;
int j;
cout << "\n \n \t \t DELETE A RECORD\n \n";
cout << "--------------------\n \n";
cout << "enter the id of student you want to delete \n \n";
cin >> x;
for (int i = 0; i<=index; i++) {
if (x == stu[i].id) {
j = i; // j jo hoga, wo index hoga, jahan par id mili hogi, jisko delete karwana hoga, j ko agle forloop mei istamal karenge
}
}
for (int i = j; i <= index; i++) {
stu[j].id = stu[j+1].id;
stu[j].name = stu[j+1].name;
stu[j].cgpa = stu[j+1].cgpa;
}
stu[index].id = 0;
stu[index].name = " ";
stu[index].cgpa = 0;
index--;
// removing and remaking wali harkat
remove("index.txt");
fstream remakeIndex;
remakeIndex.open("index.txt", ios::out);
remakeIndex << index;
remakeIndex.close();
remove("data.txt");
fstream remakeData;
remakeData.open("data.txt", ios::out);
for(int i = 0; i <= index; i++) {
remakeData << stu[i].id << endl << stu[i].name << endl << stu[i].cgpa << endl;
}
remakeData.close();
}
int main() {
// getting all values.
// total reacords jisko hum index kehte hain, uski value arahi hai from index.txt
fstream getIndex;
getIndex.open("index.txt",ios::in);
getIndex >> index;
getIndex.close();
// arrays ke andar value dal rahi hai from files
fstream getValues;
getValues.open("data.txt", ios::in);
for(int i = 0; i <= index; i++) {
getValues >> stu[i].id >> stu[i].name >> stu[i].cgpa;
}
getValues.close();
// making menu
char cont;
int choice;
do {
system("cls");
cout << "STUDENT RECORD SYSTEM \n \n";
cout << "--------------";
cout << "\n \n press 1 to input \n press 2 to print \n press 3 to search \n press 4 to update \n press 5 to delete \n \n";
cin >> choice;
if(choice == 1) {
input();
}
else if(choice == 2) {
print();
}
else if (choice == 3) {
search();
}
else if (choice == 4) {
update();
}
else if (choice == 5) {
delRec();
}
else {
cout << "invalid input, try again \n \n";
}
cout << "do you want to continue? y/n \n \n";
cin >> cont;
} while(cont == 'Y' || cont == 'y');
return 0;
}
Editor is loading...