第二次提示cin我需要該程序充當自動售貨機,保持總運行並確定更改(如果適用)。while while循環處於無限循環時,不會通過
#include <iostream>
using namespace std;
int main()
{
cout << "A deep-fried twinkie costs $3.50" << endl;
double change, n = 0, d = 0, q = 0, D = 0, rTotal = 0;
do
{
cout << "Insert (n)ickel, (d)ime, (q)uarter, or (D)ollar: ";
cin >> rTotal;
if (rTotal == n)
rTotal += 0.05;
if (rTotal == d)
rTotal += 0.10;
if (rTotal == q)
rTotal += 0.25;
if (rTotal == D)
rTotal += 1.00;
cout << "You've inserted $" << rTotal << endl;
cout.setf(ios::fixed);
cout.precision(2);
} while (rTotal <= 3.5);
if (rTotal > 3.5)
change = rTotal - 3.5;
return 0;
}
比較浮動和雙打:https://stackoverflow.com/questions/17333/what-is-the-most-effective-way-for-float-and-double-comparison –
你目前需要兩個不同的變量插入的硬幣和總金額。 – mkrieger1
此外,如果用戶輸入的字母等於0,那麼您目前正在檢查所有情況,這從來都不是真的。 – mkrieger1