-1

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. 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'.

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. 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;
}
  • 1
    Convert all (relevant) input to lowercase and then you only have one case to check. `customer_Type` here. – David C. Rankin Oct 07 '18 at 05:54
  • Easy way is to convert the input to upper case on entry. Your current code is incorrect. – kiwiron Oct 07 '18 at 05:57
  • 1
    Try `if((customer_Type=='R') || (customer_Type =='r'))`. `if(customer_Type=='R' || 'r')` is equals to `if(customer_Type=='R' || true)`. If you convert the input to upper camel, you should refer https://stackoverflow.com/questions/313970/how-to-convert-stdstring-to-lower-case – Hiroki Oct 07 '18 at 06:06

1 Answers1

5

You need to check for each character in if condition with 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.

For example for lowercase comparison:

if ( std::tolower( customer_Type ) == 'r' )
{
    // ...
}
Azeem
  • 11,148
  • 4
  • 27
  • 40
  • Thanks for the example! I had no idea about that nifty toupper/tolower command –  Oct 07 '18 at 06:06
  • @IsakAngerstig: You're welcome! The documentation links are there also. You may go through those to build an understanding. And, those are *functions*, not commands. :) – Azeem Oct 07 '18 at 06:09