Defford Defford - 1 month ago 18
C# Question

Airplane seat reservation using c#

I stumbled across a source-code for a seat reservation system online (using a console) in c++. The program allows one to select first class, business class and economy class on an airplane, select row and finally a seat. I have been trying to rewrite the same code but in c# but encountered errors, below is the code:

static void Main(string[] args)
{
int[,] airplane = new int[13, 6];
sbyte[,] airchar = new sbyte[13, 6];
string ticket;
int[] row = new int[2];


// sbyte seat;

//--------------------------------------------
for (int i = 0; i < 13; i++)
{
for (int j = 0; j < 6; j++)
{
airchar[i, j] = (sbyte)'*';
airplane[i, j] = 0;

}
}
//--------------------------------------------
for (;;)
{
Console.Write("Rows 1 and 2 are first class (FC)");
Console.Write("\n");
Console.Write("Rows 3 through 7 are business class (BC)");
Console.Write("\n");
Console.Write("Rows 8 through 13 are economy class (EC)");
Console.Write("\n");
Console.Write("\n");
Console.Write("* - Available");
Console.Write("\n");
Console.Write("X - Occupied");
Console.Write("\n");
Console.Write("\n");

Console.Write(" \tA\tB\tC\tD\tE\tF");
Console.Write("\n");
Console.Write("Row 1\t");
Console.Write(airchar[0, 0]);
Console.Write("\t");
Console.Write(airchar[0, 1]);
Console.Write("\t");
Console.Write(airchar[0, 2]);
Console.Write("\t");
Console.Write(airchar[0, 3]);
Console.Write("\t");
Console.Write(airchar[0, 4]);
Console.Write("\t");
Console.Write(airchar[0, 5]);
Console.Write("\n");

Console.Write("Row 2\t");
Console.Write(airchar[1, 0]);
Console.Write("\t");
Console.Write(airchar[1, 1]);
Console.Write("\t");
Console.Write(airchar[1, 2]);
Console.Write("\t");
Console.Write(airchar[1, 3]);
Console.Write("\t");
Console.Write(airchar[1, 4]);
Console.Write("\t");
Console.Write(airchar[1, 5]);
Console.Write("\n");

Console.Write("Row 3\t");
Console.Write(airchar[2, 0]);
Console.Write("\t");
Console.Write(airchar[2, 1]);
Console.Write("\t");
Console.Write(airchar[2, 2]);
Console.Write("\t");
Console.Write(airchar[2, 3]);
Console.Write("\t");
Console.Write(airchar[2, 4]);
Console.Write("\t");
Console.Write(airchar[2, 5]);
Console.Write("\n");

Console.Write("Row 4\t");
Console.Write(airchar[3, 0]);
Console.Write("\t");
Console.Write(airchar[3, 1]);
Console.Write("\t");
Console.Write(airchar[3, 2]);
Console.Write("\t");
Console.Write(airchar[3, 3]);
Console.Write("\t");
Console.Write(airchar[3, 4]);
Console.Write("\t");
Console.Write(airchar[3, 5]);
Console.Write("\n");

Console.Write("Row 5\t");
Console.Write(airchar[4, 0]);
Console.Write("\t");
Console.Write(airchar[4, 1]);
Console.Write("\t");
Console.Write(airchar[4, 2]);
Console.Write("\t");
Console.Write(airchar[4, 3]);
Console.Write("\t");
Console.Write(airchar[4, 4]);
Console.Write("\t");
Console.Write(airchar[4, 5]);
Console.Write("\n");

Console.Write("Row 6\t");
Console.Write(airchar[5, 0]);
Console.Write("\t");
Console.Write(airchar[5, 1]);
Console.Write("\t");
Console.Write(airchar[5, 2]);
Console.Write("\t");
Console.Write(airchar[5, 3]);
Console.Write("\t");
Console.Write(airchar[5, 4]);
Console.Write("\t");
Console.Write(airchar[5, 5]);
Console.Write("\n");

Console.Write("Row 7\t");
Console.Write(airchar[6, 0]);
Console.Write("\t");
Console.Write(airchar[6, 1]);
Console.Write("\t");
Console.Write(airchar[6, 2]);
Console.Write("\t");
Console.Write(airchar[6, 3]);
Console.Write("\t");
Console.Write(airchar[6, 4]);
Console.Write("\t");
Console.Write(airchar[6, 5]);
Console.Write("\n");

Console.Write("Row 8\t");
Console.Write(airchar[7, 0]);
Console.Write("\t");
Console.Write(airchar[7, 1]);
Console.Write("\t");
Console.Write(airchar[7, 2]);
Console.Write("\t");
Console.Write(airchar[7, 3]);
Console.Write("\t");
Console.Write(airchar[7, 4]);
Console.Write("\t");
Console.Write(airchar[7, 5]);
Console.Write("\n");

Console.Write("Row 9\t");
Console.Write(airchar[8, 0]);
Console.Write("\t");
Console.Write(airchar[8, 1]);
Console.Write("\t");
Console.Write(airchar[8, 2]);
Console.Write("\t");
Console.Write(airchar[8, 3]);
Console.Write("\t");
Console.Write(airchar[8, 4]);
Console.Write("\t");
Console.Write(airchar[8, 5]);
Console.Write("\n");

Console.Write("Row 10\t");
Console.Write(airchar[9, 0]);
Console.Write("\t");
Console.Write(airchar[9, 1]);
Console.Write("\t");
Console.Write(airchar[9, 2]);
Console.Write("\t");
Console.Write(airchar[9, 3]);
Console.Write("\t");
Console.Write(airchar[9, 4]);
Console.Write("\t");
Console.Write(airchar[9, 5]);
Console.Write("\n");

Console.Write("Row 11\t");
Console.Write(airchar[10, 0]);
Console.Write("\t");
Console.Write(airchar[10, 1]);
Console.Write("\t");
Console.Write(airchar[10, 2]);
Console.Write("\t");
Console.Write(airchar[10, 3]);
Console.Write("\t");
Console.Write(airchar[10, 4]);
Console.Write("\t");
Console.Write(airchar[10, 5]);
Console.Write("\n");

Console.Write("Row 12\t");
Console.Write(airchar[11, 0]);
Console.Write("\t");
Console.Write(airchar[11, 1]);
Console.Write("\t");
Console.Write(airchar[11, 2]);
Console.Write("\t");
Console.Write(airchar[11, 3]);
Console.Write("\t");
Console.Write(airchar[11, 4]);
Console.Write("\t");
Console.Write(airchar[11, 5]);
Console.Write("\n");

Console.Write("Row 13\t");
Console.Write(airchar[12, 0]);
Console.Write("\t");
Console.Write(airchar[12, 1]);
Console.Write("\t");
Console.Write(airchar[12, 2]);
Console.Write("\t");
Console.Write(airchar[12, 3]);
Console.Write("\t");
Console.Write(airchar[12, 4]);
Console.Write("\t");
Console.Write(airchar[12, 5]);
Console.Write("\n");
Console.Write("\n");


// Problem starts here

Console.Write("Enter Ticket type (FC, BC, or EC): ");
ticket = ConsoleInput.ReadToWhiteSpace(true);
Console.Write("Desired Row: ");
row[0] = ConsoleInput.ReadToWhiteSpace(true);
Console.Write("Desired seat (A,B,C,D,E or F): ");
seat = ConsoleInput.ReadToWhiteSpace(true);


switch (seat)
{
case 'A':
case 'a':
row[0] = row[0] - 1;
row[1] = 1;
row[1] = row[1] - 1;
break;

case 'B':
case 'b':
row[0] = row[0] - 1;
row[1] = 2;
row[1] = row[1] - 1;
break;

case 'C':
case 'c':
row[0] = row[0] - 1;
row[1] = 3;
row[1] = row[1] - 1;
break;

case 'D':
case 'd':
row[0] = row[0] - 1;
row[1] = 4;
row[1] = row[1] - 1;
break;

case 'E':
case 'e':
row[0] = row[0] - 1;
row[1] = 5;
row[1] = row[1] - 1;
break;

case 'F':
case 'f':
row[0] = row[0] - 1;
row[1] = 6;
row[1] = row[1] - 1;
break;

}


The syntax error appears in ConsoleInput.ReadToWhiteSpace(true); for ticket, row[0] and seat. Also another error that occurred is in the switch statement where all cases are highlighted and states "Cannot implicitly convert type 'char' to 'sbyte'. An explicit conversion exists (are you missing a cast?)"

I would be extremely grateful if someone could help me with this. Thank you!

Answer

In switch you have seat which is sbyte and in case you case chars. so either convert each char in case to sbyte or return char from method ReadToWhiteSpace.

Better you can use Console.ReadLine() or Console.Read()(gives ASCII value) instead of ConsoleInput.ReadToWhiteSpace to get input from console.