如果/else语句未显示/可变分配问题的输出;初学者程序员
Output for if/else statement not showing/variable assignment issues; Beginner programmer
我是编程的新手,所以我对此代码不起作用感到困惑。例如,如果我输入"C"
的车辆,而1
则在小时和几分钟内输入,则它只是在那里停止,并且不会进入if
块。我知道它缺少else
的部分,但要注意我尝试了一下,但没有区别。一旦我输入一个值几分钟,程序就可以到达Press any key to continue...
状态。请帮助?
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
char vehicle;
int hours, minutes;
cout << fixed << showpoint << setprecision(2);
cout << "If your vehicle is a car, please enter 'C'" << endl;
cout << "If your vehicle is a truck, please enter 'T'" << endl;
cout << "If you are a senior citizen, please enter 'S'" << endl;
cout << "nEnter here: ";
cin >> vehicle;
cout << "nEnter the number of hours you have been parked: ";
cin >> hours;
cout << "nEnter the number of minutes you have been parked: ";
cin >> minutes;
if (vehicle == ('C' || 'c') && minutes <= 30)
{
if (hours <= 2)
cout << "Free" << endl;
}
system("PAUSE");
return 0;
}
if (vehicle == ('C' || 'c') && minutes <= 30)
不做您认为做的事情。您需要使用:
if ( (vehicle == 'C' || vehicle =='c') && minutes <= 30)
您可以将其简化为:
if ( toupper(vehicle) == 'C' && minutes <= 30)
您的程序有逻辑错误(或者您可以说导致逻辑错误的语法eRROR)。
在情况下,您应该检查大写和下箱的车辆:
if ((vehicle == 'C' || vehicle == 'c') && minutes <= 30)
您正在检查是否有" C"或" C"的车辆,但在不知不觉中您正在对" C"answers" C"进行位操作。这些是初学者的错误,也是学习的组成部分。
相关文章:
- C++格式化输出问题
- 输出错误,问题是找到总和5000位数字cpp
- 无法在问题解决方案中执行输出逻辑
- C++ 每次运行程序时我都会"nan"输出的问题
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- OpenCV 傅里叶变换复杂输出问题
- 阶乘问题在 c++ 中给出错误的输出
- 在 C++ 中实现简单链表时出现问题,没有输出
- 是给定代码中的任何更改,以便我可以为问题提供正确的输出
- 在黑客地球上没有获得以下C ++代码问题的输出
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- 我的输出结果是 0 英寸C++.可能是什么问题
- c ++:我没有得到正确的输出(菜鸟问题)
- 代码输出随机符号,我不确定出了什么问题
- C++问题,我的两个源代码应该具有相同的输出,但它们是不同的
- C++ 回文程序总是给出 0(假)作为输出问题;我的代码哪里有问题?
- 未获得 N-皇后问题的预期输出
- 堆排序,我无法弄清楚我的代码出了什么问题,输出顺序不正确
- 编译 gsoap 输出文件 (onvif) 时出现问题