简体   繁体   English

If else语句使用大小写字母

[英]If else statement using upper and lower case letters

So I am having troubles. 所以我有麻烦。 My professor wants me to write a basic program that adds up costs and outputs the rate for a cable company based if its a residential package or a business package using if else statements. 我的教授希望我编写一个基本程序,该程序会增加成本并根据使用if else语句的有线电视公司的住宅套餐或商务套餐输出有线电视公司的费率。 The only issue I am having is he wants the user to be able to input either 'R' or 'r' upper case or lower base, same with 'B' or 'b'. 我唯一遇到的问题是他希望用户能够输入'R'或'r'大写字母或小写字母,与'B'或'b'相同。

I was doing this 我在做这个

if(customer_Type=='R' || 'r')

but it was not moving to the next else if statement if I used anything other than R or r. 但是如果我使用的不是R或r,它不会移至下一个if语句。 With the code beneath the program works exactly how I want it to but just without the lower case letters 程序下面的代码完全按照我的要求工作,但没有小写字母

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;
}

You need to check for each character in if condition with OR || 您需要检查if有OR ||条件的每个字符 operator: 操作员:

if ( customer_Type == 'R' || customer_Type == 'r' )
{
    // ...
}

Otherwise, you can use std::tolower or std::toupper to make the input characters uniform for comparisons with lowercase or uppercase letters respectively. 否则,您可以使用std :: tolowerstd :: toupper使输入字符统一,以便分别与小写或大写字母进行比较。

For example for lowercase comparison: 例如,用于小写字母比较:

if ( std::tolower( customer_Type ) == 'r' )
{
    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM