If at first you don’t succeed…

So this is it, the first ‘proper’ program I’ve attempted to write in C#. The task is easy enough to wrap my head around:

Display a list of films on show, ask the user to select a film and input there age. Then check to see if they would be old enough or not. Seems simple right? Well not for me.

You see this is what happens when I run the program (click to expand):

It's not working the way I want it too
It’s not working the way I want it too

Yeah that seems odd. I mean it’s partially working and then also just completely not. Code follows, it’s broken into separate sections as I break down block when I’m writing in Visual Studio anyway (I hear good layout is important for good coding). Beware this is going to go on for a while so you might want to grab a drink and hit auto-scroll or something.

using System;
  class Cinema
 {
 static void Main()
 {
 Console.WriteLine("Welcome to our Multiplex"); 
 Console.WriteLine("We are presently showing:");
 Console.WriteLine("1. Legend (18)");
 Console.WriteLine("2. Macbeth (15)");
 Console.WriteLine("3. Everest (12)");
 Console.WriteLine("4. A Walk in the Wood (15)");
 Console.WriteLine("5. Hotel Transalvania (U)");
int Legend, Macbeth, Everest, Walk, Hotel; 
 Legend = 18;
 Macbeth = 15;
 Everest = 12;
 Walk = 15;
 Hotel = 4;
Console.Write("Please Enter the Film Number: "); 
 string filmnoText = Console.ReadLine();
 int filmno = int.Parse(filmnoText);
 Console.WriteLine("The Film selected is: " + filmno);
if (filmno > 5) 
 {
 Console.WriteLine("You have entered an incorrect film number, please try again.");
 }
Console.Write("Please Enter your age: ");  
 string ageText = Console.ReadLine();
 int age = int.Parse(ageText);
 Console.WriteLine("The Age entered is: " + age);
 const int minage = 4; 
 const int maxage = 95;
 if (age > maxage)
 {
 Console.WriteLine("The Age entered is too old");
 Console.WriteLine("Setting to maximum age");
 age = maxage;
 }
 if (age = Legend) 
 {
 Console.WriteLine("Enjoy the film");
 }
 else
 {
 Console.WriteLine("You cannot see this film");
 }
if (filmno == 2 && age >= Macbeth)
 {
 Console.WriteLine("Enjoy the film");
 }
 else
 {
 Console.WriteLine("You cannot see this film");
 }
if (filmno == 3 && age >= Everest)
 {
 Console.WriteLine("Enjoy the film");
 }
 else
 {
 Console.WriteLine("You cannot see this film");
 }
if (filmno == 4 && age >= Walk)
 {
 Console.WriteLine("Enjoy the film");
 }
 else
 {
 Console.WriteLine("You cannot see this film");
 }
if (filmno == 5 && age >= Hotel)
 {
 Console.WriteLine("Enjoy the film");
 }
 else
 {
 Console.WriteLine("You cannot see this film");
 }
}

Now at the end of all of this I think I know what the issue in this is: all the if statements are being checked and if it finds that one of them is true it gives that response. Whilst also letting me know that all the other if statements are coming back as false. So I’m thinking that should be a nested if statement which should solve this, but also because I have no idea what possessed me to do them all separately?

But first I should cook.

One thought on “If at first you don’t succeed…

Leave a reply to Why do I love nested If statements? | Grappling with C# Cancel reply