有人可以告訴我爲什麼變量百分比= 0?應該是33.3333吧? (2/6)有人能告訴我爲什麼我的分數總是計算爲零?
因爲當我運行下面的功能,則其結果是
總線= 6
奇數行= 2個
百分比奇數行= 0
bool count_odd_lines(string a, string b)
{
ifstream in;
ofstream out;
in.open(a.c_str());
out.open(b.c_str());
string s;
int count = 0;
int odd = 0;
if (in && out)
{
while (getline(in, s))
{
count++;
if (s.length() % 2 != 0) odd++;
}
out << "Total lines = " << count << endl;
out << "Odd lines = " << odd << endl;
double percent = odd/count;
out << "Percent odd lines = " << setprecision(4) << percent <<endl;
return true;
}
else return false;
in.close();
out.close();
}
請不要建議type()轉換爲newbie。讓他習慣於static_cast。 –
+1考慮到每*分*需要一個*分* - 部分地方:) –