Cancer, particularly certain types like lung cancer, can compromise the immune system, making it harder for the body to fight off infections. Treatments such as chemotherapy, radiotherapy, and immunotherapy can further weaken the immune system, increasing the susceptibility to infections like pneumonia.