getting unassigned variable error on a variable that is assigned

userInput is assigned so im not sure why I get error. I've already sifted through the similar questions, googled, binged and didnt find what I was looking for. ps new to programming

static void Main(string[] args)
{
    //declare variables
    int hours, minutes, seconds, remainder, userInput;
    const int SECONDS_PER_MINUTE = 60;
    const int SECONDS_PER_HOUR = 3600;

    userInput = getUserInput(userInput); //error

    hours = CalculateHours(userInput, SECONDS_PER_HOUR);
    remainder = CalculateRemainder(userInput, SECONDS_PER_HOUR);
    minutes = CalculateMinutes(remainder, SECONDS_PER_MINUTE);
    seconds = CalculateSeconds(remainder, SECONDS_PER_MINUTE);


    DisplayResults(hours, minutes, remainder, userInput, seconds);

}


//prompt the user to enter number of seconds
public static int getUserInput(int userInput)     
{
    Console.WriteLine("How about you enter a number of " +
                      " seconds and Ill see what I can do for you. ");
   return userInput = int.Parse(Console.ReadLine());
}

//this method takes in userinput and converts to hours if necessary
//userInput / SECONDS_PER_HOUR will be returned to CalculateHours
//in main and stored in the hours variable. same goes for the rest
//except DisplayResults
public static int CalculateHours(int userInput, int SECONDS_PER_HOUR)
{
    return userInput / SECONDS_PER_HOUR;
}
//this method takes userinput and gives a remainder to be used for minutes and seconds
public static int CalculateRemainder(int userInput, int SECONDS_PER_HOUR)
{
    return userInput % SECONDS_PER_HOUR;
}

public static int CalculateMinutes(int remainder, int SECONDS_PER_MINUTE)
{
    return remainder / SECONDS_PER_MINUTE;
}

public static int CalculateSeconds(int remainder, int SECONDS_PER_MINUTE)
{
    return remainder % SECONDS_PER_MINUTE;
}
Jon Skeet
people
quotationmark

Here's the problematic code:

userInput = getUserInput(userInput);

userInput is assigned after that statement, but think of it this way:

int tmp = getUserInput(userInput);
userInput = tmp;

Nothing has assigned a value to userInput before the method call. It's not clear why you're specifying a parameter at all, to be honest - you're not really using it. The code would be cleaner as:

int userInput = GetUserInput();

...

public static int GetUserInput()
{
    Console.WriteLine("...");
    return int.Parse(Console.ReadLine());
}

Note how I'm declaring the variable at the point of first use - that's a generally good idea. Additionally, it would be cleaner if you'd declare constants outside your method... and use those within your Calculate* methods (why would you want to parameterize the method by a constant?). So your Main method would be clearer as:

const int SecondsPerMinute = 60;
const int SecondsPerHour = SecondsPerMinute * 60;

static void Main(string[] args)
{
    int userInput = GetUserInput();
    int hours = CalculateHours(userInput);
    int remainder = CalculateRemainder(userInput);
    int minutes = CalculateMinutes(remainder);
    int seconds = CalculateSeconds(remainder);
    DisplayResult(hours, minutes, remainder, userInput, seconds);
}

(I'd suggest changing the order of the DisplayResults parameters to be more consistent too, along with a few other things, but that's probably enough for now.)

people

See more on this question at Stackoverflow