OK, I hope I "wrapped it correctly, but if not, please advise and I will be sure to do this correctly from here.

I am having issues getting my nesting to work. Can I go this deep? Should I try switches? The program function should return values based on age and gender to "rate".

Thanks in advance!
cougarclaws.

#include <iostream>//I/O
#include <iomanip>//output manipulation library
using namespace std;

const double MALE_AGE_14_16 = .095;
const double FEMALE_AGE_14_16 = .080;
const double MALE_AGE_17_21 = .082;
const double FEMALE_AGE_17_21 = .069;
const double MALE_AGE_22_26 = .074;
const double FEMALE_AGE_22_26 = .058;
const double MALE_AGE_27_30 = .062;
const double FEMALE_AGE_27_30 = .052;
const double ALL_AGES_31_64 = .051;
const double ALL_AGES_65_75 = .062;
const double ALL_AGES_75_OVER = .075;



//function determines current insurance rates based on age and gender
   double chart (int currentAge, char gender)

{

    double rate = 0;

    if (gender == 'M' || 'm')
    if (currentAge < 16)
           rate = MALE_AGE_14_16;
    else if ((currentAge > 17) && (currentAge <= 21))
           rate = MALE_AGE_17_21;
    else if ((currentAge > 22) && (currentAge <= 26))
           rate = MALE_AGE_22_26;
    else if ((currentAge > 27) && (currentAge <= 30))
           rate = MALE_AGE_27_30;

    else if (gender == 'F' || 'f')
     if (currentAge < 16)
             rate = FEMALE_AGE_14_16;
    else if ((currentAge > 17) && (currentAge <= 21))
             rate = FEMALE_AGE_17_21;
    else if ((currentAge > 22) && (currentAge <= 26))
              rate = FEMALE_AGE_22_26;
    else if ((currentAge > 27) && (currentAge <= 30))
              rate = FEMALE_AGE_27_30;

    if (gender == 'O')
    if ((currentAge >= 31) && (currentAge <= 64))
           rate = ALL_AGES_31_64;
    else if ((currentAge > 65) && (currentAge <= 75))
           rate = ALL_AGES_65_75;
    else if (currentAge > 75)
           rate = ALL_AGES_75_OVER;

           //test
           cout << rate;

    return rate;

}

//Inquires for user input and disqualifies underage drivers with a warning and exit
int main ()
{
         int currentAge = 0;
         double vehValue = 0;
         double rate;
         double annualPremium = 0;
         char gender = ' ';


         cout << "Insurance Premium Calculation Program"; cout << endl;
         cout << endl;
         cout << "Please enter driver's age: ";
         cin >> currentAge; cout << endl;
         cout << endl;

         if (currentAge < 14){
             cout << "Sorry this applicant is too young to drive"; cout << endl;
             cout << endl << endl;
         system ("PAUSE");
         return 14;
         }

         if (currentAge >= 30){
             gender = 'O';

             rate = chart(currentAge, gender);
             }

         else if (currentAge < 31)
         cout << "Please enter driver's gender (M/F): ";
         cin >> gender; cout << endl;
         cout  << endl;

               rate = chart(currentAge, gender);



         cout << "Enter the approximate value of your vehicle: ";
         cin >> vehValue; cout << endl;
         cout << endl;

         annualPremium = rate * vehValue;
         //test
         cout << rate << endl;
         cout << fixed << showpoint << setprecision(3);
         cout << annualPremium << endl;

        cout << endl << endl;
        system ("PAUSE");
        return 0;
}

Recommended Answers

All 9 Replies

Read the intro threads again to find out how to use code tags.

It's MORE than just
[
]

Ok then, I read them and missed something. "read the threads again" offers little help. Perhaps a link to the proper thread?

I want to do this right, but I am obviously new to programming, so anything you can tell me would be great. I did a search and found some explanations, but there was no "step by step".

This is what I read "To use it, Simply encase your code in:

and

tags".

Thanks,

Is this right?

#include <iostream>//I/O
#include <iomanip>//output manipulation library
using namespace std;

const double MALE_AGE_14_16 = .095;
const double FEMALE_AGE_14_16 = .080;
const double MALE_AGE_17_21 = .082;
const double FEMALE_AGE_17_21 = .069;
const double MALE_AGE_22_26 = .074;
const double FEMALE_AGE_22_26 = .058;
const double MALE_AGE_27_30 = .062;
const double FEMALE_AGE_27_30 = .052;
const double ALL_AGES_31_64 = .051;
const double ALL_AGES_65_75 = .062;
const double ALL_AGES_75_OVER = .075;

    

//function determines current insurance rates based on age and gender
   double chart (int currentAge, char gender)

