为什么我的计算结果为0?

时间:2017-10-03 15:28:30

标签: c++

当工作时数低于40时,总工资的结果总是为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 个答案:

答案 0 :(得分:0)

我无法重现您提出的问题,但是如果小时数正好是40,我可以看到您的代码中存在错误。假设这是什么你的意思是,这部分

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;

未使用初始化率。这将导致速率乘以小时数的结果未定义,您观察到的确切行为将取决于您使用的编译器。