Coming soon - Get a detailed view of why an account is flagged as spam!
view details

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.

0
I need help to understand the error inside this function
Post Flair (click to view more posts with a particular flair)
Post Body

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;

}

Author
Account Strength
40%
Account Age
3 months
Verified Email
Yes
Verified Flair
No
Total Karma
78
Link Karma
37
Comment Karma
41
Profile updated: 2 days ago
Posts updated: 1 month ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
2 months ago