Finding an Employee without using so many Conditional Statements using Structs - c#

I have written this code for my school project program (it's still incomplete) but it does the job for the most part. The thing is, we have to use structs to make a program of pre-defined employees and then we have to make it so that we can use any attribute (Name, Employee ID, Blood group, Age etc.) to search through all of the employees.
I have found a way using conditional statements (which as of right now, works for name and employee ID). I have realized that the code is too long and there might be another way to search through the program. I also want to make it show all male/female employees when we use Gender as the searching attribute.
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Terminals
{
class Program
{
struct Employee
{
public string Name;
public int EmployeeID;
public string BloodGroup;
public int Salary;
public int Age;
public string Gender;
}
static void Main(string[] args)
{
Employee employee1;
Employee employee2;
Employee employee3;
Employee employee4;
Employee employee5;
Employee employee6;
employee1.Name = "James";
employee1.EmployeeID = int.Parse("10");
employee1.BloodGroup = "B +ive";
employee1.Salary = int.Parse("500000");
employee1.Age = int.Parse("26");
employee1.Gender = "Male";
employee2.Name = "Ali Khan";
employee2.EmployeeID = int.Parse("20");
employee2.BloodGroup = "O +ive";
employee2.Salary = int.Parse("250000");
employee2.Age = int.Parse("22");
employee2.Gender = "Male";
employee3.Name = "Jessica Hills";
employee3.EmployeeID = int.Parse("30");
employee3.BloodGroup = "A -ive";
employee3.Salary = int.Parse("400000");
employee3.Age = int.Parse("25");
employee3.Gender = "Female";
employee4.Name = "William";
employee4.EmployeeID = int.Parse("40");
employee4.BloodGroup = "O -ive";
employee4.Salary = int.Parse("700000");
employee4.Age = int.Parse("29");
employee4.Gender = "Male";
employee5.Name = "Lizzy";
employee5.EmployeeID = int.Parse("50");
employee5.BloodGroup = "AB +ive";
employee5.Salary = int.Parse("70000");
employee5.Age = int.Parse("19");
employee5.Gender = "Female";
employee6.Name = "Kyle";
employee6.EmployeeID = int.Parse("60");
employee6.BloodGroup = "AB -ive";
employee6.Salary = int.Parse("600000");
employee6.Age = int.Parse("21");
employee6.Gender = "Male";
Console.WriteLine("Please enter the number of the term you would like to search with (1-6)?");
Console.WriteLine("1. Name");
Console.WriteLine("2. Employee ID");
Console.WriteLine("3. Blood Group");
Console.WriteLine("4. Salary");
Console.WriteLine("5. Age");
Console.WriteLine("6. Gender");
Console.WriteLine("Please enter the number (1-6): ");
int searchterm = int.Parse(Console.ReadLine());
if (searchterm == 1)
{
Console.WriteLine("Please enter the name of the employee: ");
string name = Console.ReadLine();
if (name == employee1.Name)
{
Console.Write("Name: " + employee1.Name);
Console.WriteLine("Employee ID: " + employee1.EmployeeID);
Console.WriteLine("Blood Group: " + employee1.BloodGroup);
Console.WriteLine("Salary: " + employee1.Salary);
Console.WriteLine("Age: " + employee1.Age);
Console.WriteLine("Gender: " + employee1.Gender);
Console.ReadKey();
}
else if (name == employee2.Name)
{
Console.Write("Name: " + employee2.Name);
Console.WriteLine("Employee ID: " + employee2.EmployeeID);
Console.WriteLine("Blood Group: " + employee2.BloodGroup);
Console.WriteLine("Salary: " + employee2.Salary);
Console.WriteLine("Age: " + employee2.Age);
Console.WriteLine("Gender: " + employee2.Gender);
Console.ReadKey();
}
else if (name == employee3.Name)
{
Console.Write("Name: " + employee3.Name);
Console.WriteLine("Employee ID: " + employee3.EmployeeID);
Console.WriteLine("Blood Group: " + employee3.BloodGroup);
Console.WriteLine("Salary: " + employee3.Salary);
Console.WriteLine("Age: " + employee3.Age);
Console.WriteLine("Gender: " + employee3.Gender);
Console.ReadKey();
}
else if (name == employee4.Name)
{
Console.Write("Name: " + employee4.Name);
Console.WriteLine("Employee ID: " + employee4.EmployeeID);
Console.WriteLine("Blood Group: " + employee4.BloodGroup);
Console.WriteLine("Salary: " + employee4.Salary);
Console.WriteLine("Age: " + employee4.Age);
Console.WriteLine("Gender: " + employee4.Gender);
Console.ReadKey();
}
else if (name == employee5.Name)
{
Console.Write("Name: " + employee5.Name);
Console.WriteLine("Employee ID: " + employee5.EmployeeID);
Console.WriteLine("Blood Group: " + employee5.BloodGroup);
Console.WriteLine("Salary: " + employee5.Salary);
Console.WriteLine("Age: " + employee5.Age);
Console.WriteLine("Gender: " + employee5.Gender);
Console.ReadKey();
}
else if (name == employee6.Name)
{
Console.Write("Name: " + employee1.Name);
Console.WriteLine("Employee ID: " + employee6.EmployeeID);
Console.WriteLine("Blood Group: " + employee6.BloodGroup);
Console.WriteLine("Salary: " + employee6.Salary);
Console.WriteLine("Age: " + employee6.Age);
Console.WriteLine("Gender: " + employee6.Gender);
Console.ReadKey();
}
else
{
Console.WriteLine("Please try again.");
return;
}
}
if (searchterm == 2)
{
Console.WriteLine("Please enter the employee ID of the employee");
int employeeid = int.Parse(Console.ReadLine());
if (employeeid == employee1.EmployeeID)
{
Console.Write("Name: " + employee1.Name);
Console.WriteLine("Employee ID: " + employee1.EmployeeID);
Console.WriteLine("Blood Group: " + employee1.BloodGroup);
Console.WriteLine("Salary: " + employee1.Salary);
Console.WriteLine("Age: " + employee1.Age);
Console.WriteLine("Gender: " + employee1.Gender);
Console.ReadKey();
}
else if (employeeid == employee2.EmployeeID)
{
Console.Write("Name: " + employee2.Name);
Console.WriteLine("Employee ID: " + employee2.EmployeeID);
Console.WriteLine("Blood Group: " + employee2.BloodGroup);
Console.WriteLine("Salary: " + employee2.Salary);
Console.WriteLine("Age: " + employee2.Age);
Console.WriteLine("Gender: " + employee2.Gender);
Console.ReadKey();
}
else if (employeeid == employee3.EmployeeID)
{
Console.Write("Name: " + employee3.Name);
Console.WriteLine("Employee ID: " + employee3.EmployeeID);
Console.WriteLine("Blood Group: " + employee3.BloodGroup);
Console.WriteLine("Salary: " + employee3.Salary);
Console.WriteLine("Age: " + employee3.Age);
Console.WriteLine("Gender: " + employee3.Gender);
Console.ReadKey();
}
else if (employeeid == employee4.EmployeeID)
{
Console.Write("Name: " + employee4.Name);
Console.WriteLine("Employee ID: " + employee4.EmployeeID);
Console.WriteLine("Blood Group: " + employee4.BloodGroup);
Console.WriteLine("Salary: " + employee4.Salary);
Console.WriteLine("Age: " + employee4.Age);
Console.WriteLine("Gender: " + employee4.Gender);
Console.ReadKey();
}
else if (employeeid == employee5.EmployeeID)
{
Console.Write("Name: " + employee5.Name);
Console.WriteLine("Employee ID: " + employee5.EmployeeID);
Console.WriteLine("Blood Group: " + employee5.BloodGroup);
Console.WriteLine("Salary: " + employee5.Salary);
Console.WriteLine("Age: " + employee5.Age);
Console.WriteLine("Gender: " + employee5.Gender);
Console.ReadKey();
}
else if (employeeid == employee6.EmployeeID)
{
Console.Write("Name: " + employee6.Name +" ");
Console.WriteLine("Employee ID: " + employee6.EmployeeID);
Console.WriteLine("Blood Group: " + employee6.BloodGroup);
Console.WriteLine("Salary: " + employee6.Salary);
Console.WriteLine("Age: " + employee6.Age);
Console.WriteLine("Gender: " + employee6.Gender);
Console.ReadKey();
}
else
{
Console.WriteLine("Please try again.");
Console.ReadKey();
return;
}
}
}
}
}

As others said, LINQ will be your friend here, and using a collection, like a List<Employee> also helps tremendously. You can eliminate the else if logic and shorten the code up by a good amount.
For the Employee emp = employeeList.Find(employee => employee.Name == name); line, you would need to change the predicate to Find by the correct struct member depending on the user input. If it can't find the Employee out of the list, the else logic will report so (this too would also need to be specific to each search condition). I also added a private method to seed the list of employees. A simple method to contain all the console output lines will also shorten this up even more - simply supply the found Employee to it.
The only issue with this is if two or more employees have the same value for one of their members. It looks like none of your seeded employees share anything in common except Gender, in which case you would need to filter on an additional attribute to narrow it down to one employee, or output all the found employees by the search criteria. .Find will return the first Employee that matches the criteria, so that won't work (or at least, it would only output the first found Employee!) You could change this code to use .FindAll instead, and then pass a new List<Employee> to the private methods, and loop over all the employees in the list, outputting each of their information. Overloading the ValidateEmployee method I wrote to take in a List<Employee> should do the trick. You could make all your conditions perform a .FindAll and delete the ValidateEmployee method that takes in a single Employee, up to you!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Terminals
{
class Program
{
struct Employee
{
public string Name;
public int EmployeeID;
public string BloodGroup;
public int Salary;
public int Age;
public string Gender;
}
static void Main(string[] args)
{
List<Employee> employeeList = SeedEmployees();
Console.WriteLine("Please enter the number of the term you would like to search with (1-6):");
Console.WriteLine("1. Name");
Console.WriteLine("2. Employee ID");
Console.WriteLine("3. Blood Group");
Console.WriteLine("4. Salary");
Console.WriteLine("5. Age");
Console.WriteLine("6. Gender");
Console.WriteLine("Please enter the number (1-6): ");
int searchterm = int.Parse(Console.ReadLine());
if (searchterm == 1)
{
Console.WriteLine("Please enter the name of the employee: ");
string name = Console.ReadLine();
Employee emp = employeeList.Find(employee => employee.Name == name);
ValidateEmployee(emp);
}
else if (searchterm == 6)
{
Console.WriteLine("Please enter the gender of the employee: ");
string gender = Console.ReadLine();
List<Employee> emps = employeeList.FindAll(employee => employee.Gender == gender);
ValidateEmployee(emps);
}
//the rest of your "if" conditions for search terms...
else
{
Console.WriteLine("Please try again.");
Console.ReadKey();
return;
}
Console.WriteLine("Done. Press any key to exit...");
Console.ReadLine();
}
private static void ValidateEmployee(Employee emp)
{
if (emp.Equals(default(Employee)))
{
//employee not found
Console.WriteLine($"Employee not found by supplied input. Please try again.");
Console.ReadLine();
return;
}
else
{
//output the emplyee info
OutputEmployeeInfo(emp);
}
}
private static void ValidateEmployee(List<Employee> emps)
{
foreach (Employee emp in emps)
{
if (emp.Equals(default(Employee)))
{
//employee not found
Console.WriteLine($"Employee not found by supplied input.");
}
else
{
//output the emplyee info
OutputEmployeeInfo(emp);
}
}
}
private static void OutputEmployeeInfo(Employee employee)
{
Console.Write("Name: " + employee.Name);
Console.WriteLine("Employee ID: " + employee.EmployeeID);
Console.WriteLine("Blood Group: " + employee.BloodGroup);
Console.WriteLine("Salary: " + employee.Salary);
Console.WriteLine("Age: " + employee.Age);
Console.WriteLine("Gender: " + employee.Gender);
Console.ReadKey();
}
private static List<Employee> SeedEmployees()
{
Employee employee1;
Employee employee2;
Employee employee3;
Employee employee4;
Employee employee5;
Employee employee6;
employee1.Name = "James";
employee1.EmployeeID = int.Parse("10");
employee1.BloodGroup = "B +ive";
employee1.Salary = int.Parse("500000");
employee1.Age = int.Parse("26");
employee1.Gender = "Male";
employee2.Name = "Ali Khan";
employee2.EmployeeID = int.Parse("20");
employee2.BloodGroup = "O +ive";
employee2.Salary = int.Parse("250000");
employee2.Age = int.Parse("22");
employee2.Gender = "Male";
employee3.Name = "Jessica Hills";
employee3.EmployeeID = int.Parse("30");
employee3.BloodGroup = "A -ive";
employee3.Salary = int.Parse("400000");
employee3.Age = int.Parse("25");
employee3.Gender = "Female";
employee4.Name = "William";
employee4.EmployeeID = int.Parse("40");
employee4.BloodGroup = "O -ive";
employee4.Salary = int.Parse("700000");
employee4.Age = int.Parse("29");
employee4.Gender = "Male";
employee5.Name = "Lizzy";
employee5.EmployeeID = int.Parse("50");
employee5.BloodGroup = "AB +ive";
employee5.Salary = int.Parse("70000");
employee5.Age = int.Parse("19");
employee5.Gender = "Female";
employee6.Name = "Kyle";
employee6.EmployeeID = int.Parse("60");
employee6.BloodGroup = "AB -ive";
employee6.Salary = int.Parse("600000");
employee6.Age = int.Parse("21");
employee6.Gender = "Male";
return new List<Employee>
{
employee1,
employee2,
employee3,
employee4,
employee5,
employee6
};
}
}
}

Related

Keep int variables values from dataReader c#

I don't have much experience with C# but I am trying to make a simple windows forms app with personal finances.
So, I have 2 dataReader (I am using the Oracle provider), and the sql (oracle table) commands that select only 2 columns from a table, only with 1 value, mainly income 1 and income2 and the sum of all values from a specific month.
the sql strings look like this:
strSQL_sel_income1 = "select DISTINCT categorie,SUM(suma) from financiar where main_categ='income' and categorie IN ('income1') and EXTRACT(month FROM data)=" + luna_income + " Group by categorie";
strSQL_sel_income2 = "select DISTINCT categorie,SUM(suma) from financiar where main_categ='income' and categorie IN ('Income2') and EXTRACT(month FROM data)=" + luna_income + " Group by categorie";
the "luna_income" value is taken from a combobox where I select a specific month.
The problem is when I try to declare an Int variable from the values I get with data reader and these variables are not kept outside the while statement... dr_income1/2 being the dataReader
if (dr_income1.HasRows)
{
while (dr_income1.Read())
{
label26.Text = dr_income1.GetString(0) + ": " + dr_income1.GetInt32(1) + "\n";
int suma_income1 = dr_incomei1.GetInt32(1);
}
}
else
{
label26.Text = "No info;
}
so, I have two similar data readers and two int variables suma_income1 and suma_income2. If I try to make a sum of them, outside the WhIle codes, I get a zero value. Where should I declare the two variables and how to keep their values?
int suma_income_total = suma_income1 + suma_income2;
label29.Text = "Income total: " + suma_income_total;
The suma_income_total is ZERO!!!
dr_income1 = cm1.ExecuteReader();
dr_income2 = cm2.ExecuteReader();
label26.Text = "";
label28.Text = "";
if (dr_income1.HasRows)
{
while (dr_income1.Read())
{
label26.Text = dr_income1.GetString(0) + ": " + dr_income1.GetInt32(1) + "\n";
int suma_income1 = dr_income1.GetInt32(1);
}
}
else
{
label26.Text = "No info";
}
if (dr_income2.HasRows)
{
while (dr_income2.Read())
{
label28.Text = dr_income2.GetString(0) + ": " + dr_income2.GetInt32(1) + "\n";
int suma_income2 = dr_income2.GetInt32(1);
}
}
else
{
label28.Text = "no info";
}
int suma_income_total = suma_income1 + suma_income2;
label29.Text = "income total: " + suma_income_total;
dr_income2.Close();
dr_income1.Close();
I put some changes in your code. It is not ideal since there are several much simple ways. But it is ok as workaround:
dr_income1 = cm1.ExecuteReader();
dr_income2 = cm2.ExecuteReader();
label26.Text = "";
label28.Text = "";
var suma_income1 =0;
var suma_income2 =0;
if (dr_income1.HasRows)
{
while (dr_income1.Read())
{
label26.Text = dr_income1.GetString(0) + ": " + dr_income1.GetInt32(1) + "\n";
suma_income1 += dr_income1.GetInt32(1);
}
}
else
{
label26.Text = "No info";
}
if (dr_income2.HasRows)
{
while (dr_income2.Read())
{
label28.Text = dr_income2.GetString(0) + ": " + dr_income2.GetInt32(1) + "\n";
suma_income2 += dr_income2.GetInt32(1);
}
}
else
{
label28.Text = "no info";
}
int suma_income_total = suma_income1 + suma_income2;
label29.Text = "income total: " + suma_income_total;
dr_income2.Close();
dr_income1.Close();

How to get the value of a function outside the loop "while" in C#?

bool Estprenom = false;
while (!Estprenom)
{
string prenom = Console.ReadLine();
if (prenom == "x" || prenom == "y")
{
Estprenom = true;
Console.WriteLine("..." + prenom + "...");
}
else
{
Estprenom = false;
Console.WriteLine("Error " + prenom + " is not your name !\n");
}
}
This is my code, and i'd like to know how to use the value " prenom " outside the loop " While ". In this example, the value is set by the user via a " Console.ReadLine ", but I can't use it outside the loop. If I put it before the loop :
bool Estprenom = false;
string prenom = Console.ReadLine();
The loop will never stop if the name written is wrong.
How can I get the value of the string " prenom " outside the loop so I can write :
while (!Estprenom)
{
(...)
}
Console.WriteLine("..." + prenom );
Thank you for your help !
Simply declare the variable outside of the loop and then assign it inside the loop:
string prenom;
while (!Estprenom)
{
...
prenom = Console.ReadLine();
...
}
Console.WriteLine("..." + prenom );
Just pull the prenom value out of the loop scope.
bool Estprenom = false;
string prenom;
while (!Estprenom)
{
prenom = Console.ReadLine();
if (prenom == "x" || prenom == "y")
{
Estprenom = true;
Console.WriteLine("..." + prenom + "...");
}
elsestring
{
Estprenom = false;
Console.WriteLine("Error " + prenom + " is not your name !\n");
}
}
Console.WriteLine(prenom);
Thank you for your answers.
Well, I did use your code, but .. It doesn't seem to work. I still got an error.
bool Estprenom = false;
string prenom;
while (!Estprenom)
{
prenom = Console.ReadLine();
if (prenom == "x" || prenom == "y")
{
Estprenom = true;
Console.WriteLine("..." + prenom + "...");
}
else
{
Estprenom = false;
Console.WriteLine("Error " + prenom + " is not your name !\n");
}
}
Console.WriteLine(prenom);
It's still saying that the "prenom" value in "Console.WriteLine(prenom) is not assigned. I don't understand why...

How can add an another input from classes

Hello I have a little problem. I make some classes and inheritance them and now I need to know how to add another value in the same method. Need to have 2 different value :
` hastane Hbenim = new hastane();
Hbenim.ID = 1234;
Hbenim.SpitalNAme = "AmericA HosPital";
Hbenim.SpitalAdres = "Tirana Albania";
Hbenim.workersNumber = 100;
Hbenim.PersonelAdi = "Amir";
Hbenim.PersonelAdres = "KAraj";
Hbenim.PersonelTCno = 9928931;
Hbenim.PersonelAdres = "Elbasan Arnavutluk";
Hbenim.ServisIdNum = 34;
Hbenim.ServisName = "Albatur Expres";
Hbenim.ServicePlace = "Elbasan";
Console.WriteLine("Hospital Id Number: " + Hbenim.ID);
Console.WriteLine("Hospital Name: " + Hbenim.SpitalNAme);
Console.WriteLine("Hospital Adres: " + Hbenim.SpitalAdres);
Console.WriteLine("\nHosppital Workers Number: " + Hbenim.workersNumber);
Console.WriteLine("Hospital Personel1 Name: " + Hbenim.PersonelAdi);
Console.WriteLine("Hospital Personel2 Name: " + Hbenim.PersonelAdi);
Console.WriteLine("Hospital Servise No: " + Hbenim.ServisIdNum);
Console.WriteLine("Hospital Services Name: " + Hbenim.ServisName);
Console.WriteLine("Hospital Services Place: " + Hbenim.ServicePlace);
Console.WriteLine("\n");
Console.ReadLine();
}
}
}`
How to add another personnel value in Hbenim methods. Thank you
Try a nested collection for the personnel information such as:
public class Hbenim{
string blah
string blah
IList<Personnel> Personnel
}
public class Personnel{
string blah
string blah
}

Add pause to Alexa without using SSML

Is there a way to add a pause (preferably 1 second) in Amazon Alexa without using SSML? Perhaps there is a trick I can do with the Outputspeech.Text and I just don't know it.
Below, I am saying "Here are works of art by {artist name}" but the name and the start of the works of art become mixed together - in spite of the period - so I end up with things like "Here are the works of art by Pablo Picasso Harlequin..."
I am using C# and my own https endpoint, not AWS Lambda.
Any suggestions? Otherwise I will add it as SSML. Thanks.
var output = new StringBuilder();
var outputCard = new StringBuilder();
string m_location;
string m_current_location;
string m_artist = dt_artist.Rows[0]["DisplayName"].ToString();
output.Append("here are works of art for " + m_artist + ". ");
outputCard.Append("Here are works of art for " + m_artist + ".\n\n");
foreach (DataRow dr in dt_artist_objs.Rows)
{
m_current_location = dr["CurrentLocation"].ToString();
if (m_current_location == " ")
{
m_location = "The location is not available.";
}
else
{
m_location = "It is located on the " + m_current_location;
}
output.Append(dr["Title"].ToString() + " is a " + dr["Classification"].ToString() + ". The medium is " + dr["Medium"].ToString() + ". " + m_location);
outputCard.Append(dr["Title"].ToString() + ", " + dr["Dated"].ToString() + " is a " + dr["Classification"].ToString() + ". The medium is " + dr["Medium"].ToString() + ". " + dr["Creditline"].ToString() + ". " + m_location + ".\n"); // It is located on the " + dr["CurrentLocation"].ToString());
}
sql_conn_data.Close();
response.Response.OutputSpeech.Text = output.ToString();
response.Response.Card.Title = "Art";
response.Response.Card.Type = "Standard";
response.Response.Card.Text = outputCard.ToString();
response.Response.ShouldEndSession = true;
return response;
UPDATE
OK. Ended up going the SSML route which looks like this:
var output = new StringBuilder();
var outputCard = new StringBuilder();
string m_location;
string m_current_location;
string m_location_card;
string m_artist = dt_artist.Rows[0]["DisplayName"].ToString();
output.Append("<speak>");
output.Append("here are works of art for " + m_artist + ". <break time='1s'/> ");
outputCard.Append("Here are works of art for " + m_artist + ".\n\n");
foreach (DataRow dr in dt_artist_objs.Rows)
{
m_current_location = dr["CurrentLocation"].ToString();
if (m_current_location == " ")
{
m_location = "The location is not available. <break time='1s' />";
m_location_card = "The location is not available. ";
}
else
{
m_location = "It is located on the " + m_current_location + "<break time = '1s' />";
m_location_card = "It is located on the " + m_current_location;
}
output.Append(dr["Title"].ToString() + " is a " + dr["Classification"].ToString() + ". The medium is " + dr["Medium"].ToString() + ". " + m_location);
outputCard.Append(dr["Title"].ToString() + ", " + dr["Dated"].ToString() + " is a " + dr["Classification"].ToString() + ". The medium is " + dr["Medium"].ToString() + ". " + dr["Creditline"].ToString() + ". " + m_location_card + ". \n");
}
output.Append("</speak>");
sql_conn_data.Close();
response.Response.OutputSpeech.Ssml = output.ToString();
response.Response.OutputSpeech.Type = "SSML";
response.Response.Card.Title = "Art";
response.Response.Card.Type = "Standard";
response.Response.Card.Text = outputCard.ToString();
response.Response.ShouldEndSession = true;
return response;
}
There is not a way to introduce a pause in Alexa without SSML. You will need to build the ssml string and return it back to Alexa using the pause, or the cadence strings.

