0

I need some help making sure that when the user enters nothing, or not a number, it doesn't crash. I have it down so that it will tell them to enter a number if they don't, but I don't know how to make it go back to the original question and give them a chance to input correctly. Thanks for the help.

        Console.WriteLine("How much is rent: ");
        string  strRent = Console.ReadLine();
        double dblRent = 0.0;
        if (double.TryParse(strRent, out dblRent))
        {
            Console.WriteLine("How much is the car payment: ");
            string strCarPayment = Console.ReadLine();
            double dblCarPayment = Convert.ToDouble(strCarPayment);
        }
        else
        {
            Console.WriteLine("Enter a number");
        }

        Console.WriteLine("How much is student loan payment: ");
        string strStudentLoan = Console.ReadLine();

        Console.WriteLine("How much is phone bill: ");
        string strPhoneBill = Console.ReadLine();

        Console.WriteLine("How much is electric bill: ");
        string strElectricBill = Console.ReadLine();

        Console.WriteLine("Fraction deposited: ");
        string strFractionDeposited = Console.ReadLine();

        Console.WriteLine("Amount leftover: ");
        string strAmountLeft = Console.ReadLine();



        double dblStudentLoan = Convert.ToDouble(strStudentLoan);
        double dblPhoneBill = Convert.ToDouble(strPhoneBill);
        double dblElectricBill = Convert.ToDouble(strElectricBill);
        double dblFractionDeposited = Convert.ToDouble(strFractionDeposited);
        double dblAmountLeft = Convert.ToDouble(strAmountLeft);

        double dblBillSum = dblRent + dblCarPayment + dblStudentLoan + dblPhoneBill + dblElectricBill;
        double afterBills = dblAmountLeft / (1 - dblFractionDeposited);
        double totalPaycheck = afterBills + dblBillSum;
        Console.WriteLine("Total Paycheck: " + totalPaycheck.ToString("C"));

        Console.WriteLine("Enter wage $/hr: ");
        string strWage = Console.ReadLine();

        Console.WriteLine("Enter hours worked overtime: ");
        string strHoursOT = Console.ReadLine();

        Console.WriteLine("Enter overtime multiplier: ");
        string strOTWage = Console.ReadLine();

        double dblWage = Convert.ToDouble(strWage);
        double dblHoursOT = Convert.ToDouble(strHoursOT);
        double dblOTWage = Convert.ToDouble(strOTWage);

        double OTPay = (dblWage * dblOTWage) * dblHoursOT;
        Console.WriteLine("Overtime Pay: " + OTPay.ToString("C"));
        Console.ReadLine();

    }
}

}

0

3 Answers 3

2

You need a loop, something like this

        Console.WriteLine("How much is rent: ");
        string strRent = Console.ReadLine();
        double dblRent = 0.0;
        while (!double.TryParse(strRent, out dblRent))
        {
            Console.WriteLine("Enter a number");
            strRent = Console.ReadLine();
        }
        Console.WriteLine("How much is the car payment: ");
        string strCarPayment = Console.ReadLine();
        double dblCarPayment = Convert.ToDouble(strCarPayment);
Sign up to request clarification or add additional context in comments.

2 Comments

Since there are so many questions that follow a similar format, I'd recommend putting this solution into another method and pass the "question string" to the method, and return the entered value.
I used this and it worked beautifully. Thank you so much!
0

Try this

        double dblRent = 0.0;
        Boolean valid = false;
        while (!valid)
        {
            if (double.TryParse(strRent, out dblRent))
            {
                Console.WriteLine("How much is the car payment: ");
                string strCarPayment = Console.ReadLine();
                double dblCarPayment = Convert.ToDouble(strCarPayment);
                valid = true;
            }
            else
            {
                Console.WriteLine("Enter a number");
            }
        }​

Comments

0

Since you are reading many doubles, you could create a method encapsulating reading and processing the number

private static double? ReadLineDouble()
{
    while(true) {
        string s = Console.ReadLine();
        if (String.IsNullOrWhitespace(s)) {
            return null; // The user wants to abort
        }
        double d;
        if (Double.TryParse(s, out d)) {
            return d;
        }
        Console.WriteLine("Please enter a valid number");
    }
}

If the user just hits Enter probably he wants to abort. In this case the method returns null. This is possible, because the return type is a Nullable<double>. The shorthand notation for it is double?.

Now you can read a number like this:

double? rent = ReadLineDouble();
if (double == null) return; // Abort the program.
// Otherwise continue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.