Why do people with a lung infection cough a lot?

Inflammation. Inflammation in the lung triggers reflexes that cause cough. Also mucous caused by the inflammation does the same. The lungs are trying to clear the germs and debris.
Reflex... Cough is a reflex designed to rid the airways of infection. It prevents the infection from spreading to the rest of the lung and to other parts of the body. So, in bronchitis, the person is coughing in an attempt to get rid of the presumed infection. In chronic bronchitis, no infection exists but the body can't tell the difference so cough ensues.

Related Questions

How do I know if I have a lung infection if I can't get an xray done? I am 26 weeks pregnant and have had a chronic cough for 2 and a half months.

Go talk to your DR. Chronic cough suggests bronchitis- excessive mucous production and inflammation of your bronchial tubes or a sign of asthma. Pneumonia will present with worsening of above symptoms, persistent fever, shortness of breath, weakness, body aches. Ask your dr if antibiotics is all you need plus some mucolytics- Robitussin-DM or mucinex (guaifenesin). Best wishes! Read more...
You can get an xray. With proper shielding (there should be a lead shield around your abdomen (belly) you can have a chest xray if it is medically necessary. Talk with your doctor and make sure that the xray technologist is aware you are pregnant and shields you appropriately. Read more...