Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm new coding I don't understand why else if statement displays the output from if statement despite of not error. If statement should deduct a %50 when phone call is at specific time a day and deduct an extra %15 off when phone call is over 60. Second statement should take only %50 off because the condition is true, after way too many changes else if output displays both discount despite of being lower than 60.

Thanks you so much.

C++
    cout << "Enter start time: ";
    cin >> startcall;
    cout << "Enter length of call in minutes: ";
    cin >>  duration;
    
    if (duration > 60 || startcall >=1800 || startcall < 800) {
       discount = (duration * rate /2)*0.15;
       grosscost = ((((duration * rate/2)-discount)) * 0.40) + (((duration * rate/2)-discount));
       netcost = grosscost;
        
    }else if(duration <60 || startcall >=1800 ||startcall <800){
        grosscost = ((duration *rate /2)*0.40) + (duration *rate/2);
        netcost = grosscost;
    }
    
    cout <<"Gross Cost: $ " <<grosscost <<endl;
    cout <<"Net cost: $ " <<netcost <<endl;
    

    return 0;

}


What I have tried:

I tried to change the variable's names, I checked the curly bracket order. I wrote down the operation on paper several times. I checked programing books, they give me examples of how functions work, but I think I need an example to understand what I'm doing wrong.

thanks
Posted
Updated 7-Oct-18 19:35pm
v3
Comments
Member 14010405 7-Oct-18 4:14am    
s not working after a few changes

if (duration >60 || startcall >=1800 || startcall < 800) {
discount = (duration * rate /2)*0.15;
grosscost = ((((duration * rate/2)-discount)) * 0.40) + (((duration * rate/2)-discount));
netcost = grosscost;
}
else{if( startcall >=1800 ||startcall <800 ||duration <60){
grosscost = ((duration *rate /2)*0.40) + (duration *rate/2);
netcost = grosscost;
}
}

cout <<"Gross Cost: $ " <<grosscost <<endl;
cout <<"Net cost: $ " <<netcost <<endl;


return 0;

Enter length of call in minutes :40
Gross Cost: $ 9.52
Net cost: $ 9.52

This is what i don't understand. It keeps deducting %15 off despite of being lower than 60.

The result should be $11.20

11.20- 9.52 = 1.68 (which is the %15 when calling over 60 minutes)

thanks for any help
Patrice T 7-Oct-18 4:25am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

The way you have written the if statements is wrong.
You probably meant (please note you may profitably refactor such a code)
C++
if (duration > 60 && (startcall >=1800 || startcall < 800) )
{
  discount = (duration * rate /2)*0.15;
  grosscost = ((((duration * rate/2)-discount)) * 0.40) + (((duration * rate/2)-discount));
  netcost = grosscost;

}
else if(duration < 60 && ( startcall >=1800 ||startcall <800) )
{
  grosscost = ((duration *rate /2)*0.40) + (duration *rate/2);
  netcost = grosscost;
}

However it is just a guess, because you didn't provide enough details.
 
Share this answer
 
Look at what you are doing. I'll shorten it a bit so identical bits are more obvious:
C++
if (a || b || c)
   x;
else if ( d || b || c)
   y;

That refactors - just to make it more obvious what is happening - to this:
if (a || b || c)
   x;
else 
   if ( d || b || c)
      y;
And "||" means "OR" - if either of the conditions on the left or right hand side are true, the whole expression is true.
So if either b or c are true, then the whole of the first condition is automatically true. Which means that if either b or c is true, the else condition will always be false, and they never contribute to the condition at all: the code in the else block will only get executed is d is true.

In your case, the only time the else if expression can be executed is if both b and c are false: if startcall is between 1800 and 800 and that remains true for the second test, so the second test will always be just duration < 60

I'd suggest that you start by reading the question again, and applying the discount in separate tests:
C++
if (right time of day)
   {
   apply 50% discount;
   if (call is long enough)
      {
      apply extra discount;
      }
   }
 
Share this answer
 
v2
Thanks so much for your time. Program is working properly.
I had some missing curly brackets and some statements were not in a logical order either. for example I was calculating grosscost and deducting a discount value before calculating discount.

if(duration >60 && (startcall >=1800 || startcall < 800)) {
    discount = (duration * rate /2)*0.15;
    grosscost = ((((duration * rate/2)-discount)) * 0.040) + (((duration * rate/2)-discount));
    netcost = grosscost;
 
Share this answer
 
When you use the debugger you find out what your code is doing.

Advice: write some else code for the case that the else if isnt true.

C++
else if(duration <60 || startcall >=1800 ||startcall <800){
        grosscost = ((duration *rate /2)*0.40) + (duration *rate/2);
        netcost = grosscost;
    } else {
      //some code 
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900