Untitled

 avatar
unknown
c_cpp
2 years ago
5.5 kB
15
Indexable
#include <iostream>
#include <string>
#include <fstream>
#include <string>


using namespace std;
struct studentRec {
	int id, cgpa;
	string name;
}stu[10];



int index = -1;


bool isEmpty() {
	if (index == -1) {
		return true;
	}
	
	else {
		return false;
	}
}

bool isFull() {
	if (index == 10) {
		return true;
	}
	
	else {
		return false;
	}
}


// function to get index value at start of program
void getIndex() {
	fstream getIndex;

getIndex.open("index.txt",ios::in);
getIndex>> index;
getIndex.close();
}

// printing function
void printAllData() {
	for (int i = 0; i < index; i++) {
		cout << endl << "STUDENT ID :" << stu[i].id << endl << "STUDENT NAME : " << stu[i].name << endl << "STUDENT CGPA : " << stu[i].cgpa << endl;
	}
}

// searching function
void searchData() {
	
	
	if (isEmpty() == true) {
		cout << "array underflow.";
	}
	
	else {
	int x;
	
	bool found = false;
	cout << "SEARCH DATA \n \n";
	cout << "enter which id you want to search \n \n";
	cin >> x;
	
	for (int i = 0; i < index; i++) {
		if(stu[i].id == x) {
			found = true;
			
			cout << "FOUND!" << endl;
			cout << "FOUND AT " << i << endl;
		}	
	}
	
	if (found == false) {
		cout << "NOT FOUND ANYWHERE! \n ";
		}
	}
}


// input function
void inputData() {
	
	if (isFull() == true) {
		cout << "array overflow. \n \n";
	}
	
	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 putValue;
		putValue.open("data.txt", ios::app);
		
		putValue << stu[index].id << endl << stu[index].name << endl << stu[index].cgpa << endl;
		
		putValue.close();
		
		// now updating the index value inside the index.txt file.
		
		fstream putIndex;
		putIndex.open("index.txt", ios::out);
		putIndex << index;
		putIndex.close();	
	}
	
}

// updating function
void updateRec() {
	system("CLS");
	
	int x;
	cout << "\n \n \t \t UPDATE RECORD \n \n";
	cout << "enter id of student to make changes to the record \n \n";
	cin >> x;
	
	for (int i = 0; i < index; i++) {
		if (stu[i].id == x) {
			cout << "enter the changed name. \n \n";
			cin >> stu[i].name;
			cout << "\n enter the changed cgpa \n \n";
			cin >> stu[i].cgpa;
		}
	}
	
	
	// now removing and remaking the records to save changes permenantly.
	
	remove("data.txt");
	// the arrays were saved in the program, hence the data is saved in arrays, so we are using them to remake the file after changes
	fstream remakeData;
	remakeData.open("data.txt");
	
	for(int i = 0; i < index; i++) {
		remakeData << stu[i].id << endl << stu[i].name << endl << stu[i].cgpa << endl;
	}
	
	remakeData.close();
		// there is no change in index, hence index remains same, however, in delete function we would have to remake index.txt	
}


//deleting function
void delRec() {
	system("CLS");
	
	int j;
	int x;
	if (isEmpty() == true) {
		cout << "array underflow.";
	}
	
	else {
	cout << "DELETE A RECORD \n \n";
	// to delete a record, using student id instead of index of array for efficiency
	cout << "enter id of student you want to delete \n \n";
	cin >> x;
	
	for (int i = 0; i < index; i++) {
		if(stu[i].id == x) {
			j = i; // j is the index at which the id is found
		}
	}
	
	// we will use j as the value for i, so that we start from there and then give stu[j] value of stu[j+1] and so and, and give stu[index] = 0
	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--;
	
	// now we have to remake index.txt because a change in index variable is made.
	
	remove("index.txt");
	fstream remakeIndex;
	
	remakeIndex.open("index.txt", ios::out);
	remakeIndex << index;
	remakeIndex.close();
	
	// remaking the data.txt file
	
	remove("data.txt");
	fstream remakeData;
	remakeData.open("data.txt",ios::out); // using ios::out because array has saved data before.
	for (int i = 0; i < index; i++) {
		remakeData << stu[i].id << endl << stu[i].name << endl << stu[i].cgpa << endl;
	}
	
	remakeData.close();
}
}


int main() {
	
getIndex();	
	// getting index value from a txt file, which will be ++ whenever an input is given


// getting all values from file to array::: 
	string temp;
		fstream getValues;
	getValues.open("data.txt",ios::in);	
	for(int i = 0; i < index; i++) {
		getValues >> stu[i].id >> temp >> stu[i].cgpa;
		stu[i].name = temp;
		}

// making a menu

int choice;
char cont;

		do {
		system("cls");
		cout << "press 1 to input data \n press 2 to print all data \n press 3 to search data \n press 4 to update data \n press 5 to delete rec" << endl;
		
		cin >> choice;
		
		if (choice == 1) {
			inputData();
		}
		
		else if (choice == 2) {
			printAllData();
		}
		
		else if (choice == 3) {
			searchData();
		}
		
		else if (choice == 4) {
			updateRec();
		}
		
		else if (choice == 5) {
			delRec();
		}
	
		else {
			cout << "invalid choice";
		}
		
		cout << "do you want to continue? y/n" << endl;
		cin >> cont;
	} while(cont == 'y' || cont == 'Y');
	
	return 0;
}


Editor is loading...