Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Made an insurance calculator and wish to press y for yes to go back to menu.

Options
  • 15-11-2013 6:16pm
    #1
    Banned (with Prison Access) Posts: 531 ✭✭✭


    I am having some problems with this insurance calculator I created. OK So the first problem is that say I calculate an insurance after the calculation is done at the bottom of the program I want it to say "Would you like to calculate another premium? (y/n):"

    In this case y is for yes which I would like it to bring the program back to the menu with my three options and if n is pressed it would say "Thank you for using this program" and exit.

    Lastly I am having a big problem with the if statement where it says if the persons age is less than 17 or greater than 70 it should give an error message. If I select option 2 or even 3 and put a age less than 17 or greater than 70 what happens is first a line is shown then I have to press enter on my keyboard to show the error message of contacting the office for further details. Can anyone help me please?

    Here is my code for everyone to view by the way:
    int yourchoice = 0;

    Console.WriteLine("\t***********************************");
    Console.WriteLine("\t* Insurance Calculator *");
    Console.WriteLine("\t***********************************");
    Console.WriteLine("\t 1. Car Insurance ");
    Console.WriteLine("\t 2. Van Insurance ");
    Console.WriteLine("\t 3. Motorbike Insurance ");
    Console.WriteLine("\t___________________________________");
    Console.Write("\tPlease enter your desired selection: ");
    yourchoice = (int)Convert.ToInt32(Console.ReadLine());

    Console.Clear();

    if (yourchoice == 1)
    { // Declaring my variable
    string name = "";
    double age = 0.0;
    string make = "";
    string model = "";
    string euro = "";
    double cc = 0.0;
    double premium = 0.0;


    //Inputs
    Console.WriteLine("\t***********************************");
    Console.WriteLine("\t* Insurance Calculator *");
    Console.WriteLine("\t***********************************");

    Console.Write("\tPlease enter your name:");
    name = Console.ReadLine();

    Console.Write("\tPlease enter your age:");
    age = (double)Convert.ToDouble(Console.ReadLine());

    Console.Write("\tPlease enter the make of your car:");
    make = Console.ReadLine();

    Console.Write("\tPlease enter the model of your car:");
    model = Console.ReadLine();

    Console.Write("\tPlease enter your cc:");
    cc = (double)Convert.ToDouble(Console.ReadLine());

    //Clear screen
    Console.Clear();

    //Central Code
    if (age >= 17 && age < 25)
    {
    Console.WriteLine("name : " + name);
    Console.WriteLine("age : " + age);
    Console.WriteLine("make : " + make);
    Console.WriteLine("model : " + model);
    Console.WriteLine("cc : " + cc);
    Console.Clear();
    premium = cc - (10 / age * cc);

    Console.WriteLine("\t***********************************");
    Console.WriteLine("\t* Insurance Calculator *");
    Console.WriteLine("\t***********************************");
    Console.WriteLine("\t___________________________________");
    Console.WriteLine("\tPremium for : " + name);
    Console.WriteLine("\tEuro : " + premium);
    } Console.WriteLine("\t___________________________________");
    Console.ReadLine();

    if (age >= 25 && age <= 69)
    {
    Console.WriteLine("name : " + name);
    Console.WriteLine("age : " + age);
    Console.WriteLine("make : " + make);
    Console.WriteLine("model : " + model);
    Console.WriteLine("cc : " + cc);
    Console.Clear();
    premium = cc - (10 / age * cc);

    Console.WriteLine("\t***********************************");
    Console.WriteLine("\t* Insurance Calculator *");
    Console.WriteLine("\t***********************************");
    Console.WriteLine("\t___________________________________");
    Console.WriteLine("\tPremium for : " + name);
    Console.WriteLine("\tEuro : " + premium);
    Console.WriteLine("\t___________________________________");
    Console.ReadLine();

    }

    if (age < 17 || age > 69)
    {
    Console.WriteLine("\tSorry but a quote can not be displayed.");
    Console.WriteLine("\tPlease contact our main office at 1850 246 365 for further details");
    Console.WriteLine("\tApology for any inconvenience's caused.");


    }



    }
    if (yourchoice == 2)
    {
    // Second set of variables
    string yourname = "";
    double yourage = 0.0;
    string carmake = "";
    string carmodel = "";
    string ineuro = "";
    double yourcc = 0.0;
    double yourpremium = 0.0;


    //Inputs
    Console.WriteLine("\t***********************************");
    Console.WriteLine("\t* Insurance Calculator *");
    Console.WriteLine("\t***********************************");

    Console.Write("\tPlease enter your name:");
    yourname = Console.ReadLine();

    Console.Write("\tPlease enter your age:");
    yourage = (double)Convert.ToDouble(Console.ReadLine());

    Console.Write("\tPlease enter the make of your car:");
    carmake = Console.ReadLine();

    Console.Write("\tPlease enter the model of your car:");
    carmodel = Console.ReadLine();

    Console.Write("\tPlease enter your cc:");
    yourcc = (double)Convert.ToDouble(Console.ReadLine());

    //Clear screen
    Console.Clear();

    //Central Code
    if (yourage >= 17 && yourage < 25)
    {
    Console.WriteLine("name : " + yourname);
    Console.WriteLine("age : " + yourage);
    Console.WriteLine("make : " + carmake);
    Console.WriteLine("model : " + carmodel);
    Console.WriteLine("cc : " + yourcc);
    Console.Clear();
    yourpremium = yourcc - (6.6 / yourage * yourcc);

    Console.WriteLine("\t***********************************");
    Console.WriteLine("\t* Insurance Calculator *");
    Console.WriteLine("\t***********************************");
    Console.WriteLine("\t___________________________________");
    Console.WriteLine("\tPremium for : " + yourname);
    Console.WriteLine("\tEuro : " + yourpremium);
    } Console.WriteLine("\t___________________________________");
    Console.ReadKey();

    if (yourage >= 25 && yourage <= 69)
    {
    Console.WriteLine("name : " + yourname);
    Console.WriteLine("age : " + yourage);
    Console.WriteLine("make : " + carmake);
    Console.WriteLine("model : " + carmodel);
    Console.WriteLine("cc : " + yourcc);
    Console.Clear();
    yourpremium = yourcc - (6.6 / yourage * yourcc);

    Console.WriteLine("\t***********************************");
    Console.WriteLine("\t* Insurance Calculator *");
    Console.WriteLine("\t***********************************");
    Console.WriteLine("\t___________________________________");
    Console.WriteLine("\tPremium for : " + yourname);
    Console.WriteLine("\tEuro : " + yourpremium);
    Console.WriteLine("\t___________________________________");
    Console.ReadKey();

    } Console.Clear();

    if (yourage < 17 || yourage > 69)
    {
    Console.WriteLine("\t***********************************");
    Console.WriteLine("\t* Insurance Calculator *");
    Console.WriteLine("\t***********************************");
    Console.WriteLine("\t___________________________________");
    Console.WriteLine("\tSorry but a quote can not be displayed.");
    Console.WriteLine("\tPlease contact our main office at 1850 246 365 for further details");
    Console.WriteLine("\tApology for any inconvenience's caused.");
    Console.WriteLine("\t___________________________________");
    Console.ReadKey();

    }

    }
    if (yourchoice == 3)
    {
    string yourname = "";
    double yourage = 0.0;
    string carmake = "";
    string carmodel = "";
    string ineuro = "";
    double yourcc = 0.0;
    double yourpremium = 0.0;

    //Inputs
    Console.WriteLine("\t***********************************");
    Console.WriteLine("\t* Insurance Calculator *");
    Console.WriteLine("\t***********************************");

    Console.Write("\tPlease enter your name:");
    yourname = Console.ReadLine();

    Console.Write("\tPlease enter your age:");
    yourage = (double)Convert.ToDouble(Console.ReadLine());

    Console.Write("\tPlease enter the make of your car:");
    carmake = Console.ReadLine();

    Console.Write("\tPlease enter the model of your car:");
    carmodel = Console.ReadLine();

    Console.Write("\tPlease enter your cc:");
    yourcc = (double)Convert.ToDouble(Console.ReadLine());

    //Clear screen
    Console.Clear();

    //Central Code
    if (yourage >= 17 && yourage < 25)
    {
    Console.WriteLine("name : " + yourname);
    Console.WriteLine("age : " + yourage);
    Console.WriteLine("make : " + carmake);
    Console.WriteLine("model : " + carmodel);
    Console.WriteLine("cc : " + yourcc);
    Console.Clear();
    yourpremium = yourcc - (6.6 / yourage * yourcc);

    Console.WriteLine("\t***********************************");
    Console.WriteLine("\t* Insurance Calculator *");
    Console.WriteLine("\t***********************************");
    Console.WriteLine("\t___________________________________");
    Console.WriteLine("\tPremium for : " + yourname);
    Console.WriteLine("\tEuro : " + yourpremium);
    } Console.WriteLine("\t___________________________________");
    Console.ReadKey();

    if (yourage >= 25 && yourage <= 69)
    {
    Console.WriteLine("name : " + yourname);
    Console.WriteLine("age : " + yourage);
    Console.WriteLine("make : " + carmake);
    Console.WriteLine("model : " + carmodel);
    Console.WriteLine("cc : " + yourcc);
    Console.Clear();
    yourpremium = yourcc - (6.6 / yourage * yourcc);

    Console.WriteLine("\t***********************************");
    Console.WriteLine("\t* Insurance Calculator *");
    Console.WriteLine("\t***********************************");
    Console.WriteLine("\t___________________________________");
    Console.WriteLine("\tPremium for : " + yourname);
    Console.WriteLine("\tEuro : " + yourpremium);
    Console.WriteLine("\t___________________________________");
    Console.ReadKey();

    } Console.Clear();

    if (yourage < 17 || yourage > 69)
    {
    Console.WriteLine("\t***********************************");
    Console.WriteLine("\t* Insurance Calculator *");
    Console.WriteLine("\t***********************************");
    Console.WriteLine("\t___________________________________");
    Console.WriteLine("\tSorry but a quote can not be displayed.");
    Console.WriteLine("\tPlease contact our main office at 1850 246 365 for further details");
    Console.WriteLine("\tApology for any inconvenience's caused.");
    Console.WriteLine("\t___________________________________");
    Console.ReadKey();

    }
    }


