C++ if语句因输出问题而失败
每当我输入一个不同的案例并输入一个小时来满足if语句的要求时,else也会被打印出来,我只需要从每个不同的情况中得到一个输出。 下面的例子就是我得到的错误。 例如:输入“a”输入“11”输出“到期金额为11.95”到期金额为9.95”C++ if语句因输出问题而失败,c++,loops,if-statement,case,C++,Loops,If Statement,Case,每当我输入一个不同的案例并输入一个小时来满足if语句的要求时,else也会被打印出来,我只需要从每个不同的情况中得到一个输出。 下面的例子就是我得到的错误。 例如:输入“a”输入“11”输出“到期金额为11.95”到期金额为9.95” //Libraries #include <iostream> #include <iomanip> #include <cstdlib> using namespace std; //Global Constants //
//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;
//库
#包括
#包括
#包括
使用名称空间std;
//全局常数
//功能原型
//行刑从这里开始
int main(int argc,char*argv[]){
//声明变量
炭包;
整小时;
浮动到期日;
//提示用户
卧铺;
库图尔;
//过程
coutelse(hours您的一些else
和else if
参数后面有分号。哦,“;”之间有一个间谍!我删除了它们,但是我遇到了同样的问题,输出结果通过“You need to remove the”;”—是的,但在else
后面也要放一个if
(或者,如果此条件始终为真,您可以删除(当我删除分号时,我也会收到一个预期错误;before(在else行上。即使在我将分号放在else块中的}之后)。谢谢tony d我终于成功了!谢谢大家的输入!
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;
}