有人可以在此代码中找到错误
Could some one find the error in this code?
我已经编写了此程序,当我运行它并输入20.14时,这是输出:
输入金额:20.14更改应得:
20美元0季度1角0镍3便士
应该是4便士。但这显示了3。这是另一个输出:
输入金额:79.58更改应得:
79美元2季度0角1个镍3便士
但由于某种原因,在这里对其进行了正确的计算。
有人可以帮助我找到错误吗?预先感谢。
这是代码:
//Description: This program takes in a dollar amount from the user and
//and calculates and displays how to make the change using the smallest
//number of bills and coins possible.
#include <iostream>
using namespace std;
int main()
{
//Declaring the variables.
//dollarAmount is the amount that will be input by the user, which
//will be split into dollars, quarters, dimes, nickels, pennies.
float dollarAmount;
int dollars = 0, quarters = 0, dimes = 0, nickels = 0, pennies = 0;
//Displaying message to user to input value for dollarAmount.
cout << "Enter the amount: ";
//Taking in the value for dollarAmount.
cin >> dollarAmount;
//Splitting the dollarAmount into dollars, quarters, dimes,
//nickels and pennies.
pennies = dollarAmount * 100.0;
dollars = pennies / 100;
pennies = pennies % 100;
quarters = pennies / 25;
pennies = pennies % 25;
dimes = pennies / 10;
pennies = pennies % 10;
nickels = pennies / 5;
pennies = pennies % 5;
//Displaying a message to the user with desired output
cout << "Change Due:nn";
cout << dollars << " dollarsn";
cout << quarters << " quartersn";
cout << dimes << " dimesn";
cout << nickels << " nickelsn";
cout << pennies << " penniesn";
return 0;
}
您必须使用<cmath>
中包含的std::fmod
才能计算浮点数的浮点数。
相关文章:
- 错误处理.将系统错误代码映射到泛型
- 为什么我在使用void函数时得到错误代码C2276
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- 逻辑运算符上出现错误代码 a')'
- 'val' Arduino 错误代码之前的预期'('
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 根据 GetLastError 直接写入磁盘会导致错误代码 5
- 当我选择大于 720 的矩阵大小时,程序退出并显示错误代码.可能是什么原因?
- 创建进程 API 失败,在窗口 122 上出现错误代码 10
- 使用另一个函数调用一个函数(都在类中)时出现问题.没有错误代码C++
- 当命令失败时,gzip 会在 C++ 中返回错误代码吗?
- 从 C++ 调用 cURL 命令会返回意外的错误代码,如 1792 和 6656
- WSALookupServiceBegin() 上的错误代码 10022(无效的 arugment)
- C++ Schannel POST 400 错误代码错误请求
- openCL-创建子缓冲区返回错误代码13
- 更新了Runge-Kutta(RK4)C++错误代码中的二阶DE
- 获取有关使用未初始化内存的错误代码
- 排序程序的意外错误代码
- 正在将DJI错误代码记录到流中
- 来自API的错误代码..处理什么是好的做法