如果语句通过输出问题下降
if statement fall through output issues
每当我输入其他情况并输入满足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;
}
您需要删除";"。否则,最后一个代码块将始终执行,因为它不是其他块的一部分。
相关文章:
- 可能我知道为什么这段代码没有给出任何输出吗?
- 有没有办法我可以在 C++ 中使用 if 语句来说明如果一个字符串不是两个名称的 eqal,它会给出一个输出,ATM i
- 为什么带有逻辑和运算符以及增量运算符的 c++ 程序给出的输出与预期不同?
- C++向量排序给出0作为输出
- 奇怪的数字随机出现在输出中.(C++)
- 如何从Visual Studio中的给出.pyd输出中构造Python代码
- 我将 c++ 矢量数据集转换为 c 数组.我将相同的数据集传递到一个应该给出相同输出的函数中
- 向量<对<双精度,双精度>>给出问题
- 为什么这段代码给出的输出与预期不同?
- C 反向一个不给出预期输出的数组
- 矢量在超出边界时给出0作为输出
- 我很确定我犯了一个愚蠢的错误,但是这段代码给出的输出太长而无法阅读
- C++ 中的侵蚀函数不给出任何输出
- 为什么字符串变量"sum"不存储任何数据,程序不给出任何输出
- 输入层类型:图像数据在窗口咖啡cpp中给出空白输出
- 在队列中,插入和删除后,被删除的元素也会出现在输出中.为什么
- 服务器未收到UDP数据包,花费数小时试图找出问题所在
- 程序给出 0 作为输出 (C++)
- C++ 输出找出问题
- 在成功运行10小时后,在发布模式下从windows应用程序中获得异常,如何检测出问题