0

New to C#. This is the code I am working with:

using SC = System.Console;

public class Lab01
{ // Main Method 
    public static void Main()
    {   
        string hoursWorkedAsString;
        string payRateAsString;
        double hoursWorked = 24;
        double payRate = 11.5;
        double grossPay = hoursWorked * payRate;
        double stateTax = 0.04;
        double federalTax = 0.23;
        double ficaTax = 0.14;
        double totalTax = grossPay * (stateTax + federalTax + ficaTax); 
        double netPay = grossPay - totalTax;

        SC.WriteLine("Lab 4");
        SC.WriteLine();

        //get input
        SC.Write("Please input hours worked: ");
        hoursWorkedAsString = SC.ReadLine();
        hoursWorked = System.Convert.ToDouble(hoursWorkedAsString);
        SC.WriteLine();

        SC.Write("Please rate of pay: ");
        payRateAsString = SC.ReadLine();
        payRate = System.Convert.ToDouble(payRateAsString);
        SC.WriteLine();

        SC.Write("Gross Pay is: ");
        SC.WriteLine(hoursWorked * payRate);
        SC.WriteLine(); 

        SC.Write("State tax is: ");
        SC.WriteLine(stateTax * grossPay);
        SC.WriteLine(); 

        SC.Write("Federal tax is: ");
        SC.WriteLine(federalTax * grossPay);
        SC.WriteLine(); 

        SC.Write("FICA tax is: ");
        SC.WriteLine(ficaTax * grossPay);
        SC.WriteLine(); 

        SC.Write("Net Pay is: ");
        SC.WriteLine(grossPay - totalTax);
        SC.WriteLine(); 

        SC.Write("End of Lab 4");
        SC.WriteLine();
    }

}

Everything works out fine... IF I use thnumbers I declared for hoursWorked and payRate.

My question is how do I set those variables to user input? Because, if I just leave it at "double hoursWorked;" and "double payRate;", it doesn't work. I've also tried the {0} operator, and that's not accepted.

And yes, it is homework, but I've done 99% of it already.

3
Contributors
5
Replies
7
Views
4 Years
Discussion Span
Last Post by nakor77
0

You can use a WriteLine() to describe what you want the user to enter, then wait for their response using ReadLine(). You will need to make sure that the user has entered a number.

0

I'm talking about lines 9 & 10. VS doem't let me leave it blank

0

Sorry I misunderstood what you were asking. You should be able to leave them uninitialised (ie blank), but you can't use them until after they have been assigned. You will need to move the line double grossPay = hoursWorked * payRate; and each of the tax calculation lines after the user input lines.

0

Thank you. Unfortunately, I was doing it totally different than what the lab specs wanted.

0

you can always initialize them to 0 just so they have a value and the compiler doesn't throw a fit about unused variables

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.