每当我输入不同的情况并输入符合if语句要求的小时时,其他人也会被打印出来,帮助我只需要从每种不同的情况中输出一个。 以下示例是我得到的错误。 例如:输入'a'输入'11'输出'到期金额为11.95''到期金额为9.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 c\n";
cin>>pack;
cout<<"Enter how many hours were that month used\n";
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 hours\n";
}
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 hours\n";
}
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 hours\n";
}
else if (hours<744);{
due=19.95;
cout<<"The amount due is $"<<due<<endl;
}
break;
}
system("PAUSE");
return 0;
}
答案 0 :(得分:3)
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;
}
您需要删除“;”。否则,将始终执行最后一个代码块,因为它不是else块的一部分。