【问题标题】:The number entered isn't the same as the result输入的数字与结果不同
【发布时间】:2016-10-02 07:05:34
【问题描述】:

我的代码有问题,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,有人可以帮我解决这个问题吗? :)

【问题讨论】:

    标签: c++ iostream


    【解决方案1】:

    在打开“debit.txt”之前,您没有关闭“infile”对象。 请按如下所述关闭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(); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-04
      • 2016-03-29
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-16
      相关资源
      最近更新 更多