Been to both, and spent enough time to fall in love with both
Ghyorghy above was correctly pointing at a few characteristics that make Barcelona a lovely place to be, but Madrid really has a lot of good sides, too! Yeah, you might have to endure a harsher climate - hotter in the summer, colder in wintertime - but if you're after art, you'll love Prado, Reina Sofía and Thyssen-Bornemisza, which you'll find one after another pretty much along the very same avenue
Nightlife has a lot to offer, in different areas of the city, and the whole city center has a lovely feeling about itself.
Barcelona feels warm, relaxed, and art-wise you're gonna lova the whole of gaudì's production, not to mention other museums and little jewels scattered around the city - the Sagrada familia being but one of them.
As a matter of fact visiting both would be harder, coz they're not that close. It wouldn't be a problem, if you could spend some extra money and fly from one to another. Otherwise there's plenty of trains and coaches you can choose among. Even the slower option (the coach) would take you from Madrid to Zaragoza in about 3.5 hours and it would then take you another 3 to reach Barcelona.