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.

Recommended Answers

All 5 Replies

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.

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

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.

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.