I have a problem with my C ++ program ... My whole program is a database for student names, grades and ages, and I have a problem with a function when a user wants to delete data for 1 student. Here is the code:
void deletestudentdata()
{
string name, grade, tname;
int age, x=0;
system("cls");
cout << "Enter name of the student you want to erase from database" << endl;
cin >> tname;
ifstream students("students.txt");
ofstream temp("temp.txt");
while(students >> name >> grade >> age)
{
if(tname!=name){
temp << name << ' ' << grade << ' ' << age << endl;
}
if(tname==name){
x=1;
}
}
students.clear();
students.seekg(0, ios::beg);
students.close();
temp.close();
remove("students.txt");
rename("temp.txt","students.txt");
if(x==0){
cout << "There is no student with name you entered." << endl;
}
else{
cout << "Student data has been deleted." << endl;
}
}
This works, but the problem is that I entered the student information, and when I want to delete it using this function, it does not delete it, first I have to close the program, then open the program again, and then call this function, it deletes student data.
How can I change it to delete student data immediately after input, without having to close the program first?
source
share