Gilang Rizkie Gilang Rizkie - 2 months ago 13
C++ Question

the number inputted isn't same with the result

I've problem with my code, the result of

debit.txt
doesnt same with what inputted

long int s;

ofstream outfile;
outfile.open("saldo.txt");
cout << "Masukan jumlah saldo kredit : "; cin >> s;
outfile << s << endl;
outfile.close();


my
saldo.txt


long int db;

ofstream outfile;
outfile.open("debit.txt");
cout << "Masukan jumlah saldo kredit : "; cin >> db;
outfile << db << endl;
outfile.close();


my
debit.txt


long int s, db;

ifstream infile;
infile.open("saldo.txt");
infile >> s;
cout << s << endl;

infile.open("debit.txt");
infile >> db;
cout << db << endl;
infile.close();


} and this the result
cek.txt


when i try to input 150 in
debit.txt
the result is random number but not for
saldo.txt
, can someone help me fix this ? :)

Answer

you did not close "infile" object, before opening "debit.txt". Please close the infile as said below

long int s, db;

ifstream infile;
infile.open("saldo.txt");
infile >> s;
cout << s << endl;


infile.close(); //close here.


infile.open("debit.txt");
infile >> db;
cout << db << endl;
infile.close();