簡體   English   中英

If else語句使用大小寫字母

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

所以我有麻煩。 我的教授希望我編寫一個基本程序,該程序會增加成本並根據使用if else語句的有線電視公司的住宅套餐或商務套餐輸出有線電視公司的費率。 我唯一遇到的問題是他希望用戶能夠輸入'R'或'r'大寫字母或小寫字母,與'B'或'b'相同。

我在做這個

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

但是如果我使用的不是R或r,它不會移至下一個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 :: tolowerstd :: toupper使輸入字符統一,以便分別與小寫或大寫字母進行比較。

例如,用於小寫字母比較:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM