労働時間が40時間を下回った場合、総給与の結果は常に0になります。なぜですか?コードを並べ替えてみましたが、まだ同じことをしています。私はここで間違って何をしていますか?計算結果が0になるのはなぜですか?
#include <cstdio>
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
int skill, ins, hour, ret;
double rate;
cout << "Input Skill Level (1, 2, or 3)\n";
cin >> skill;
cout << "Input Hours Worked\n";
cin >> hour;
if (skill==1 &&hour<40) rate = 17;
else if (skill==2 && hour<40) rate = 20;
else if (skill==3 && hour<40) rate = 22;
else if (skill==1 && hour>40) rate = 25.5;
else if (skill==2 && hour>40) rate = 30;
else if (skill==3 && hour>40) rate = 33;
int pay = hour * rate;
int over = 0.5 * pay;
if (skill==1 &&hour<40) over = 0;
else if (skill==2 && hour<40) over = 0;
else if (skill==3 &&hour<40) over = 0;
int gross = pay + over;
switch (skill)
{
case 1:
{
if (hour>40)
{
cout << "You are Skill Level 1 and your regular pay is " <<pay << " and your overtime pay is "<< over<< " and your total pay is " << gross <<endl;
}
else if(hour<40)
{
cout << "You are Skill Level 1 and your regular pay is " <<gross << endl;
}
break;
}
case 2:
{
if (hour>40)
{
cout << "You are Skill Level 2 and your regular pay is " <<pay << " and your overtime pay is "<< over<< " and your total pay is " << gross <<endl;
}
else if(hour<40)
{
cout << "You are Skill Level 2 and your regular pay is " <<gross << endl;
}
break;
}
case 3:
{
if (hour>40)
{
cout << "You are Skill Level 3 and your regular pay is " <<pay << " and your overtime pay is "<< over<< " and your total pay is " << gross<< endl;
}
else if(hour<40)
{
cout << "You are Skill Level 3 and your regular pay is " <<gross << endl;
}
break;
}
default: cout << "Invalid Input" << endl; break;
}
if (skill==2 || skill==3) cout << "Select one of the following Insurance Options \n1 for Medical Insurance \n2 for Dental Insurance\n3 for Long-term Disability Insurance\n";
else cout << "Thank you for using our program\n";
cin >> ins;
switch(ins)
{
case 1: cout << gross - 32<< " is your final pay\n";break;
case 2: cout << gross - 20<< " is your final pay\n";break;
case 3: cout << gross - 10<< " is your final pay\n";break;
default: cout << "Invalid Input\n";
}
if (skill==3) cout << "Do you want to take our Retirement Plan? \n1 for Yes\n2 for No\n";
else cout << "Thank you for using out program\n";
cin >> ret;
if (ret==1) cout << "You have took our Retirement Plan and your Final Pay becomes "<< 0.97 * gross << endl;
else if (ret==2) cout << "You did not took our Retirement Plan and your Final Pay remains the same at " << gross << endl;
else cout << "Invalid Input\n";
cout << "Thank you for using our program, Hope you enjoy it and May You Have A Good Day!";
}
ようこそスタックオーバーフロー!デバッガを使用してコードをステップ実行する方法を学ぶ必要があるようです。良いデバッガを使用すると、プログラムを1行ずつ実行し、どこからずれているかを確認することができます。これはプログラミングをする場合に不可欠なツールです。さらに読む:[小さなプログラムをデバッグする方法](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – NathanOliver
浮動小数点と整数の切り捨て問題ですタイプ。 –
C++教科書のサンプルのように、コードを書式設定してください。それがここにあるので、それは読むことができません。 –