Despite being in the north of Spain, Barcelona isn't a million miles from the French Riviera and so enjoys good weather for much of the year. From July to September, Barcelona enjoys quite hot weather, with average temperatures in excess of 20ºC (68ºF). Note that this includes night time temperatures - in the day time, temperatures can reach 30ºC (86ºF). September and October are typically Barcelona's wettest months.
Madrid is the highest capital city in Europe and its altitude gives it quite unpredictable weather conditions for much of the year. I spent the whole of 2004 in Madrid - up until mid-May I saw glorious sunshine in February and torrential rain in May, although 2005 was far more reasonable. The average temperatures shoot up after May, with July and August being incredibly hot. Many Spaniards leave the city for a couple of months during this time, which can leave the city feeling like a ghost town (albeit inhabited by large quantities of tourists).