首页 > 解决方案 > 即使使用 toupper 循环也不接受小写(C++)

问题描述

目前,我希望我的代码接受小写或大写 a、b、c 或 u 作为来自用户的有效条目。但是,每当我将字符输入为小写时,它们都会以错误消息响应并继续循环,直到将其输入大写为止。我是 C++ 新手,所以我可能使用错误的 toupper。

#include <iostream>

using namespace std;

int main()
{
    bool custGBTypeValid = false;
    bool custPlnTypeValid = false;
    char custPlanType = toupper('Z');
    int custUsedData = 1;

    cout << "Hello, welcome to AT&T wireless. We're here to help you decide if your current plan is what's right for you." << endl;
    cout << "Here are our plans:" << endl;
    cout << "Plan A: For $25 per month 0GB are provided. Data is $10 per GB." << endl;
    cout << "Plan B: For $45 per month 2GB are provided." << endl;
    cout << "Plan C: For $80 per month 6GB are provided." << endl;
    cout << "Plan Unlimited: Unlimited data for $100 per month." << endl;

    while (custPlnTypeValid == false)
    {
        cout << "What type of plan are you on? (Please answer with A, B, C, or U): ";
        cin >> custPlanType;
        if (custPlanType == toupper('A') || custPlanType == toupper('B') || custPlanType == toupper('C') || custPlanType == toupper('U'))
            custPlnTypeValid = true;
        else
            cout << "ERROR: Incorrect data type entered." << endl;
    }
}

我如何让它也接受小写字母?我也尝试将 if 语句中的每个更改为custPlanType == toupper('a')etc.,toupper(custPlanType == 'A')但这也不起作用。如果代码中的字符是小写的,则后者有效,但随后拒绝使用大写字符。

标签: c++toupper

解决方案


It should be:

if (toupper(custPlanType) == 'A' ....) 

推荐阅读