user3404486
user3404486

Reputation: 1

c++ compiler ignoring first if statement

I am a newby at this and am working on my fist if/else program. I am having trouble getting the first if statement to recognize my input of "r". I tried playing with just one statement at a time I was able to input all the examples of input the teacher gave us with the desired output for residential and business. However when I run the program altogether I have a problem. I select R for residential, 0 for additional connections, 0 for premium channels and instead of output of $18.50 I get the business fee of $75.00. I am sure it is a simple mistake but I can't figure out what I am doing wrong. Can someone who knows how to work an if/else give me some insight on this!

#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <iomanip>
using namespace std;

int main()

{
    const float BASIC_RESIDENTIAL   =   18.50;
    const float BASIC_BUSINESS      =   75.00;
    const float CONNECT_RESIDENTIAL =   6.50;
    const float CONNECT_BUSINESS    =   5.00;
    const float PREMIUM_RESIDENTIAL =   7.50;
    const float PREMIUM_BUSINESS    =   50.00;


    char    customerType;
    int     numOfConnections;
    int     numOfPremiumChannels;
    float   amountCableBill;

    cout << fixed << setprecision(2);
    cout << "Residential or Business [R or B]?   ";
    cin  >> customerType;
    cout << endl << endl;
    cout << "How many Additional Connections?    ";
    cin  >> numOfConnections;
    cout << endl << endl;
    cout << "Total number of Premium Channels:   ";
    cin  >> numOfPremiumChannels;
    cout << endl << endl;


    if (customerType == 'R' || customerType == 'r')
    {
        amountCableBill = BASIC_RESIDENTIAL + CONNECT_RESIDENTIAL * numOfConnections +       PREMIUM_RESIDENTIAL * numOfPremiumChannels;
    }
    //else customerType == 'B' || customerType == 'b';   // unnecessary
    {
        if (numOfConnections <= 9)

            amountCableBill = BASIC_BUSINESS + PREMIUM_BUSINESS * numOfPremiumChannels;

        else
            amountCableBill =  BASIC_BUSINESS + (numOfConnections - 9) * CONNECT_BUSINESS + PREMIUM_BUSINESS *numOfPremiumChannels;
    }

    cout << "Total amount of Cable Bill:  "  << amountCableBill << endl << endl;
    cout << "Press <ENTER> to end..." << endl;



    _getch();

    return 0;
}

Upvotes: 0

Views: 932

Answers (2)

Digital_Reality
Digital_Reality

Reputation: 4738

You need else in the condition (unless you want "some other code" to be executed every time)

if (customerType == 'R' || customerType == 'r')
{
   //Some Code
}
else  //<--Notice else
{
   //Some other code.
}

Upvotes: 1

Alexander Gessler
Alexander Gessler

Reputation: 46607

While the condition else if (customerType == 'B' ...) may be redundant, you still have to put an else before the opening brace of the branch.

It's

if (condition) { code } else { code }

Upvotes: 1

Related Questions