string FirstName, LastName;
char MiddleInitial;
Console.Write("Enter first name: ");
FirstName = Console.ReadLine();
if (FirstName.Equals("null"))
{
Console.Write("Insert middle initial ");
Console.Write("\nInsert last name \n");
}
else
{
Console.Write("Insert middle initial: ");
MiddleInitial = Console.ReadLine()[0];
Console.Write("Insert last name: ");
LastName = Console.ReadLine();
}
int Months = 1 ;
if (Months == 2)
{
Console.WriteLine("Incorret");
}
else
{
Console.WriteLine("\nYour record");
Console.WriteLine("FIRST NAME:{0}", FirstName);
Console.WriteLine("MIDDLE INITIAL: {0}", MiddleInitial);
Console.WriteLine("LASTNAME: {0}", LastName);
}
I can call the FirstName, but the MiddleInitial and LastName is not working if you run it in Visual studio. what is the possible error?
The program output should display like this:
Enter first name: Joshua
Insert middle initial:F
Insert last name:Capili
Your record
FIRST NAME:Joshua
MIDDLE INITIAL: f
LASTNAME:Capili
this Console.WriteLine("MIDDLE INITIAL: {0}", MiddleInitial); Console.WriteLine("LASTNAME: {0}", LastName); it says that "use of unassigned local variables 'MiddleInitial' "use of unassigned local variables 'LastName' that's why i can't run the program.
CodePudding user response:
Understanding error messages is a critical component of programming. Always make sure to review and comprehend the official documentation of error messages whenever possible:
Use of unassigned local variable 'name'
The C# compiler doesn't allow the use of uninitialized variables. If the compiler detects the use of a variable that might not have been initialized, it generates compiler error CS0165. For more information, see Fields. This error is generated when the compiler encounters a construct that might result in the use of an unassigned variable, even if your particular code does not. This avoids the necessity of overly complex rules for definite assignment.
In your case, you are using a variable (or rather two, MiddleInitial
and LastName
) without potentially being assigned, due to your conditional statements. The compiler will not allow this.
You can solve this by simply initializing them to white space.
string FirstName, LastName = "";
char MiddleInitial = ' ';
CodePudding user response:
Hi what Im seeing is that the problem with your code is in the logic you are writing. The fact is that you ask for FirstName but as long at its null your program won't run inside the else statement
Console.Write("Enter first name: ");
FirstName = Console.ReadLine();
while (FirstName.Equals("null")){
Console.Write("Enter first name: ");
FirstName = Console.ReadLine();
}
Console.Write("Insert middle initial: ");
MiddleInitial = Console.ReadLine()[0];
while (MiddleInitial.Equals("null")){
Console.Write("Insert middle initial: ");
MiddleInitial = Console.ReadLine()[0];
}
Console.Write("Insert last name: ");
LastName = Console.ReadLine();
while (LastName.Equals("null")){
Console.Write("Insert last name: ");
LastName = Console.ReadLine();
}
int Months = 1 ;
if (Months == 2)
{
Console.WriteLine("Incorret");
}
else
{
Console.WriteLine("\nYour record");
Console.WriteLine("FIRST NAME:{0}", FirstName);
Console.WriteLine("MIDDLE INITIAL: {0}", MiddleInitial);
Console.WriteLine("LASTNAME: {0}", LastName);
}