This is Question 3 of the Exercise (pdf page 6). How could this program be modified? So that if user entered non numeric value program must end.
using System;
namespace CheckLowRate
{
class EnsureValidPayRate
{
static void Main(string[] args)
{
double payRate;
payRate = HourlyPayRate();
while (true)
{
if (49.99 > payRate && payRate > 5.65)
{
Console.WriteLine("{0} Acceptable", payRate.ToString("C"));
}
if (49.99 < payRate || payRate < 5.65)
{
Console.WriteLine("{0} Not Acceptable", payRate.ToString("C"));
}
payRate = HourlyPayRate();
}
Console.WriteLine("Program Ends");
Console.ReadKey();
}
public static double HourlyPayRate()
{
Console.Write("\nHourly Pay Rate is $");
double HpayRate = Convert.ToDouble(Console.ReadLine());
return HpayRate;
}
}
}
Loading
VulpesPosted Aug 6, 2012, 11:14 AM
Posted Aug 7, 2012, 6:23 AM
VulpesPosted Aug 7, 2012, 5:25 AM
If you didn't include it, then the while statement would simply continue.
Instead of 'return', you could also use 'break' which would exit the while statement and allow the Main method (and hence the application itself) to terminate 'normally'.
Posted Aug 6, 2012, 3:41 PM
if (!HourlyPayRate(out payRate)) // non-numeric input entered
{
Console.WriteLine("Program Ends");
Console.ReadKey();
return;
}
VulpesPosted Aug 6, 2012, 1:26 PM
If the user enters a valid double, the method returns true and the actual double value in the out parameter.
If the user fails to enter a valid double, the method returns false and a value of 0 in the out parameter.
This behaviour is simply reflecting the behaviour of the Double.TryParse method:
http://msdn.microsoft.com/en-us/library/system.double.tryparse.aspx
So if the method returns false, the return statement is called to exit the Main method and hence the program itself - otherwise the while statement would continue.
Posted Aug 6, 2012, 11:24 AM
(1) I wish to learn about following code, please give me addresses of the relevant website
if(!HourlyPayRate(out payRate)) // non-numeric input entered
(2) What is the reason to have return within the if block.
if (!HourlyPayRate(out payRate)) // non-numeric input entered
{
Console.WriteLine("Program Ends");
Console.ReadKey();
return;
}
Posted Aug 6, 2012, 10:32 AM
/*
Hourly Pay Rate is $80
$80.00 Not Acceptable
Hourly Pay Rate is $5
$5.00 Not Acceptable
Hourly Pay Rate is $44
$44.00 Acceptable
Program Ends
*/
VulpesPosted Aug 6, 2012, 9:30 AM
using System;
namespace CheckLowRate
{
class EnsureValidPayRate
{
static void Main(string[] args)
{
double payRate;
while(true)
{
if(!HourlyPayRate(out payRate)) break; // non-numeric unput entered
if (49.99 >= payRate && payRate >= 5.65)
{
Console.WriteLine("{0} Acceptable", payRate.ToString("C"));
break;
}
Console.WriteLine("{0} Not Acceptable", payRate.ToString("C"));
}
Console.WriteLine("Program Ends");
Console.ReadKey();
}
public static bool HourlyPayRate(out double HPayRate)
{
Console.Write("\nHourly Pay Rate is $");
bool isValid = Double.TryParse(Console.ReadLine(), out HPayRate);
return isValid;
}
}
}
Posted Aug 6, 2012, 7:09 AM
Whether it is possible to modify, so that output will be as follows:
/*
Hourly Pay Rate is $5
$5.00 Not Acceptable
Hourly Pay Rate is $12
$12.00 Acceptable
Hourly Pay Rate is $4
$4.00 Not Acceptable
Hourly Pay Rate is $11
$11.00 Acceptable
Hourly Pay Rate is $afdasf
Program Ends
*/
VulpesPosted Aug 6, 2012, 4:44 AM