嵌套if else语句C ++

时间:2018-09-27 19:48:13

标签: c++ if-statement

伙计们,所以我已经完成了这项任务,我正在处理该即时消息遇到的麻烦。作为要求的一部分,我不得不调用所有整数,因此为什么只有59和60仍然是数字。

我的问题是使我无法进行这项工作吗? Ive唯一能够完成的示例输出是第一个。

示例输出1:

Enter start time: 2322
Enter length of call in minutes: 67    
gross cost: $26.80
net cost: $11.85

示例输出2:

Enter start time: 759
Enter length of call in minutes: 10    
gross cost: $4.00
net cost: $2.08

示例输出3:

Enter start time: 1300
Enter length of call in minutes: 100    
gross cost: $40.00
net cost: $35.36

样本输出4:

Enter start time: 1300
Enter length of call in minutes: 10    
gross cost: $4.00
net cost: $4.16    

////////////

//
//  main.cpp
//  Assignment 5
//
//  Created by Jake Anderson on 9/20/18.
//  Copyright © 2018 Jake Anderson. All rights reserved.
//
double grosscost1;
double netcost1;
double netcost2;
double netcost3;
double starttime;
double calllength;
double taxes=.04;
double rate=.40;
double hrdiscount=.15;
double grosscost = (calllength * .40) ;

#include <iostream>
# include <iomanip>
using namespace std;
int eighteenhundred=1800;
int eighthundred=800;
int two=2;
int main()
{

cout << "Enter start time : ";
cin >> starttime;

cout << "Enter length of call in minutes : ";
cin >> calllength;

if (starttime >= eighteenhundred) {
    grosscost = (calllength * rate) ;
    netcost1= (grosscost/two) ;


if (starttime <= eighthundred) {
    grosscost = (calllength * rate) ;
    netcost1= (grosscost/two) ;

} else {

    if (calllength>=60) {
        netcost2= netcost1-(netcost1 * hrdiscount);
        netcost3= netcost2 + netcost2 * taxes;

    if (calllength<=59) {
        netcost3= netcost2 + netcost2 * taxes;
    }
cout << fixed << std::setprecision(2) << "gross cost: $" << grosscost << endl;
cout << fixed << std::setprecision(2) << "net cost: $" << netcost3 << endl;



}





}

}

}

1 个答案:

答案 0 :(得分:1)

如果花括号的缩进准确对齐,则代码将变得更加可读:

int main()
{
    cout << "Enter start time : ";
    cin >> starttime;

    cout << "Enter length of call in minutes : ";
    cin >> calllength;
    if (starttime >= eighteenhundred)
    {
        grosscost = (calllength * rate) ;
        netcost1= (grosscost/two) ;
        if (starttime <= eighthundred)
        {
            grosscost = (calllength * rate) ;
            netcost1= (grosscost/two) ;
        }
        else
        {
            if (calllength>=60)
            {
                netcost2= netcost1-(netcost1 * hrdiscount);
                netcost3= netcost2 + netcost2 * taxes;

                if (calllength<=59)
                {
                    netcost3= netcost2 + netcost2 * taxes;
                }
                cout << fixed << std::setprecision(2) << "gross cost: $" << grosscost << endl;
                cout << fixed << std::setprecision(2) << "net cost: $" << netcost3 << endl;
            }
        }
    }
}

现在很明显,如果开始时间小于1800,第一个条件将阻止任何输出。