Comments

  • Closed Accounts Posts: 2,117 ✭✭✭Defiler Of The Coffin


    OP, that code is a nightmare to read. Have you covered functions yet? There is a lot of duplication in your code that could be re-factored into small distinct functions.

    For examples, the lines:
    Console.WriteLine("\t***********************************");
    Console.WriteLine("\t* Insurance Calculator *");
    Console.WriteLine("\t***********************************");
    

    Could be placed inside of a 'writeBanner()' function and called every time you need to print the banner.

    Also, why are the user details variables being re-declared for every single choice? Why not declare them once and use them across the entire program?


  • Closed Accounts Posts: 2,117 ✭✭✭Defiler Of The Coffin


    For your first problem OP, if you want the program to go back to the start after the premium is calculated then you will need to wrap it in a loop. A 'do-while' loop would be a good option here, as you want the program to execute at least once, and perhaps more if the user so wants. So you would code it something like:
    char choice = 'n';
    do
    {
        Console.Clear();
        <entire program>
        Console.Clear();
        Console.WriteLine("Another quote? Enter y or n: ");
        choice = Console.ReadLine().ElementAt(0);
    } while (choice == 'y' || choice == 'Y');
    <thank you for using program etc.>
    

    For your second problem, you are missing a Console.ReadKey(); from the block of code where you print the error message.


  • Banned (with Prison Access) Posts: 531 ✭✭✭fontdor


    OK so after a long struggle I got it working thank you so much for the tips! Just one last question how can I do the graphic algorithim for this?


  • Banned (with Prison Access) Posts: 531 ✭✭✭fontdor


    Thank You all for the help.


Advertisement