When running my basic c# console app, my console opens blank and I don't know why

I'm making a small game to assist me in learning c#. All I have done so far is added some variables like fullName, firstName, and lastName so I can reference them later on. That worked fine. But when I cam to the issue of adding a name prefix like Mr. or Ms. (I called them "titles" in the code) the console just opens up blank. Here's my code...
using System;
namespace My_Fist_Console_Game
{
class Program
{
static void Main(string[] args)
{
string userValue = Console.ReadLine();
Console.WriteLine("Airport Sim: Console Edition (literally)");
Console.Write("Type your first name here: ");
string firstName = Console.ReadLine();
Console.Write("Type your last name here: ");
string lastName = Console.ReadLine();
string title;
Console.Write("are you male or female?");
if ((userValue == "male") || (userValue == "Male"))
{
title = "Mr. ";
}
else if ((userValue == "female") || (userValue == "Female"))
{
title = "Ms. ";
}
string fullName = firstName + " " + lastName;
Console.ReadLine();
Console.Clear();
Console.WriteLine("Flight Attendant: ");
Console.WriteLine(" Hello " + title + " " + lastName);
Console.ReadLine();
}
}
}
The first statement in your program is Console.ReadLine which waits for the user to input something. Since you never told it to print to the console before that the console opens and appears blank.
If you input something and hit "Enter" then the code could continue executing and you would see output.
using System;
namespace My_Fist_Console_Game
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Airport Sim: Console Edition (literally)");
Console.Write("Type your first name here: ");
string firstName = Console.ReadLine();
Console.Write("Type your last name here: ");
string lastName = Console.ReadLine();
string title;
while (title != "Mr. " && title != "Ms. ")
{
Console.Write("are you male or female? Input male, if you are male or imput female if you are female");
string userValue = Console.ReadLine();
if ((userValue == "male") || (userValue == "Male"))
{
title = "Mr. ";
}
else if ((userValue == "female") || (userValue == "Female"))
{
title = "Ms. ";
}
}
string fullName = firstName + " " + lastName;
Console.ReadLine();
Console.Clear();
Console.WriteLine("Flight Attendant: ");
Console.WriteLine(" Hello " + title + " " + lastName);
Console.ReadLine();
}
}
}
Put your uservalue variable above your condition:
string userValue = Console.ReadLine();
if ((userValue == "male") || (userValue == "Male"))
{
title = "Mr. ";
}
Then try to edit this part:
string fullName = firstName + " " + lastName;
Console.ReadLine(); //Remove this and put it after Console.WriteLine
Console.Clear(); //Remove this and put it after Console.WriteLine
Console.WriteLine("Flight Attendant: ");
Console.WriteLine(" Hello " + " " + lastName);
What you are doing is you are putting Console.ReadLine() which will expect any input from the user first before anything happens on your program.

Categories