为什么这些简单的功能给我天文数字大小?

时间:2015-02-07 20:09:15

标签: 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;
}

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:-2)

首次创建变量时,没有小时值或付费。因此,例如,声明net = hours * pay应该等于零,这样就可以解释为什么你的结果可能不准确。尝试将输入函数切换到变量赋值之前,看看是否有诀窍