RyC
RyC

Reputation: 15

How to repeat code if invalid data is entered

I need to make this code which calculates a Parking Fee give an error and ask the user to re-input the data if a false number is provided. For example if the user enters an amount that is less than 1 or greater than 24, an error code will appear and ask the user to re-enter a valid amount. Once a valid amount is entered I'd like it to output the parkFee. I haven't updated my Pseudocode so apologies about that.

    /* PSEUDOCODE */
    /* HOURLY_RATE=2.5
     * INPUT parkTime
     * parkFee = HOURLY_RATE * hours
     * OUTPUT parkFee */


        decimal parkTime;  // input - time in hour eg 1.5 for 1 and a half hours 
        const decimal HOURLY_RATE = 2.50m; // HOURLY_RATE * INPUT parkTime = parkFee
        const decimal MAX_FEE = 20.00m; // MAX_FEE is set as a payment cap and ignores any extra charges incurred over 8 hours
        decimal parkFee;
        Console.WriteLine("ParkingFee1 program developed by: Ryley Copeman");
        Console.WriteLine("Please enter your total time parked in hours: Eg 1.5 or 3.0");
        parkTime = decimal.Parse(Console.ReadLine());

        if (parkTime > 8)
        {
            Console.Write("Total fee is $" + MAX_FEE);
        }
        else
        {
            parkFee = Math.Ceiling(parkTime) * HOURLY_RATE;
            Console.Write("Parking Fee = $" + parkFee);
        }
        while(parkTime < 0 || parkTime > 24)    // validate... 
        //while (parkTime <= 0) )
        {
        Console.WriteLine("Error – Park Time out of range");
        Console.WriteLine("Enter - Park Time between 0 and 24 (HOURS):");
        parkTime = int.Parse(Console.ReadLine());

    }

    }
}

}

Upvotes: 1

Views: 124

Answers (4)

Mojtaba Tajik
Mojtaba Tajik

Reputation: 1733

 /* PSEUDOCODE */
    /* HOURLY_RATE=2.5
     * INPUT parkTime
     * parkFee = HOURLY_RATE * hours
     * OUTPUT parkFee */

        bool mustRepeat = true;
        decimal parkTime = 0;  // input - time in hour eg 1.5 for 1 and a half hours 
        const decimal HOURLY_RATE = 2.50m; // HOURLY_RATE * INPUT parkTime = parkFee
        const decimal MAX_FEE = 20.00m; // MAX_FEE is set as a payment cap and ignores any extra charges incurred over 8 hours
        decimal parkFee;
        Console.WriteLine("ParkingFee1 program developed by: Ryley Copeman");
        Console.WriteLine("Please enter your total time parked in hours: Eg 1.5 or 3.0");

        while(mustRepeat)    // validate... 
        {
            parkTime = decimal.Parse(Console.ReadLine());

            if(parkTime < 1 || parkTime > 24)
            {       
                Console.WriteLine("Error – Park Time out of range");
                Console.WriteLine("Enter - Park Time between 0 and 24 (HOURS):");
                continue;
            }

            mustRepeat = false;

            if (parkTime > 8)
            {
                Console.Write("Total fee is $" + MAX_FEE);
                break;
            }
            else
            {
                parkFee = Math.Ceiling(parkTime) * HOURLY_RATE;
                Console.Write("Parking Fee = $" + parkFee);
                break;
            }
        }
    }
}

Upvotes: 1

ProgrammingLlama
ProgrammingLlama

Reputation: 38785

I think you just need this:

do
{
    Console.WriteLine("Please enter your total time parked in hours: Eg 1.5 or 3.0");
    parkTime = decimal.Parse(Console.ReadLine());

    if (parkTime < 1 || parkTime > 24)
    {
        Console.WriteLine("Error – Park Time out of range");
    }
}
while (parkTime < 1 || parkTime > 24);

if (parkTime > 8)
{
    Console.Write("Total fee is $" + MAX_FEE);
}
else
{
     parkFee = Math.Ceiling(parkTime) * HOURLY_RATE;
     Console.Write("Parking Fee = $" + parkFee);
 }

Note that you may want to adjust the code to always calculate the fee, and then apply the maximum:

parkFee = Math.Min(MAX_FEE, Math.Ceiling(parkTime) * HOURLY_RATE);
Console.Write("Parking Fee = $" + parkFee);

Here Math.Min will choose the smallest of the two values.

Finally, note that decimal.Parse will error if you enter something it doesn't expect (e.g. "1.2Hello", or ""), so it might be better to use TryParse:

bool isValidTime = false;
do
{
    Console.WriteLine("Please enter your total time parked in hours: Eg 1.5 or 3.0");
    bool parsedOK = decimal.TryParse(Console.ReadLine(), out parkTime);
    isValidTime = parsedOK && parkTime >= 1 && parkTime <= 24;
    if (!isValidTime)
    {
        Console.WriteLine("Error – Park Time out of range");
    }
}
while (!isValidTime);

parkFee = Math.Min(MAX_FEE, Math.Ceiling(parkTime) * HOURLY_RATE);
Console.Write("Parking Fee = $" + parkFee);

Here the loops will continue until a valid value is entered. Note that in loop structures you can also use break; (leave the loop), and continue; (move to the next iteration of the loop) to control the flow.

Upvotes: 6

aozk
aozk

Reputation: 418

Console.WriteLine("ParkingFee1 program developed by: Ryley Copeman");
Console.WriteLine("Please enter your total time parked in hours: Eg 1.5 or 3.0");
parkTime = decimal.Parse(Console.ReadLine());
do
{
    if(parkTime < 1 || parkTime > 24)
    {
        Console.WriteLine("Error – Park Time out of range");
        Console.WriteLine("Enter - Park Time between 0 and 24 (HOURS):");
        parkTime = decimal.Parse(Console.ReadLine());
        continue;
    }
    if (parkTime > 8)
    {
        Console.Write("Total fee is $" + MAX_FEE);
    }
    else
    {
        parkFee = Math.Ceiling(parkTime) * HOURLY_RATE;
        Console.Write("Parking Fee = $" + parkFee);
    }
} while(parkTime < 1 || parkTime > 24);

Upvotes: 1

RalfFriedl
RalfFriedl

Reputation: 1130

As you already know about the concept of loops, there are a few possibilities, the simple one is a loop with break.

for (;;) {
  // input
  if (condition ok)
    break;
  // output "wrong, try again"
}

This will repeat the input as often as necessary and quit the loop once accaptable values are entered.

Upvotes: 1

Related Questions