利用 char 确定输出不起作用

Posted

技术标签:

【中文标题】利用 char 确定输出不起作用【英文标题】:Utilizing char to determine output isnt working 【发布时间】:2015-10-06 14:45:27 【问题描述】:

我正在尝试这样做,因此当用户输入 r 或 p 时,将执行不同的部分,但由于某种原因,它无法识别单个字符输入。它只是将我带到第一个 if 语句,说我没有正确的值,然后不管我输入什么都为 r 执行。为什么这对我不起作用?我用字母本身和相应字母的 ascii 数字尝试了它,每次它都做同样的事情。它甚至没有输出说无效服务代码重试的最终语句。我的程序看起来像这样。提前感谢您的宝贵时间!

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()

    char servicetype;
    int account;
    double minutes;
    double initialCharge;
    double overCharge;
    double day;
    double night;
    double dayrate;
    double nightrate;
    double balance;
    string service;

    cout << "Please enter your account number: ";
    cin >> account;
    cout << endl;

    cout << "Please enter your service code: ";
    cin >> servicetype;
    cout << endl;

    if (servicetype != 'r' || 'R' || 'p' || 'P')
    
        cout << "Invalid service code entered. Please enter a valid service code of P for premium service or R for regular service: ";
        cin >> servicetype;
        cout << endl;
    

    else if (servicetype == 'r' || 'R')
    
        service = "regular";

        initialCharge = 10.00;
        overCharge = .2;

        cout << "Please enter the number of minutes the service was used: ";
        cin >> minutes;
        cout << endl;

        balance = initialCharge + (minutes * overCharge);

        cout << "Account number " << account << " with the " << service << " service which was utilized for " << minutes << " minutes and therefore a balance is due of $" << fixed << setprecision(2) << balance << endl;
    
    else if (servicetype == 'p' || 'P')
    
        service = "premium";

        initialCharge = 25.00;
        cout << "Please enter the number of minutes which were used between the hours of 6:00 am and 6:00 pm: ";
        cin >> day;
        cout << endl;

        cout << "Please eneter the number of minutes which were used between the hours of 6:00 pm and 6:00 am: ";
        cin >> night;
        cout << endl;

        if (day < 75)
            dayrate = 0;
        else
            dayrate = (day - 75) * .1;

        if (night < 100)
            nightrate = 0;
        else
            nightrate = (night - 100) * .05;

            balance = initialCharge + nightrate + dayrate;

            minutes = day + night;

            cout << "Account number " << account << " with the " << service << " service which was utilized for " << minutes << " minutes and therefore a balance is due of $" << fixed << setprecision(2) << balance << endl;
    
    else
        cout << "An invalid service code was entered, please try again." << endl;


    system("pause");

    return 0;

【问题讨论】:

我知道我可以使用 switch 代替 if 并且它可能会起作用,但我现在有点想知道为什么它不能使用 if 语句 || 是一个 operator ,这意味着它接受输入并给出输出。 || 运算符接受两个输入,并输出truefalse。您已将'r''R' 的输入提供给您的第一个||。结果将是true,因为至少有一个输入不是假的。 【参考方案1】:

您需要将serviceType 与每个字母进行比较。事实上,你基本上是在说:

“如果 serviceType 不是 'r' 或如果 'R' ... etc”总是评估为true,因为'R'(它被转换为它的等效数字)评估为true。这类似于说:

if ('R')


您可以通过将serviceType 与每个字符进行比较来修复它:

if (serviceType != 'r' && serviceType != 'R' && serviceType != 'p' && serviceType != 'P')


您可以使用tolower(或toupper)稍微简化它:

if (tolower(serviceType) != 'r' && tolower(serviceType) != 'p')


【讨论】:

应该是tolower((unsigned char)serviceType),或者tolower(serviceType, std::locale())。 Reference

以上是关于利用 char 确定输出不起作用的主要内容,如果未能解决你的问题,请参考以下文章

列出所有打开的设备 AL 不起作用

H2 删除并创建别名 to_char 不起作用

Char 数组在 C++ 中不起作用

为啥 memset 功能不起作用? [复制]

SQL Server 2008 varchar 和 char 不起作用

Laravel - 与资源的关系和加载时不起作用