{
    
    double rate = 0;
    
    if (gender == 'M' || 'm')
    if (currentAge < 16)
           rate = MALE_AGE_14_16;
    else if ((currentAge > 17) && (currentAge <= 21))
           rate = MALE_AGE_17_21;
    else if ((currentAge > 22) && (currentAge <= 26))
           rate = MALE_AGE_22_26;
    else if ((currentAge > 27) && (currentAge <= 30))
           rate = MALE_AGE_27_30;
        
    else if (gender == 'F' || 'f')
     if (currentAge < 16)
             rate = FEMALE_AGE_14_16;
    else if ((currentAge > 17) && (currentAge <= 21))
             rate = FEMALE_AGE_17_21;
    else if ((currentAge > 22) && (currentAge <= 26))
              rate = FEMALE_AGE_22_26;
    else if ((currentAge > 27) && (currentAge <= 30))
              rate = FEMALE_AGE_27_30;
    
    if (gender == 'O')
    if ((currentAge >= 31) && (currentAge <= 64))
           rate = ALL_AGES_31_64;
    else if ((currentAge > 65) && (currentAge <= 75))
           rate = ALL_AGES_65_75;
    else if (currentAge > 75)
           rate = ALL_AGES_75_OVER;
           
           //test
           cout << rate;
           
    return rate;
          
}

//Inquires for user input and disqualifies underage drivers with a warning and exit
int main ()
{
         int currentAge = 0;
         double vehValue = 0;
         double rate;
         double annualPremium = 0;
         char gender = ' ';
         
         
         cout << "Insurance Premium Calculation Program"; cout << endl;
         cout << endl;
         cout << "Please enter driver's age: ";
         cin >> currentAge; cout << endl;
         cout << endl;
         
         if (currentAge < 14){
             cout << "Sorry this applicant is too young to drive"; cout << endl;
             cout << endl << endl;
         system ("PAUSE");
         return 14;
         }
         
         if (currentAge >= 30){
             gender = 'O';
           
             rate = chart(currentAge, gender);
             }
         
         else if (currentAge < 31)
         cout << "Please enter driver's gender (M/F): ";
         cin >> gender; cout << endl;
         cout  << endl;
         
               rate = chart(currentAge, gender);
              
         
         
         cout << "Enter the approximate value of your vehicle: ";
         cin >> vehValue; cout << endl;
         cout << endl;
         
         annualPremium = rate * vehValue;
         //test
         cout << rate << endl;
         cout << fixed << showpoint << setprecision(3);
         cout << annualPremium << endl;

        cout << endl << endl;
        system ("PAUSE");
        return 0;
}

Thanks for all the help, I really appreciate it. If you ever need help on routing or administration, I'd be happy to help you. So far, you have pointed out issues and offered no real suggestions. At least I am doing my best to try and get this. Usually the people here are much kinder. This was a mistake. I wll go away since it is obvious nothing I am doing is acceptable and your tiltle here is "team colleage" At this point I will find another forum or perhaps a friend for some real help.

Thanks again!

commented: When I do, I make sure to read ALL the forum rules to present my case in the best possible way; Not go on a bender arging about rules and how I'm special enough not to pay any attention -7
commented: Agreed. 100% +1

You need braces.

if (gender == 'M' || 'm')
{
    if (...)
        do something
    else if(...)
        do something
    else
        do something else
}

The problem is that if (with no braces) only sees the next line that follows it. So if you do this....

if (you == "boy")
if (age < 20)

Then it tests if you're a boy. Then it tests if you're less than 20. But this doesn't test for "you're a boy that's less than 20". If you use braces around everything you want included with your if statement then it does. So, it should be...

if (you == "boy")
{
    if (age < 20)
}

To further explain what I meant above this is a snippet of your code corrected:

if (gender == 'M' || 'm')
{
  if (currentAge < 16)
    rate = MALE_AGE_14_16;
  else if ((currentAge > 17) && (currentAge <= 21))
    rate = MALE_AGE_17_21;
  else if ((currentAge > 22) && (currentAge <= 26))
    rate = MALE_AGE_22_26;
  else if ((currentAge > 27) && (currentAge <= 30))
    rate = MALE_AGE_27_30;
}
#include <iostream>//I/O
#include <iomanip>//output manipulation library
using namespace std;

const double MALE_AGE_14_16 = .095;
const double FEMALE_AGE_14_16 = .080;
const double MALE_AGE_17_21 = .082;
const double FEMALE_AGE_17_21 = .069;
const double MALE_AGE_22_26 = .074;
const double FEMALE_AGE_22_26 = .058;
const double MALE_AGE_27_30 = .062;
const double FEMALE_AGE_27_30 = .052;
const double ALL_AGES_31_64 = .051;
const double ALL_AGES_65_75 = .062;
const double ALL_AGES_75_OVER = .075;



