bretth18
bretth18

Reputation: 53

c++ && conditional never executed

I'm trying to create a program for an assignment that computes the rate for a long distance call, that takes in the starting time and length of call and outputs the gross cost and net cost. There are several parameters involved, which i've implemented in conditional statements,

However, the first conditionals code is never executed and I've tried several different operands and still have not figured it out.

#import <iostream>


using namespace std;

int main() {

//declare variables
int startTime;
int callLength;
double grossCost = 0;
double netCost;
const double callRate = 0.35;
const double fedTax = 0.04;
const double discount1 = 0.50;
const double discount2 = 0.16;


//begin user prompt
cout << "Enter start time: " << endl;
cin >> startTime;
cout << "Enter length of call in minutes: " << endl;
cin >> callLength;

//define our grossCost
grossCost = (callLength * callRate);

//lez do some conditionals
    if ((startTime >= 1800) && (startTime <= 800)){

        netCost = grossCost - (grossCost * discount1);
        netCost *= fedTax;
    }

    else if (callLength > 60){
        netCost = grossCost - (grossCost * discount2);
        netCost *= fedTax;
    }
    else{
        netCost = (grossCost * fedTax);
    }

    //print out our final costs
    cout << "gross cost : " << "$" << grossCost << endl;
    cout << "net cost: " << "$" << netCost << endl;

    return 0;

    }

So, when trying an input of:

Enter start time: 2322
Enter length of call in minutes: 67
gross cost : $23.45
net cost: $0.78792

The netcost is not being evaluated correctly, as it is skipping the first conditional, even though the values of startTime meet the parameters. The output should be:

    Enter start time: 2322
    Enter length of call in minutes: 67    
    gross cost: $23.45
    net cost: $10.24 

I'm not looking for someone to do my homework, just some pointers to lead me in the right direction, i've gone over the conditional several times and I'm still confused as to why it's not evaluating. Any help is appreciated, thanks.

Upvotes: 0

Views: 142

Answers (4)

iksemyonov
iksemyonov

Reputation: 4196

You have a logic error here: a number can not be greater or equal than 1800 and less or equal than 800 at the same time. Revise the first conditional. I would break it in two parts

if((time >= 1800 & <=2400) || (time >= 0 && time <= 800)).

The actual logical mistake here is that calls after 6PM and before 8AM would exist in different days! So we do need two conditions that check on the days' boundaries as well, combined.

Edit per comment by @Reto Koradi:

The outcome won't change if we omit the boundary checks. As mentioned above, the gist of the change is to highlight the mutual exclusiveness of th two conditions, which is achieved by swapping the && out with an ||:

if (time >= 1800 || time <= 800)

Upvotes: 4

Jorge Torres
Jorge Torres

Reputation: 1465

The problem is

if ((startTime >= 1800) && (startTime <= 800)){

The two conditions are mutually exclusive. There is no number greater than 1800 and at the same time smaller than 800. You might want to fix the comparison condition like this (improved for future readers thanks to iksemyonov's comments. Please refer to his answer to this same question)

if ((startTime >= 1800) || (startTime <= 800)){

Upvotes: 2

Reto Koradi
Reto Koradi

Reputation: 54642

The explanation why it's not working is fairly obvious, and already covered in other answers. This expression can never be true because no number is both greater than 1800 and also less than 800.

if ((startTime >= 1800) && (startTime <= 800)){

The && operator tests for both conditions to be true.

The more interesting part is why people make this mistake. And you're neither the first nor the last person to fall into this trap when starting out with logical expressions.

I believe the reason is that in natural language, we use the word "and" in ways that are very different from the logical operator. When you say "before 8 am and after 6 pm", the "and" corresponds much more to the "union" set operation. You're basically defining a set that contains "all times before 8 am" and "all times after 6 pm", which is a set union.

The logical operator that matches the "union" set operation is the logical "or". Any element that is in at least one of the input sets is part of the union. So for the union of two sets, the union is the set with elements that are in the first set or the second set.

With that understood, the correct condition is:

if ((startTime >= 1800) || (startTime <= 800)){

where || is the logical "or" operator.

Logical operations working differently from the way they are used in everyday language is not unusual at all. So you always need to be careful when translating language to logical expressions. Another popular example is that "or" in natural language mostly corresponds to an exclusive or, while the "or" operator is inclusive. So if somebody tells you that "you can take it or leave it", it means that you have to decide, while the standard logical "or" operator would actually allow you to do both.

Upvotes: 2

Jon Kiparsky
Jon Kiparsky

Reputation: 7753

How can x > 1800 and x < 800 ever be true at the same time?. If the first is true, the statement will always be false.

Upvotes: 1

Related Questions