今天剛剛開始學習C++,我很驚訝。其驚人的語言,但遇到了一些麻煩IM覆蓋文件非常簡單的io問題C++
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
double payIncrease = 7.6;
double annual;
double annualIncrease;
double newAnnual;
double monthlyIncrease;
double newMonthly;
ifstream inStream;
ofstream outStream;
//繼承人問題所在
inStream.open("annualSalary.txt");
outStream.open("newAnnualSalary.txt");
如果我改變newAnnualSalary.txt到annualSalary.txt我得到了一些非常奇怪的數字。 有誰知道爲什麼?
inStream >> annual;
inStream.close();
double monthly = (annual/12);
annualIncrease = ((annual/100)*payIncrease);
monthlyIncrease = ((monthly/100)*payIncrease);
newMonthly = (monthly + monthlyIncrease);
newAnnual = (annual + annualIncrease);
outStream <<"annual salary was: "<< annual << "\n" ;
outStream <<"new annual salary is " << newAnnual << "\n ";
outStream <<"new monthly salary is " << newMonthly <<"\n ";
outStream.close();
return 0;
}
IM意識到這是一個非常低技術水平的問題,但我剛學。
「奇怪的數字」是什麼意思? – Jess 2010-09-16 15:22:23
它可能是因爲你試圖打開annualsalary.txt兩次,一次入站和一次出站。嘗試_not_打開您要覆蓋的文件,直到您關閉它(即將outStream.open命令放入inStream.close命令後)以查看是否有幫助。 – 2010-09-16 15:26:11
@Michael,外流正在打開newAnnualSalary.txt。他很好去:) – jlafay 2010-09-16 15:27:22