输入的数字与结果不同

时间:2016-10-02 07:05:34

标签: c++ iostream

我的代码有问题,debit.txt的结果与输入不一样:

long int s;

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

我的saldo.txt

long int db;

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

我的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(); 

}

这就是结果cek.txt

当我尝试在debit.txt中输入150时,结果是一个随机数,但不是saldo.txt,有人可以帮我解决这个问题吗? :)

1 个答案:

答案 0 :(得分:0)

你没有关闭&#34; infile&#34;对象,在打开之前&#34; debit.txt&#34;。 请关闭infile如下所述

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();