If you aren't nuts for modernista architecture (Barcelona) or art (both cities), you have enough time to go to a third city or take multiple day trips. Girona is north of Barcelona and has good sightseeing options. It's especially recommended (by me) to folks who also want to see the Dali Theatre-Museum in Figueres, the former fishing village of Cadaques (with a Dali home nearby) and/or Besalu (medieval town with a fortified bridge). Those are all accessible via public transportation and closer to Girona than to Barcelona. You can get from Barcelona to Girona and onward to Figueres by express train. Those tickets are cheaper if purchased well in advance. Slower, cheaper trains also run that route.
I haven't been to Tarragona (on the coast south of Barcelona), but it's known for its Roman ruins. Sitges is between Barcelona and Tarragona.
Zaragoza is on the way from Madrid to Barcelona via the direct train line. It's a very interesting, much less touristy city. I think it makes an especially good add-on when a trip doesn't extend south of Madrid, because Zaragoza has a well-restored Moorish palace. train tickets on that very busy Madrid-Barcelona rail line sell out frequently, so be careful about that. You can save money by buying early once you're certain of your plans. Check the Renfe website carefully for rules about changes and cancellations.
Toledo, south of Madrid and served by express trains, is a great overnight destination (or multi-night for some people), but many people make it a day trip from Madrid. Those express trains sell out fairly often, so--even though you won't save any money on the rail fare--it's smart not to wait until the last minute to buy the tickets.
Segovia is another very popular and worthwhile day trip from Madrid. Fares to Segovia vary a bit, so you can save some money by committing early.
I haven't needed to buy tickets to La Sagrada Familia or Parc Guell recently, but those are definitely not places you want to walk up to without having pre-purchased tickets. Tickets are timed. I don't know how early La Sagrada Familia sells out, but I believe it sometimes does. It's one of the city's most popular sights, as is Parc Guell. Both tend to be extremely crowded, which makes the first-time-slot tickets especially popular and likely to sell out first. I'd recommend looking today at ticket availability for tomorrow and the other dates between now and your arrival day in Barcelona (or as far out as tickets are for sale). And I'd keep monitoring ticket sales to be sure I didn't end up with a problem seeing those sights. I'm not a believer in buying sightseeing tickets earlier than strictly necessary, because they are usually not changeable or refundable, and sometimes trips get postponed or cancelled.
There are other sights in Barcelona that also call for purchasing tickets in advance. I mention them here in case you plan to see some of them: Casa Mila/La Pedrera, Casa Batllo, Picasso Museum and the Palau de la Musica Catalana. The Palau's English-language tours sometimes sell out. The first three are subject to painfully long ticket lines that you will not want to wait in.
Some visitors to Madrid have reported significant ticket lines for the Royal Palace in Madrid. It is a popular sight, and not just with tourists. I suspect it might help a bit to go on a non-holiday weekday, and I'd recommend buying a ticket online in advance. I think most of the time it's not necessary to pre-purchase tickets to the Prado Museum, but it would be possible to run into a line.