Wednesday, July 11, 2012

C# Hourly Payrate Program

The if Selection Structure


Here is a useful program I wrote in C#, remember to save the file extension as .CS, and download the free trial version of Microsoft C# 2010, although all you really need is notepad. Just remember to save the file as .CS not as .TXT.


1) Compile (write this down in notepad)


2) Example, if problem: Error code (3, 27), in this example, the number "3" indicates error is line 3, this will save you lots of time trying to find out where the error is, since Microsoft FAQ descriptions are useless.


3) Run


After you run, this useful program will automatically calculate, for user on ReadLine(), how to estimate their weekly salary forever in DOS (C prompt). Here you go.


---------------------------------------------------------------



using System;


class SelectionExample
{
public static void Main( )
{
string strHours, strRate, strAnswer;
double hours, rate, pay;
char answer;


Console.Out.Write("How many hours did you work last week? ");
strHours = Console.ReadLine();
hours = Convert.ToDouble(strHours);


Console.Out.WriteLine();
Console.Out.Write("What is your hourly rate? ");
strRate = Console.ReadLine();
rate = Convert.ToDouble(strRate);


if(hours<=40.0)
    pay = hours * rate;
else
{
pay = hours * rate;


//Calculate overtime pay as time
//and a half for all
//hours over 40
pay = pay + ((hours -40) * (rate/2.0));
}


Console.Out.WriteLine();
Console.Out.WriteLine("Your weekly pay is: " + pay);


Console.Out.WriteLine();
Console.Out.WriteLine("Your weekly pay is: " + pay);


Console.Out.WriteLine();
Console.Out.WriteLine("Are you happy with your pay?  ");
strAnswer = Console.ReadLine();
answer = Convert.ToChar(strAnswer);


if(answer=='y'||answer=='Y')
    Console.Out.WriteLine("I'm glad that you are happy!");
else
Console.Out.WriteLine("Maybe next week you can work more hours");
}
}



You have to type "Y" or "N" not characters more than one, like "Yes" (3 characters). If you type 2 or more characters DOS says "Unhandled Exception: System.FormatException: String must be exactly one character r long."

No comments:

Post a Comment