因此,由於某些原因,當我從某個文本文件中讀取數據時,我只能讀取並輸出第一個和第三個文件。我不確定它爲什麼不讀它,不管第二個人是否有負數。這裏是我使用的文本文件:編輯:C++上的輸入和輸出文件
編輯:修正了閱讀的問題,但現在我想讓它認識到,如果它讀取一個空文件,它會識別它是空的,不會終止但給出一個消息,如「文件是空的」。
約瑟夫Kradin III
喬啵
南西亞當斯
這裏是的一部分我代碼在哪裏我的循環和鰭的東西都在那裏:
fin >> firstName;
getline(fin, lastName);
fin >> deposit >> year >> numCompound;
do
{
fullName = firstName + " " + lastName;
// if statements to determine the interest rate depending on the years
if (year >= 5)
rate = 0.045;
else if ((year < 5) & (year >= 4))
rate = 0.04;
else if ((year < 4) & (year >= 3))
rate = 0.035;
else if ((year < 3) & (year >= 2))
rate = 0.025;
else if ((year < 2) & (year >= 1))
rate = 0.02;
else
rate = 0.015;
if (deposit < 0 || year < 0 || numCompound < 0)
{
cout << fullName << " You have entered a negative number" << endl;
fout << fullName << " You have entered a negative number" << endl;
}
else if (deposit > 0 || year > 0 || numCompound > 0)
{
numname++;
// finding the value for amount of money aquired after n years, including interest.
moneyAquired = deposit * pow((1 + rate/numCompound), numCompound*year);
// finding earned interest
earnedInterest = moneyAquired - deposit;
// For the Total Deposit entered
totaldeposit += deposit;
// For total earned interest
totalinterest += earnedInterest;
cout << fixed << setprecision(2) << showpoint << setw(20) << left << fullName << setw(15) << year << setw(14) << rate * 100 << "$" << setw(15) << deposit << "$" << setw(15) << earnedInterest << "$" << setw(20) << moneyAquired << endl;
fout << fixed << setprecision(2) << showpoint << setw(20) << left << fullName << setw(15) << year << setw(14) << rate * 100 << "$" << setw(15) << deposit << "$" << setw(15) << earnedInterest << "$" << setw(20) << moneyAquired << endl;
}
} while (fin >> firstName >> lastName >> deposit >> year >> numCompound && !fin.eof());
//end of do while loop
fout.close();
fin.close();
http://ericlippert.com/2014/03/05/how-to-debug -small-programs/ – Biffen
是的,這裏是您可以考慮發佈的所有內容,除了實際的問題。 – StoryTeller