c++ - 为什么这些简单的函数会给出天文数字大或小的数字?

标签 c++ math syntax

所以我的教授要求我们制作一个程序,根据输入的工资率和工作时间计算预算。百分比是教授给出的。另外,我是 C++ 的新手,所以请原谅我。

#include <iostream>

using namespace std;

int main()

{
  int hours;
  double Pay;

  const double Net      = hours * Pay;
  const double Tax      = Net * .14;
  const double afterTax = Net - Tax;
  const double Clothes  = .1 * afterTax;
  const double Supplies = .01 * afterTax;
  const double Bonds    = .25 * afterTax;
  const double Parents  = Bonds / 2;

  cout << "Please enter hours worked \n";
  cin >> hours;
  cout << "Please enter hourly rate \n";
  cin >> Pay;

  cout << "Total Number of hours worked: " << hours << endl;
  cout << "Total income before taxes: " << Net << endl;
  cout << "Net Income: " << afterTax << endl;
  cout << "Money Spent on clothes and accessories: " << Clothes << endl;
  cout << "Money spent on school supplies: " << Supplies << endl;
  cout << "Money spent on savings bonds: " << Bonds << endl;
  cout << "Money spent by parents for savings bonds: " << Parents << endl;
  cout << "Remaining: " << afterTax - Clothes - Supplies - Bonds << endl;

  return 0;
}

最佳答案

hours 和其他几个变量未初始化,但您使用它们的值来初始化其他变量。您需要重新安排计算,以便在用户输入必要的项目后执行这些计算。

关于c++ - 为什么这些简单的函数会给出天文数字大或小的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28386794/

相关文章:

javascript - 这个脚本标签语法是什么意思?

c++ - OpenProcessToken 在 ImpersonateLoggedOnUser 后失败

python - 您如何确定一个点位于线段上的其他两个点之间?

c++ - 在远程 session 上运行时,LVS_EX_DOUBLEBUFFER 是否正确地不加倍缓冲?

c# - 用于找峰的数学库

math - float 学有问题吗?

c# - 如何将列表的内容放入单个 MessageBox 中?

javascript - 将函数及其参数传递给另一个函数来执行它的语法是什么?

c++ - 替换枚举以最大化编译时间检查的最佳方法

c++一旦达到数组大小或eof就停止循环