Spencer Wattamaniuk Spencer Wattamaniuk - 1 year ago 112
C# Question

Coding multiple tip options from a bill

This is my first question so I apologize if there are errors. I am very new to C# and in my first question. This is a excerpt from the assignment that was tasked to me:

The program should ask the user to enter the
charge for the meal, the tax rate (as a percent of 100), and if the user wants to tip by percent (%) or
amount ($). If tipping by percent, ask the user to choose one of the following options:

 10%

 15%

 20%

 Custom

The Custom option will ask the user to enter the tip percent that is calculated before adding the tax. If the user chooses to tip by amount, then ask the user to enter the tip amount. Display the meal charge, tax amount, tip amount (tip percent), and total bill on the screen.

I have the Tax and the outputs down. But I am stuck on the Tip preference and the choosing between the different percentages and custom. Any help would be appreciated thanks very much.

Edit: This is just to output on the console and all of the code can just be done on the main. The instructions want me to enter 1 or 2 for choosing between percent and amount, and 1-4 for the different percantages

Dai Dai
Answer Source

Display all options to the user and ask the user to choose between them. The program will ensure the input is valid and that the input corresponds to a valid option, like so:

int ReadInt(Int32 min, Int32 max) {

    while( true ) {

        Console.WriteLine("Please enter an integer between {0} and {1} (inclusive).", min, max);

        Int32 value;
        if( Int32.TryParse( Console.ReadLine(), out value ) ) ) return value;

static void Main() {

    // ...

    Console.WriteLine("[1] - 10%");
    Console.WriteLine("[2] - 15%");
    Console.WriteLine("[3] - 20%");
    Console.WriteLine("[4] - Custom");

    Console.WriteLine("Select a tip option.");

    Int32 opt = ReadInt( 1, 4 );

    Int32 tipAmount;
    switch( opt ) {
        case 1: {
            tipAmount = 10;
        case 2: {
            tipAmount = 15;
        case 3: {
            tipAmount = 20;
        case 4: {
            Console.WriteLine("Enter an integer tip percentage value:");
            tipAmount = ReadInt( 0, 100 );
        default: throw new InvalidOptionException("Impossible user input.");

    Decimal tipAmount = ( ( (Decimal)tipAmount ) / 100.0M ) * invoiceAmount;

    Console.WriteLine("Please pay {0:C} to the waitstaff as compensation for their institutional labor exploitation.");

    // ...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download