如果语句通过输出问题下降

if statement fall through output issues

本文关键字:出问题 输出 语句 如果      更新时间:2023-10-16

每当我输入其他情况并输入满足if语句要求的一个小时时,还需要打印,请帮助我只需要每种不同情况中的一个输出即可。下面的示例是我遇到的错误。例如:输入'a'输入'11'输出'到期的金额为11.95'

//Libraries
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
//Global Constants
//Functioning Prototypes
//Execution Begins here
int main(int argc, char *argv[]){
//Declare Variables
char pack;
int hours;
float due;
//prompt user
cout<<"Please choose one monthly internet subscription package: a, b, or cn";
cin>>pack;
cout<<"Enter how many hours were that month usedn";
cin>>hours;
//process 
cout<<setprecision(2)<<fixed<<showpoint;
switch (pack){
    case 'a':
        if (hours>=10 && hours<=744){
            hours=(hours-10)*2;
            due=hours;
            due+=9.95;
            cout<<"The amount due is $"<<due<<endl;
        }
        else if (hours>=744){
                cout<<"ERROR: a month cannot exceed 744 hoursn";
        }
        else ( hours<=10);{
            due=9.95;
            cout<<"The amount due is $"<<due<<endl;
        }
        break;
    case 'b':
        if (hours>20 && hours<744){
            hours=(hours-20);
            due=hours;
            due+=14.95;
            cout<<"The amount due is $"<<due<<endl;
        }
        else if (hours>=744){
            cout<<"ERROR: a month cannot exceed 744 hoursn";
        }
        else (hours<=20);{
            due=14.95;
            cout<<"The amount due is $"<<due<<endl;
        }
        break;
    case 'c':
        if (hours>=744){
            cout<<"ERROR: a month cannot exceed 744 hoursn";
        }
        else if (hours<744);{
            due=19.95;
            cout<<"The amount due is $"<<due<<endl;
        }
        break;
    }
system("PAUSE");
return 0;

}

else (hours<=20);{
        due=14.95;
        cout<<"The amount due is $"<<due<<endl;
    }

等效于:

else (hours<=20) {
//Do nothing
}
{
    due=14.95;
    cout<<"The amount due is $"<<due<<endl;
}

您需要删除";"。否则,最后一个代码块将始终执行,因为它不是其他块的一部分。