Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C# Hello, I am having trouble changing this code to read letters instead of numb

ID: 3727615 • Letter: C

Question

C# Hello, I am having trouble changing this code to read letters instead of numbers. Please reply with the solution and explain. Thanks.

Console.WriteLine("For Monday enter M, For Tuesday enter T:");

//Using try parse to handle invlaid values gracefully
int.TryParse(Console.ReadLine(), out day);
if (day != 0 && day < 3)  
{
for (int i = 0; i < 8; i++) // would like for this to read M ( for morning), and A (for afternoon).
Console.WriteLine(string.Format("For Slot {0}({1}) choose {2}", i + 1, i < 4 ? "Morning" : "Afternoon", i + 1));
int.TryParse(Console.ReadLine(), out time);
if (time != 0 && time < 9)
{
Console.WriteLine("For Cleaning enter 1, For Procedures enter 2:"); // C = Cleanin , P= Procedures
int.TryParse(Console.ReadLine(), out type);
if (day != 0 && day < 3)
{
//Check and conform appointments
app.CheckAppt(day - 1, time - 1, type);
}
else
{
Console.WriteLine("Invalid Selection, Please try again.");
return;
}
}
else
{
Console.WriteLine("Invalid Selection, Please try again.");
return;
}
}
else
{
Console.WriteLine("Invalid Selection, Please try again.");
return;
}

Explanation / Answer

Hi,

Normally all input values your reading from line will be String type only, So Tryparse is very usefull when do you want to keep your number input values as Int. So in your case tryparse will not work, so I have given alternate method to handle string input and convert to int. This will help for your requirement.

Here, I have used direct char converter to convert letter into thier int32 values. Say example, M is equal to 77 and T is equal to 84, So same way you can use for all cases in your code and use that number in if condition.

Please find below modifed code and let me know if you need anything.

using System;

class Program
{
static void Main()
{
Console.WriteLine("For Monday enter M, For Tuesday enter T:");
//int day;
string stringInput = Console.ReadLine();
//int.TryParse(stringInput, out day);
char[] value =stringInput.ToCharArray();
Console.WriteLine(value);
foreach (char letter in value)
{
int time,type;
int value_int = Convert.ToInt32(letter);
Console.WriteLine(value_int);
if (value_int == 77 | value_int == 84)
{
Console.WriteLine("For to read M ( for morning), and A (for afternoon)");
string stringInputMA = Console.ReadLine();
char[] readMA =stringInputMA.ToCharArray();
foreach (char letter1 in readMA)// would like for this to read M ( for morning), and A (for afternoon).
{
int writeMA = Convert.ToInt32(letter1);
if (value_int == 77 | value_int == 65)
{
Console.WriteLine(writeMA);
Console.WriteLine("Value for M and A ok - If block");
int.TryParse(Console.ReadLine(), out time);
if (time != 0 && time < 9)
{
Console.WriteLine("For Cleaning enter 1, For Procedures enter 2:"); // C = Cleanin , P= Procedures
string stringInputCP = Console.ReadLine();
char[] readCP =stringInputMA.ToCharArray();
int.TryParse(Console.ReadLine(), out type);
foreach (char letter2 in readCP)
{
if (type != 0 && type < 3)
{
//Check and conform appointments
//app.CheckAppt(day - 1, time - 1, type);
}
else
{
Console.WriteLine("Invalid Selection, Please try again.");
return;
}
}
  
}
else
{
Console.WriteLine("Invalid Selection, Please try again.");
return;
}
}
}
  
}
else
{
Console.WriteLine("Else");
}
}
}
}