如果/else语句未显示/可变分配问题的输出;初学者程序员

Output for if/else statement not showing/variable assignment issues; Beginner programmer

本文关键字:问题 输出 初学者 程序员 分配 语句 else 显示 如果      更新时间:2023-10-16

我是编程的新手,所以我对此代码不起作用感到困惑。例如,如果我输入"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"进行位操作。这些是初学者的错误,也是学习的组成部分。