Beginner Help: Unassigned local variable
-
Hello all, I am brand new to the C# world and am having issues with my first program. Below is the code, and I am getting an "Unassigned local variable" on the Console.Write for the finalSalary variable. Is my problem with data type, or more likely the conditional? Thanks in advance for the help, and I appologize for the simplicity of my problem!
using System; namespace EmployeeSalary { /// /// Calculates employee salary based on experience, birthday, position, and company success /// class EmployeeSalary { [STAThread] static void Main(string[] args) { int executive, //employee's executive status baseSalary = 5000; //employee's base salary finalSalary, //employee's final salary experience, //employee's years of experience birthday; //employee's birthday bonus // Employee's executive status (1 = exec, 2 = not exec) Console.Write( "Please enter executive code (1 = yes 2 = no): " ); executive = Int32.Parse( Console.ReadLine() ); if ( executive == 1 ) Console.WriteLine( "\nSorry, you're an executive, no benefits for you!" ); //Employee's years of experience (1 = less than 3 yrs, 2 = 3 to 5 yrs, 3 = over 5 yrs) Console.WriteLine( "\nPlease enter your experience code (1 = less than 3 yrs, 2 = 3-5 yrs, 2 = over 5 yrs)" ); experience = Int32.Parse( Console.ReadLine() ); if ( experience == 1 ) finalSalary = baseSalary - 2000; else if ( experience == 2 ) finalSalary = baseSalary + 2500; else if ( experience == 3 ) finalSalary = baseSalary + baseSalary; else Console.WriteLine("That is not a valid entry"); //Write final salary Console.WriteLine(finalSalary.ToString("c")); } } }
-
Hello all, I am brand new to the C# world and am having issues with my first program. Below is the code, and I am getting an "Unassigned local variable" on the Console.Write for the finalSalary variable. Is my problem with data type, or more likely the conditional? Thanks in advance for the help, and I appologize for the simplicity of my problem!
using System; namespace EmployeeSalary { /// /// Calculates employee salary based on experience, birthday, position, and company success /// class EmployeeSalary { [STAThread] static void Main(string[] args) { int executive, //employee's executive status baseSalary = 5000; //employee's base salary finalSalary, //employee's final salary experience, //employee's years of experience birthday; //employee's birthday bonus // Employee's executive status (1 = exec, 2 = not exec) Console.Write( "Please enter executive code (1 = yes 2 = no): " ); executive = Int32.Parse( Console.ReadLine() ); if ( executive == 1 ) Console.WriteLine( "\nSorry, you're an executive, no benefits for you!" ); //Employee's years of experience (1 = less than 3 yrs, 2 = 3 to 5 yrs, 3 = over 5 yrs) Console.WriteLine( "\nPlease enter your experience code (1 = less than 3 yrs, 2 = 3-5 yrs, 2 = over 5 yrs)" ); experience = Int32.Parse( Console.ReadLine() ); if ( experience == 1 ) finalSalary = baseSalary - 2000; else if ( experience == 2 ) finalSalary = baseSalary + 2500; else if ( experience == 3 ) finalSalary = baseSalary + baseSalary; else Console.WriteLine("That is not a valid entry"); //Write final salary Console.WriteLine(finalSalary.ToString("c")); } } }
The ";" at the end of code baseSalary=5000 stops the int declaration. Add "int finalSalary=0" and your code should work fine. Its a good practice to always add type "int" before variables hv fun
-
The ";" at the end of code baseSalary=5000 stops the int declaration. Add "int finalSalary=0" and your code should work fine. Its a good practice to always add type "int" before variables hv fun
I knew it would be something simple. The semicolon is actually not there in my initial code, I was rearranging things after I posted it so it would be a little more clear. I think assigning 0 to finalSalary should do the trick, thanks for the help, I'm sure I will be back for more!
-
Hello all, I am brand new to the C# world and am having issues with my first program. Below is the code, and I am getting an "Unassigned local variable" on the Console.Write for the finalSalary variable. Is my problem with data type, or more likely the conditional? Thanks in advance for the help, and I appologize for the simplicity of my problem!
using System; namespace EmployeeSalary { /// /// Calculates employee salary based on experience, birthday, position, and company success /// class EmployeeSalary { [STAThread] static void Main(string[] args) { int executive, //employee's executive status baseSalary = 5000; //employee's base salary finalSalary, //employee's final salary experience, //employee's years of experience birthday; //employee's birthday bonus // Employee's executive status (1 = exec, 2 = not exec) Console.Write( "Please enter executive code (1 = yes 2 = no): " ); executive = Int32.Parse( Console.ReadLine() ); if ( executive == 1 ) Console.WriteLine( "\nSorry, you're an executive, no benefits for you!" ); //Employee's years of experience (1 = less than 3 yrs, 2 = 3 to 5 yrs, 3 = over 5 yrs) Console.WriteLine( "\nPlease enter your experience code (1 = less than 3 yrs, 2 = 3-5 yrs, 2 = over 5 yrs)" ); experience = Int32.Parse( Console.ReadLine() ); if ( experience == 1 ) finalSalary = baseSalary - 2000; else if ( experience == 2 ) finalSalary = baseSalary + 2500; else if ( experience == 3 ) finalSalary = baseSalary + baseSalary; else Console.WriteLine("That is not a valid entry"); //Write final salary Console.WriteLine(finalSalary.ToString("c")); } } }
It is entirely possible that
finalSalary
will not be assigned a value before it is used in execution (consider ifexperience
is not 1, 2, or 3). This is what the compiler is complaining about. You could assignfinalSalary
a value right after you declare it. You could also make theelse
branch assign it. -
Hello all, I am brand new to the C# world and am having issues with my first program. Below is the code, and I am getting an "Unassigned local variable" on the Console.Write for the finalSalary variable. Is my problem with data type, or more likely the conditional? Thanks in advance for the help, and I appologize for the simplicity of my problem!
using System; namespace EmployeeSalary { /// /// Calculates employee salary based on experience, birthday, position, and company success /// class EmployeeSalary { [STAThread] static void Main(string[] args) { int executive, //employee's executive status baseSalary = 5000; //employee's base salary finalSalary, //employee's final salary experience, //employee's years of experience birthday; //employee's birthday bonus // Employee's executive status (1 = exec, 2 = not exec) Console.Write( "Please enter executive code (1 = yes 2 = no): " ); executive = Int32.Parse( Console.ReadLine() ); if ( executive == 1 ) Console.WriteLine( "\nSorry, you're an executive, no benefits for you!" ); //Employee's years of experience (1 = less than 3 yrs, 2 = 3 to 5 yrs, 3 = over 5 yrs) Console.WriteLine( "\nPlease enter your experience code (1 = less than 3 yrs, 2 = 3-5 yrs, 2 = over 5 yrs)" ); experience = Int32.Parse( Console.ReadLine() ); if ( experience == 1 ) finalSalary = baseSalary - 2000; else if ( experience == 2 ) finalSalary = baseSalary + 2500; else if ( experience == 3 ) finalSalary = baseSalary + baseSalary; else Console.WriteLine("That is not a valid entry"); //Write final salary Console.WriteLine(finalSalary.ToString("c")); } } }
Your problem is that it is possible in your if statement to never assign a value to finalSalary since you declared finalSalary at the beginning and did not initialize it. So if someone enters a "4" at your prompt the code will execute the final "else" statement and write out to the console "That is not a valid entry" but will continue on to the final Console.WriteLine which will try to execute with an uninitialized variable. Visual studio is smart enough to detect when people try to initialize variables inside of "if" statements but not smart enough to know if all paths assign a valid result to the variable so it always throws a flag when you try to do this. The answer is to do one of the following: 1) initialize the variable at instantiation 2) move the Console.WriteLine statement to inside each "if" clause that you want it to print the final salary (probably your best bet since you don't want it to write out if the users response is invalid) Hope this helps
-
The ";" at the end of code baseSalary=5000 stops the int declaration. Add "int finalSalary=0" and your code should work fine. Its a good practice to always add type "int" before variables hv fun
> Its a good practice to always add type "int" before variables. :wtf: But then it is so hard to do things like floating point math, string manipulation and all kinds of object oriented stuff. ;P Matt Gerrans