switch 语句未执行
switch statement is not executing
我有一个C++程序,我必须在程序中实现一个switch语句。 由于某种原因,我不知道 switch 语句没有执行。 整个程序如下所示,http://pastebin.com/VxXFhGkQ。
我在使用时遇到问题的程序部分如下所示,
void processCharges() // function to calculate charges
{
int charges = 0;
// switch statement cannot be applied to strings :(
if(vehicle == "C")
{
cout << "TYPE OF VEHICLE: CAR" << endl;
cout << "TIME IN: " << hh << ":" << mm << endl;
cout << "TIME OUT: " << hhout << ":" << mmout << endl;
cout << "======================================" << endl;
thh = hhout - hh;
tmm = mmout - mm;
int tthh = 0;
if(tmm > 0)
{
tthh = thh + 1;
}
else tthh = thh;
cout << "TOTAL TIME PARKED: " << tthh << endl;
switch(tthh) {
case 1:
if(tthh <= 3) {
charges = 0;
cout << "TOTAL CHARGES:$"<<charges << endl;
break;
}
case 2:
if(tthh >= 4) {
charges = tthh * 1.25;
cout << "TOTAL CHARGES:$"<<charges << endl;
break;
}
}
}
}
switch(tthh)
{
case 1:
case 2:
case 3:
charges = 0;
cout << "TOTAL CHARGES:$"<<charges << endl;
break;
default:
charges = tthh * 1.25;
cout << "TOTAL CHARGES:$"<<charges << endl;
break;
}
显然您的变量tthh
的值与 1 或 2 不同。要找出值是什么,请在带有 print 语句的 switch
语句中添加一个 default
子句并打印出其值。
您的案例陈述写得不正确。您可以取出开关并将其设为 if else 或 if else if。现在它正在寻找 tthh 到 == 1 ||阿拉伯数字
你打开tthh
,测试它是 1 的情况,然后测试它是否小于或等于 3(显然是这样)。
然后你测试用例 2,并测试它是否大于或等于 4,它不能 b(自 2 < 4
以来)。
所以基本上,你的开关做任何事情的唯一情况是如果 tthh == 1。
我会完全删除开关,因为我似乎没有添加任何东西。
这个 switch 语句没有多大意义。 请参阅下面的评论。
switch(tthh) {
case 1:
if(tthh <= 3) { //THIS WILL ALWAYS BE TRUE BECAUSE tthh is 1 here
charges = 0;
cout << "TOTAL CHARGES:$"<<charges << endl;
break;
}
case 2:
if(tthh >= 4) { // THIS WILL NEVER BE TRUE BECAUSE tthh is 2 here
charges = tthh * 1.25;
cout << "TOTAL CHARGES:$"<<charges << endl;
break;
}
}
相关文章:
- 如何及时执行if语句?
- 获取上次执行的返回语句的行
- 如何仅在父循环的所有迭代都为真时才执行嵌套的 if 语句?
- 表单显示对话框函数错误,并且不执行下面的语句
- 如何知道调试时执行了哪个语句
- 在可变函数调用中执行一次语句
- 在多个对象上执行同一语句的零成本抽象
- 我怎样才能接受输入作为双精度值并执行 IF ELSE 语句
- c++:if 语句无法正常运行,并继续执行无效输出
- 为什么 cout 语句在传递指向函数的指针时没有执行?
- c++ IF 语句执行,但添加 ELSE 会显示错误
- C 程序如果功能中的语句不执行.在逻辑上似乎是错误的
- 当您希望在Arduino Uno编程中同时执行不同函数时,使用什么代码/语句
- OR 语句执行所有评估
- C++ Switch 语句在中断后执行
- 强制执行C++语句顺序
- SQLITE 内存不足 无法执行语句
- 如何在c++中执行语句
- 如何使用c++/python程序在终端中运行程序,然后在该程序中执行语句
- QODBCResult::exec: 无法执行语句:"[Microsoft][ODBC SQL Server Driver]COUNT 字段不正确或语法错误