The primary grammatical rule at play here is Parallel Structure (Parallelism).
Parallelism: When a sentence lists two or more actions or ideas, they must follow the same grammatical pattern. In the original sentence, the doctor gave two pieces of advice:
Action 1: to avoid eating sweets.
Action 2: doing exercise regularly.
"To avoid" (infinitive) and "doing" (gerund) do not match. Since the sentence begins with "The doctor advised him to...", the second action should also be in the infinitive form (to exercise).
Logic and Meaning: The original sentence literally says he should "avoid... doing exercise regularly," which is the opposite of what a doctor would typically advise! By changing the structure to "to avoid eating sweets and to exercise," the sentence clearly separates the two distinct instructions:
Instruction A: Avoid sweets.
Instruction B: Exercise regularly.