-1
int anotherShowing = 1; // Variable for the user to select another showing.

        while (anotherShowing == 1) // While loop so the program keeps going until the user has selected a film.
        {
            Console.Write("Select A Showing: "); // Outputs "Select A showing" to the console window.
            int showingSelect = Console.ReadLine(); // Variable for the user to input a showing number and converts it to a int.
            int.TryParse(anotherShowing, out showingSelect);
            if (showingSelect == 0)
            {
                Console.Write("please input valid number");
            }
            Showing Selection; // Variable so Selection can be linked to the Showing class to be used for the ShowingSelect.
            Selection = list[showingSelect - 1]; // Recongnizes the showing number the user inputs.
            Console.Write("Number of Tickets: "); // Oupts "Number of Tickets" to the console window.
            int numberOfTickets = Convert.ToInt32(Console.ReadLine()); // Variable for the user to input how many tickets they want and converts it to a int. 
            Booking Booking1; // Variable so Booking1 can be linked to the Booking class to be used for the NumberOfTickets.
            if (Selection.getSeatsAvailable() > numberOfTickets) // If statement so the program knows if there are enough tickets left.
            {
                Booking1 = new Booking(Selection, numberOfTickets); // If enough tickets program will make a new booking (Booking1) which includes the Selection and the NumberOfTickets.
                Selection.updateSeatsAvailable(numberOfTickets); // Updates how many tickets are available for that showing.
                Console.Clear(); // Clears the console window.
                Console.Write(Booking1.toString()); // Outputs the Booking1 variable, including the Film selected and the number of tickets.
                break; // Stops the program.
            }
            else // If there aren't enough tickets.
            {
                Console.WriteLine("Showing Full"); // Outputs "Showing Full" to the console window.
                Console.WriteLine("Select another showing"); // Outputs "Select another showing" to the console window.
            }

            Console.Write("Press 1 Then Return To Select Another Showing\n"); // Outputs to the console window.
            anotherShowing = Convert.ToInt32(Console.ReadLine()); // Allows the user to input 1 to select another showing and converts it to a int.
        }

        Console.Read();
    }

I am trying to write a simple cinema program for a assignment. I looked at some example on how to tell the user to enter a letter instead of number if they enter a number. I have added TryParse however there are some errors:

errors

Please help in code, Thanks

René Vogt
  • 43,056
  • 14
  • 77
  • 99
Tim
  • 33
  • 5
  • 1
    Possible duplicate of [How do I only allow number input into my C# Console Application?](http://stackoverflow.com/questions/13106493/how-do-i-only-allow-number-input-into-my-c-sharp-console-application) – BunkerMentality Jun 09 '16 at 08:08

1 Answers1

2

Console.ReadLine() returns a string not an int value.

so we should be doing this.

int showingSelect;

if(int.TryParse(Console.ReadLine(), out showingSelect))
{
    // valid showingSelect
}
else
{
    // invalid input  
    Console.Write("please input valid number");
    continue;  // continue loop. 
}
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35