Wait for a certain number of keypad inputs

In the function I want the program to wait for 4 key inputs from the keypad and display them to the LCD and when the user presses the # key it clears the LCD and displays a message. Using a while statement and then an if statement brings up two lines per block on a 16x2 LCD.

#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Stepper.h>

#define STEPS 200
#define ledRed 8
#define ledGreen 9
#define input1 52
#define input2 53

const int numRows = 2;
const int numCols = 16;
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = { 40, 50, 48, 44 };
byte colPins[COLS] = { 42, 38, 46 };

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal lcd( 12, 11, 5, 4, 3, 2 );
Stepper stepper(STEPS, 36, 34, 32, 30);
Stepper stepper2(STEPS, 28, 26, 24, 22);

int bays = 10;
int steps = 0;

void setup()
{
  pinMode(input1, INPUT);
  pinMode(input2, INPUT);
  pinMode(ledRed, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  lcd.begin(numCols, numRows);
  lcd.print("Welcome");
  lcd.setCursor(0, 1);
  stepper.setSpeed(30);
}

void loop()
{
  lcdin();
}

int opengate(int i)
{
  int val = digitalRead(input1);
  if (val == LOW)
  {
    steps = 50;
    stepper.step(steps);
    delay(10000);
    steps = -50;
    stepper.step(steps);
    delay(1000);
  }
  i = i - 1;
  return i;
}

int closegate(int i)
{
  int val = digitalRead(input2);
  if (val == LOW)
  {
    steps = 50;
    stepper2.step(steps);
    delay(10000);
    steps = -50;
    stepper2.step(steps);
    delay(1000);
  }
  i = i + 1;
  return i;
}

int ledlight(int i)
{
  if (i >= 1)
  {
    digitalWrite(ledGreen, HIGH);
    lcd.clear();
    lcd.print("Input passcode:");
    lcd.setCursor(0, 1);
  }
  else
  {
    digitalWrite(ledRed, HIGH);
    lcd.clear();
    lcd.print("No spots left");
    lcd.setCursor(0, 1);
  }
}

int lcdin()
{
  char key = kpd.getKey();
  while(key != '#')
  {
    lcd.print(key);
  }
  if (key == '#')
  {
    lcd.clear();
    lcd.print("Input passcode:");
    lcd.setCursor(0, 1);
  }
}

int lcdout()
{
  char key = kpd.getKey();
  if (key)
  {
    switch(key)
    {
      case '#':
        lcd.clear();
        lcd.print("Thx for coming");
        delay(5000);
        lcd.clear();
        lcd.print("Input passcode:");
        lcd.setCursor(0, 1);
        break;
      default:
        lcd.print(key);
    }
  }
}

Please attach your complete sketch, use the </> icon on the left side of the posting menu.

I just attached it after reading one of the pinned posts

  while(key != '#')
  {
    lcd.print(key);
  }

Complete nonsense. If no key is pressed, you will print nothing useful FOREVER. There are times where a while statement makes sense. This is NOT one of them.

while(key != '#')
{
lcd.print(key);
}

Do realize that you will be locked in this loop since key will never change.

PaulS is sooo fast.

It doesn't help me if you don't add anything useful and I realise that I will be stuck in that loop forever because nothing happens, so what can I do to wait for four key inputs and then clear the screen with the hash key, I have not been doing programming for long either.

That while loop continues as long as key is not equal to #
You never update key so once you get in, you never get out.
You have to at least put:
char key = kpd.getKey();
inside the loop to update key.
I would make key global.

You have delay() at different locations, when you are in delay() the sketch cannot do anything until the delay is over.
This effectively freezes things.
Have you studied the Blink Without Delay sketch that comes with the IDE.
You can use BWD techniques to avoid freezing your sketch.

Thanks for the help LarryD, however when I put char key = kpd.getKey() inside the while loop the compiler requires an initializer and when I put char key = kpd.getKey() in the while statment and before it stills comes up with the same two lines on every block, however it does look like it types out the numbers only to be replaced by the lines quickly after.

Take things one step at a time.
Use this, what happens:

int lcdin()
{
  char key = kpd.getKey();
  if (key)
  {
    lcd.setCursor(0,1);
    lcd.print(key);
    if (key == '#')
    {
      lcd.clear();
      lcd.print("Input passcode:");
      lcd.setCursor(0, 1);
    }
  }
}

The code works fine however I need to get the program to wait for 4 keys from the keypad and display so the user can see what they are inputting into the keypad

Ok
Try this, what happens?

int lcdin()
{
  static byte pos = 0;
  static unsigned int passcode = 0;

  char key = kpd.getKey();
  if (key)
  {
    lcd.setCursor(pos++,1);
    lcd.print(key);

    if(key != '*' && key != '#')
    {
      passcode = (passcode *10 ) + (key - '0');
    }

    if (key == '#')
    {
      pos = 0;
      lcd.clear();
      lcd.print("Input passcode:");
      lcd.setCursor(0, 1);
      passcode = 0;
    }

    if (key == '*')
    {
      lcd.clear();
      lcd.print(passcode);
      pos = 0;
      passcode = 0;      
    }
  }
}

Thanks that works just how I would like the program to. The numbers are displayed one after another as you would expect from a pin number and then you can display it to check.

Before getting too deep into your project break it up into manageable sections.
When one aspect works, proceed to the next.
When all sections have been resolved then put them together.

Have you read Robin2's thread:
http://forum.arduino.cc/index.php?topic=261445.0

.

That's why I'm breaking up the program into functions.

Good.
BTW
We urge you to not use delay() in your sketches.
As mentioned, see BWD, and master it.

Also there is a programming technique called "State Machine".
Do some research into using it in your projects.

.