c++ - 即使使用 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')
但这也不起作用。如果代码中的字符是小写的,则后者有效,但随后拒绝使用大写字符。
解决方案
It should be:
if (toupper(custPlanType) == 'A' ....)
推荐阅读
- javascript - 如何将运行时定义的变量、函数、类添加到 CodeMirror 自动完成列表,并在从 JS 中的代码中删除时删除它们
- node.js - 配置文件 .eslintrc 是否已弃用以支持 .eslintrc.* 文件?
- c# - Selenium C# 禁用 Microsoft Chromium Edge 浏览器同步弹出窗口
- java - 具有多个条件的聚合
- flutter - 在“initState”内或在类定义下方初始化变量?
- node.js - 如何在特定字符串或关键字之后获取字符串
- vue.js - v-combobox - 具有自定义项目外观的大型列表上的性能问题
- deep-learning - U-Net with Pre-Trained ResNet throws 维度错误必须匹配
- javascript - 这个jsx表达式的错误是什么?
- python - SVM 卡在模型拟合中