0

Some insurance companies offer discounts on car insurance premiums depending on the number of years the driver has had a driver's licence and the number of claims the driver has made in the last five years. In this program, the user inputs (as integers) •years the number of years the driver has had a driver's license •claims the number of insurance claims made in the last five years The (integer) percentage of the "standard premium"that will be charged is initially calculated as follows: •if years< 5 then percentage = 100 –10 * years + 20 * claims •otherwise ("else") percentage = 50 + 20 * claims The percentage calculated in this way is then further adjusted as follows: •if percentage> 150 then insurance is refused •otherwise,if the percentage is between 100% and 150% then set percentage = 100 •(otherwise,the percentage isn't adjusted)

cout << "enter years licenced: " << endl;
cin >> years;
cout << "enter number of claims: " << endl;
cin >> claims;
if (years < 5)
{
    percentage = 100 - (10 * years) + (20 * claims);
    cout << "percentage from 0 to 5:" << percentage << endl;
}
else (years > 5);
{
    percentage = 50 + (20 * claims);
    cout << "percentage higher than 5:" << percentage << endl;
}
if (percentage > 150)
{
    cout << "insurance is refused." << percentage << endl;
}
else if (100 <= percentage <= 150)
{
    cout << "percentage = 100." << endl;
}
else;
{
    cout << "insurance is refused." << endl;
}
return 0;
pts
  • 80,836
  • 20
  • 110
  • 183
Reza
  • 1
  • Please post the entire C++ source file you are writing. Please include the input you give it and both the expected and actual output. – pts Aug 19 '20 at 11:25
  • 1
    To debug and fix problems like this, you should run your program in a debugger, and single-step over each line. It would reveal which lines are executed. Another option is printing extra lines, e.g. `cout << "DEBUG3=" << (100 <= percentage <= 150) << endl;`. – pts Aug 19 '20 at 11:33

1 Answers1

0
  1. Replace

    else if (100 <= percentage <= 150)
    

    with

    else if (100 <= percentage && percentage <= 150)
    

    In C++, chaining of comparison operators works differently than in math. To get the math meaning, don't use chaining, and connect individual comparisons with logical and (&&).

  2. Replace

    else (years > 5);
    

    with

    else if (years > 5)
    

    The ; is equivalent to {}, thus it makes the else branch empty, and the code below (the multiline {...}) would be executed unconditionally.

  3. Replace

    else;
    

    with

    else
    
pts
  • 80,836
  • 20
  • 110
  • 183
  • thank you very much but when replacing else (years > 5); with else (years > 5) i get an error saying i need the (;). – Reza Aug 19 '20 at 11:47