c++ - 使用大小写字母的 if else 语句
问题描述
所以我遇到了麻烦。我的教授希望我编写一个基本程序,该程序根据有线电视公司的住宅套餐或使用 if else 语句的商业套餐来计算成本并输出费率。我遇到的唯一问题是他希望用户能够输入“R”或“r”大写或小写,与“B”或“b”相同。
我在做这个
if(customer_Type=='R' || 'r')
但如果我使用 R 或 r 以外的任何内容,它不会移动到下一个 else if 语句。程序下面的代码完全按照我的意愿工作,但没有小写字母
cout<<"Welcome to Cable Company billing procedure.\n";
cout<<"Enter your account number : ";
cin>>customer_Account_Number;
cout<<"Enter your customer type (residential input R or business input B) : ";
cin>>customer_Type;
-
if(customer_Type=='R') // If residential
{
cout<<"How many premium channels have you subscribed to?\n";
cin>>num_Of_Prem_Channels;
amount_Due = ResBillProcFee + ResBasicServCost + ResCostPremChannels * num_Of_Prem_Channels;
cout<<"Your residential bill is $"<<amount_Due<<endl;
}
else if(customer_Type=='B')
{
cout<<"Enter number of premium channels\n";
cin>>num_Of_Prem_Channels;
cout<<"Enter number of basic service connections\n";
cin>>num_Of_Basic_Service_Connections;
if (num_Of_Basic_Service_Connections <= 10)
{
amount_Due = BusBillProcFee + BusBasicServCost + num_Of_Prem_Channels * BusCostPremChannel;
}
else
{
amount_Due = BusBillProcFee + BusBasicServCost + (num_Of_Basic_Service_Connections - 10) * BusBasicConCost + num_Of_Prem_Channels * BusCostPremChannel;
}
cout<<"Your bill is :"<<BusBillProcFee + BusBasicServCost + (num_Of_Prem_Channels * BusCostPremChannel);
}
return 0;
}
解决方案
您需要if
使用 OR||
运算符检查条件中的每个字符:
if ( customer_Type == 'R' || customer_Type == 'r' )
{
// ...
}
否则,您可以使用std::tolower或std::toupper使输入字符统一,以便分别与小写或大写字母进行比较。
例如小写比较:
if ( std::tolower( customer_Type ) == 'r' )
{
// ...
}
推荐阅读
- regex - 正则表达式,我如何提取字符串VBscript的一部分
- typescript - 打字稿字符串枚举访问
- javascript - 调用 PHP 脚本而不期望 js 中的任何返回
- angular - 页面下方的条目列表。就像将 2 个文本框控件添加到 formarray
- algorithm - MIPS中二叉树的最长路径
- java - 从随机持续时间获取平均值
- java - 如何使用 recyclerview 打开一个隐式意图,以便它打开一个 youtube 视频
- javascript - 未填充空白时 Vuetify 步进器警报消息
- javascript - onClick时如何不对按钮的文本应用动画?
- python - 无法使用 peewee (Python) 在 postgresql 数据库上执行错误:IndexError: tuple index out of range