Class and while loop

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <ctime>

using namespace std;

class Hastane
{
private:
long long int id;
string name;
string surname;
string dogumtarihi;
string sigortaturu;
fstream *fileptr;
public:

Hastane(fstream&);
void changingname(long long int,string);
void changingsgt(long long int,string);
void display();


};


int main()
{
fstream input ("Kayit.txt",ios::out|ios::in);
Hastane h1(input);
Hastane h2(input);
long long int tc;
string name;

while(true){
cout<<"degistirmek istediginiz tcyi giriniz"<<endl;
cin>>tc;
cout<<"yeni ismi giriniz"<<endl;
cin>>name;

h1.changingname(tc,name);
cout<<"done"<<endl;}

return 0;
}
Hastane::Hastane(fstream & input)
{
fileptr=&input;
}
void Hastane::changingname(long long int tc,string newname)
{
ofstream temp("temp.txt",ios::out);



for(int i=0; i < 10 ;i++){

*fileptr>>id>>name>>surname>>dogumtarihi>>sigortaturu;


if (id == tc)
{
temp<<id<<" "<<newname<<" "<<surname<<" "<<dogumtarihi<<" "<<sigortaturu<<endl;

}
else{

temp<<id<<" "<<name<<" "<<surname<<" "<<dogumtarihi<<" "<<sigortaturu<<endl;
}


}
temp.close();
fileptr->close();

remove("Kayit.txt");
rename("temp.txt","Kayit.txt");


}


I can use this code once. When I try to use this code, private variable remain constant
closed account (367kGNh0)
what DO you need help with?
Yes. How can i fix this problem
My first suggestion is that you stop opening and closing files all over the place. I'd recommend you open the files in main(), checking for successful opening, and then pass that open stream instance into the functions that require them.

I'd also recommend that you not have a pointer to a stream in your class, in fact I don't recommend even an instance of a stream in your class.

Lastly for now I would recommend that you not close your streams, let the class destructor close the stream when it goes out of scope. As it is right now you appear to be closing a file before you're done with it. And until you get your program working I would stop erasing the input file and just save the changes to another file.

Topic archived. No new replies allowed.