Does bad posture affect height?

Certainly yes!

The spine is compressed due to bad posture while it will be elongated by good posture.

This compression can add up to 2 inches in your height depending on how much compressed the spine is due to your bad posture.