I do agree with the above suggestion.
More in details.
You eat mainly carbs from wheat which is loaded with gluten of the bad kind (hard wheat, creso variant).
Gluten plays a very bad role on your immune system and gut health in general, that translate in likely hormonal imbalances or at least weird reactions to nutrients, usually it leads to gain weight but it might lead to difficult in gaining weight as well, it sounds like a contradiction but often an imbalance can lead to two opposite outcomes.
The point is that the picture is not healthy.
Regarding exercise you have to realize the biggest factor is it leads to an hormonal balance which has huge health implications, a better physique is a welcome side effect but it just reflects an healthier internal environment.
The take home message here is that when you are underweight or overweight the cause may likely be quite the same: an hormonal imbalance.
In order to correct that I suggest to avoid gluten, especially wheat, lower your carbs intake and increase proteins which means automatically to increase fats too, include olive oil, butter and/or coconut oil which are all good healthy fats, substitute wheat carbs with rice (maybe long grain like basmati or jasmine) and/or sweet potatoes.
Eat plenty of veggies of all kind but be aware potatoes, carrots and pumpkins have to be considered carbs.
Every time you eat something insulin release is triggered, insulin is an hormone, if our aim is to optimize hormones to trigger insulin too often is not smart, it might lead to diabetes and anyway doesn't play our game, therefore 4 or 5 meals a day might no be the smartest choice, I would recommend 2 or 3 instead, bigger meals but not so often, that will lead to less frequent insulin spikes which is a good thing.
Training 2 or 3 times a week lifting kind of heavy weights in compound movements and maybe some HIIT (High Intensity Interval Training, like Tabatas, for example) would make a huge impact on your hormones in a positive way.
Following the above recommendations will make the overweight to lose fat and the underweight to gain muscles, those are just main principles, of course, there are many and different strategies to optimize according to individual needs but the nuts and bolts are those described.
I know...it means to dedicate more time and care to prepare proper meals and to find those 2-3 hours a week to train...but it is your health, your body, your life....your choice.