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

VulpesRank #55 · 29.2K reputationPosted Aug 6, 2012, 7:14 AM EDT
VulpesRank #55 · 29.2K reputationPosted Aug 6, 2012, 12:44 AM EDT
Posted Aug 6, 2012, 3:09 AM EDT
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
*/
VulpesRank #55 · 29.2K reputationPosted Aug 6, 2012, 5:30 AM EDT
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, 6:32 AM EDT
/*
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
*/
Posted Aug 6, 2012, 7:24 AM EDT
(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;
}
VulpesRank #55 · 29.2K reputationPosted Aug 6, 2012, 9:26 AM EDT
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:41 AM EDT
if (!HourlyPayRate(out payRate)) // non-numeric input entered
{
Console.WriteLine("Program Ends");
Console.ReadKey();
return;
}
VulpesRank #55 · 29.2K reputationPosted Aug 7, 2012, 1:25 AM EDT
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 7, 2012, 2:23 AM EDT