//function determines current insurance rates based on age and gender
double chart (int currentAge, char gender)
{

  double rate = 0.0;


  if (gender == 'M' || 'm')
  { /* Need to add the braze.  Otherwise, the statement 'else if  (gender == 'F' || 'f')
     will be treated as part of options for 'if (currentAge < 16)'.*/
    if (currentAge < 16)
      rate = MALE_AGE_14_16;
    //else if ((currentAge > 17) && (currentAge <= 21))
    /* You need to change the condition. Otherwise, 17 will be left out.
    else if ((currentAge >= 17) && (currentAge <= 21))
      rate = MALE_AGE_17_21;
    //else if ((currentAge > 22) && (currentAge <= 26))
    else if ((currentAge >= 22) && (currentAge <= 26))
      rate = MALE_AGE_22_26;
    //else if ((currentAge > 27) && (currentAge <= 30))
    else if ((currentAge >= 27) && (currentAge <= 30))
      rate = MALE_AGE_27_30;

    /* Switches will be a good idea too.
       You can use:
    switch (currentAge)
    {
      case 14:
      case 15:
      case 16:
        rate = MALE_AGE_14_16;
        break;
      case 17:
      case 18:
      case 19:
      case 20:
      case 21:
        rate = MALE_AGE_17_21;
        break;
      case 22:
      case 23:
      case 24:
      case 25:
      case 26:
        rate = MALE_AGE_22_26;
        break;
      case 27:
      case 28:
      case 29:
      case 30:
      case 31:
        rate = MALE_AGE_27_30;
        break;
    }
    */
  }
  else if (gender == 'F' || 'f')
  {
    if (currentAge < 16)
      rate = FEMALE_AGE_14_16;
    //else if ((currentAge > 17) && (currentAge <= 21))
    else if ((currentAge >= 17) && (currentAge <= 21))
      rate = FEMALE_AGE_17_21;
    //else if ((currentAge > 22) && (currentAge <= 21))
    else if ((currentAge >= 22) && (currentAge <= 26))
      rate = FEMALE_AGE_22_26;
    //else if ((currentAge > 27) && (currentAge <= 30))
    else if ((currentAge >= 27) && (currentAge <= 30))
      rate = FEMALE_AGE_27_30;
  }
  else if (gender == 'O')
  {
    if ((currentAge >= 31) && (currentAge <= 64))
      rate = ALL_AGES_31_64;
    //else if ((currentAge > 65) && (currentAge <= 75))
    else if ((currentAge >= 65) && (currentAge <= 75))
      rate = ALL_AGES_65_75;
    else if (currentAge > 75)
      rate = ALL_AGES_75_OVER;
  }
  //test
  cout << rate;

  return rate;

}

//Inquires for user input and disqualifies underage drivers with a warning and exit
int main ()
{
int currentAge = 0;
double vehValue = 0;
double rate;
double annualPremium = 0;
char gender = ' ';


cout << "Insurance Premium Calculation Program"; cout << endl;
cout << endl;
cout << "Please enter driver's age: ";
cin >> currentAge; cout << endl;
cout << endl;

if (currentAge < 14)
{
  cout << "Sorry this applicant is too young to drive"; cout << endl;
  cout << endl << endl;
  system ("PAUSE");
  return 14;
}

//if (currentAge >= 30){
else if (currentAge > 30)
{
  gender = 'O';
  rate = chart(currentAge, gender);
}

/*
Thie following 'else if is ambiguous.  What happens if currentAge = 30?
If the intention is to treat ages above 30 separtely, the ccondition needs
to be changed to 'if (currentAge > 30)'.
There is no need for the following 'else if'.  A simple 'else will do.
*/

else if (currentAge < 31)
{ /* Need the braze*/
  cout << "Please enter driver's gender (M/F): ";
  cin >> gender; cout << endl;
  cout << endl;

  rate = chart(currentAge, gender);
}


cout << "Enter the approximate value of your vehicle: ";
cin >> vehValue; cout << endl;
cout << endl;

annualPremium = rate * vehValue;
//test
cout << rate << endl;
cout << fixed << showpoint << setprecision(3);
cout << annualPremium << endl;

cout << endl << endl;
system ("PAUSE");
return 0;
}
commented: a) quit spoon-feeding, b) learn code tags YOURSELF AS WELL - sheesh -7

You are all incredible! Thank you soo much for the help! I see my errors. I just want to improve, and this really helps

Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.