This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
In the following code (This is C 98) I have a function that deletes values of a plain text by placing the values that do match the code on a new text. Whenever I have tried the function it worked but then I tried other different values and it did not work.
What does cause the pointer to go mad?(if it is, I just simply assume)
I added some prompts(I'm learning and simply wanted the user to give more control over the data, if they wanted to continue deleting and such). Therefore there are times that I close the file early while also closing a file that I know is open. Are the times that I use (filevalue).close() making the pointer go places that it shouldnt?
Also, I add that I only seek help, I do not want anyone to do any work for me but an explanation or an example will help me greatly, thanks
PD: I had the function to be far more simpler and it didn't ask anything else, it simply deleted and it also worked but yeah, suddenly I get to it and now it doesn't work with some values.
bool borrarAlumno(){
ifstream original;
ofstream copia;
string respuesta,codigoBor,codigo,nombre,apellido1,apellido2,asignatura,nota1,nota2,nota3,notaFinal;
bool encontrado=false;
system("cls");
//Se debe de obtener el codigo, es algo elemental para encontrar el alumno
cout<<"\\nDebe de introducir el codigo del alumno que va a borrar: ";
codigoBor = codigoGen();
//Se abren los archivos
original.open("alumnos.txt");
copia.open("copia.txt");
if(original.fail()){
cout<<"\\nError al intentar abrir el archivo de lectura para borrar a un alumno";
}
if(copia.fail()){
cout<<"\\nError al intentar abrir el archivo copia para borrar un alumno";
}
while(original>>codigo>>nombre>>apellido1>>apellido2>>asignatura>>nota1>>nota2>>nota3>>notaFinal){
//En el caso de que se encuentre no se añade esa linea al archivo
if(codigoBor==codigo){
encontrado=true;
}//En el caso de que no se encuentre se añade al archivo
else{
copia<<codigo<<" "<<nombre<<" "<<apellido1<<" "<<apellido2<<" "<<asignatura<<" "<<nota1<<" "<<nota2<<" "<<nota3<<" "<<notaFinal<<"\\n";
}
}
//Tras tenerlo todo añadido se ofrece la posibilidad de continuar borrando en el mismo menu, si no se cierra el archivo
if(encontrado==true){
cout<<"Se ha borrado el alumno con el codigo: "<<codigoBor<<"\\n";
cout<<"Desea borrar otro alumno?(Si/no)\\n";
cin>>respuesta;
respuesta = mayus(respuesta);
while(respuesta != "SI" and respuesta != "NO"){
cout<<"\nSolo se admiten si o no como respuestas validas, por favor introduzca una de estas: ";
cin>>respuesta;
respuesta = mayus(respuesta);
}
if(respuesta == "SI"){
copia.close();
original.close();
remove("alumnos.txt");
rename("copia.txt","alumnos.txt");
return borrarAlumno();
}
else if(respuesta == "NO"){
copia.close();
original.close();
cout<<"\\nVolviendo al menu principal";
system("pause");
return 0;
}
}
else if(encontrado==false){
cout<<"No se ha podido borrar un alumno ya que no se ha encontrado ningun alumno con ese codigo\\n";
}
//Se cierra todo siempre antes de salir
original.close();
copia.close();
//Se renombra y se borra el archivo
remove("alumnos.txt");
rename("copia.txt","alumnos.txt");
return true;
}
Subreddit
Post Details
- Posted
- 2 months ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/cpp_questio...