Welcome to the first challenge for section 19.
This challenge is all about using what you learned about formatting output
to format a table of tours to different cities in
South America, including prices and so forth,
and we'll get to that in a second.
But I'm in the section 19 workspace,
and there are two projects, challenge_1 is
the source code that I'm providing for you,
and challenge_1_solution is my solution to the
challenge.
So let's take a look at what the challenge is all about, then I'll give you a sample run,
and then you can try it out on your own.
So as I said, it's about formatting output, and we're going to do everything to cout.
We're going to display everything to the console.
Later on we can change that and display the files,and that's what we're going to learn next in this section.
So let's start. I'm giving you this starter source code for the challenge,
and I'll go through that in just a second,
but the challenge is for you to display the provided data in a nicely formatted table.
Your table doesn't have to match mine. You can do however you like.
Now the point is for you to become comfortable with these manipulators,
play around with them, don't just try to mimic my table exactly.
Be creative, do your own thing.
So I'm going to give you three structures. We haven't really done
these sorts of structures before, but they're really, really straightforward.
They're not classes. We're going to use structures because
we don't really need to model operations. We just want to model data.
And when we're modeling data. It's really common to just use structures.
Again, I'll show you what they look like in just a second.
So the first thing is these structures work together to create a tours structure,
and that's going to contain about information about tours to South America.
And the tours include the countries that you can visit,
the cities within those countries,
and those cities have population and cost data associated with the tour.
I know population, I was thinking the same thing.
I just wanted a really big number so that you could format it.
Watch the challenge video, which you're obviously watching right now.
But if you're looking at the code, I'm hoping that you come back to this video and watch it,
so that you can see what the expected output looks like. I tried to copy the output to this
document here,
but everybody's got different fonts, and some of the fonts
aren't mono-spaced and the document would look really weird.
So I'll just run it for you so you can see exactly what it's supposed to look like.
In the code, I'm going to give you some starter code that displays all the data
from these structures and vectors
using tabs to kind of align things and it looks pretty ugly.
But your job is to clean it up make it look pretty.
Okay. So as I always say have fun, create functions, constants.
Do whatever you think makes sense, explore, play around.
Do it, and then do it again a different way. That's how you really really learn.
Okay. So let's go to the source code. So again, I'm in the challenge_1 project.
And I'm looking at main code right here.
And let's go over these data structures. First of all,
what we've got is we're including our normal
group here.
We've got iostream, we've got iomanip since we're going to use the manipulators.
We've got a vector. And we've got a string.
And we've got these three structures right here. Here's the first one. It's city,
country and tours. And let me start with city.
Think of a struct. We we talked about a struct a little bit earlier in the object-oriented classes
and fun methods section.
It's like a class except everything's public,
you can have constructors, you can have all kinds of stuff. But
typically, we use structs like this
to just model data, sometimes you just want pure data
and you want to deal with constructors and methods. And you really don't need any of that.
So this is just a good way to collect data together. So what we've got here is we've got a city,
and each city has a name. So let's say the name for one of the cities is
Bogota in Colombia,
a population which is a long.
And I don't know how many millions there are.
I made some of those numbers up.Let's just say 18 million or something like that.
And the cost of taking a tour to Bogota from Miami.
So that's 569 dollars. And again, I don't know what that number is, it's in the
data you'll see in a minute.
So that's a city.
We're going to have many of these cities, right. We'll have Rio de Janeiro. We'll have Santiago Chile.
We'll have a whole bunch of places that we can visit in South America.
Each one will be modeled by a city.
Perfect.
Now let's look at the country.
The country has a name in this case, it would be Colombia,
right. And it has a std vector of cities.
So I might have three or four cities associated with Colombia.
I might have two or three cities associated with Brazil and so forth.
So this is how we structure our data using these structures building
one within the other.
Finally, we've got our main object here, which is the tours, and it has a title,
and that title,
I don't remember exactly what it is, but I'll show it to you in a minute when I show you the data.
It's something like the tours to South America or something and some
big title that will be the header on our report.
And the tours has a vector of countries.
Okay, makes sense. So we could have the title, which is this guy.
And then we could have, let's say, Columbia,
and we could have Brazil
as our countries, and we could have Argentina.
And then the countries have cities within them. You can see them right here.
So you may have Bogota here,
and the population is 18 million
and the cost is $567 or something like that.
Then you would display the next city and the next city.
Then you'd go to Brazil, and you'd display Rio and so forth.
So this is what our report is going to look like.
And the reason we structure this data is because it makes it so much easier
to process that way. We don't have to just play around with hundreds of arrays
and strings and so forth.
So we're using what we should be using. We're using std strings, and we're using std vectors.
I mean that's what modern c++ is all about. It's not using
character pointers, and we're not using anything else,
just real solid data structures.
Okay. So how do we initialize these guys.
Let me scroll down just a little bit.
And here you see
some of the initialization. And this is done for you. I've already written this for you.
You could just use it.
Notice that this piece right here. This is using our uniform initialization,
right. This is the kind of stuff we've been using all along,
except we're doing it all in one shot here.
So here is a city.
Here's another city. Here's another city. Here's another city.
These four cities are a vector.
And that vector is the vector of cities in that country Columbia,
right. And that country Columbia is
one of these guys, and here's the title, at the top of the tours.
So what we're doing is we're building this using uniform initialization,
which makes it really, really nice to visually see what's going on here.
So you could see that I've got, let me scroll up just a little bit here,
so hopefully, we can see them all.
Yep, I think we can see them all. Great. So in this case,
there's my title.
Okay. Here is a country, another country,
another country, another country.
Okay. I don't know what happened to my mouse. It all of a sudden got really big,
let me fix that real quick. And
within those countries, I've got cities, and cities,
and cities. And here's a single city. You can always assume that there must be --
if you've got a country, you must have a city
so you can assume that you're never going to have a country without a city. That'll just make your
processing a little bit easier.
That's it. So that's our data.
And let me show you how you would work with this data.
So let me scroll up and clear this a little bit. Here's the code I'm giving you.
And you may not see it right off the bat,
but it should be pretty intuitive once you start walking to it and thinking about it a little bit.
You can see what we're starting out here, we're displaying the title.
The title is part of that tours object, right tours.title.
And then for each
country that's in the countries in that tour object.
So we're going to loop through all the different countries,
right, Colombia, Brazil, Argentina and so forth.
For each one, I'm displaying the name.
And then I'm looking through that cities vector that's inside of it.
And for each city in that city's vector, I'm displaying the city.
That's really it. It's really, really straightforward.
Again, take a minute, understand the concept, walk through this.
Before you start trying to format this data, make sure you understand how we're getting the data,
it's real important.
Once we do that, we can display this information.
It's not going to look very pretty because you can see we're using tab characters
to kind of align these columns,
and it's not going to do a real good job but at least you'll be able to see what's going on.
If I run this now, it'll be the output that you should be getting as well.
So let me run that project right now, that's what you should get.
Tour ticket prices from Miami, you can see Colombia and the city is Brazilian, city is Chile
and Argentina.
But you can see this isn't lined up nicely because we're not doing any real formatting,
we're just using the tab character.
Okay. So let me stop that, and then let me show you the solution output
looks something like this, which is much nicer.
And I'm displaying this ruler up here.
I'm doing this in a width of 70 total, you don't have to do that.
That's just what I chose because it's easy to see.
You can see here I'm displaying tour ticket prices from Miami centered.
There's no way to center something. Remember, when we use the set width
and the justification, we can only do left or right.
So centering it, you're going to have to do maybe a little bit of math and figure out what's the center.
Now I've got a header here. I'm displaying
the country, the city, the population, the price, a line of dashes.
And then I'm just lining up the data. So I've got all the countries:
Columbia, Brazil, Chile, Argentina;
the cities within the countries, the population of those cities
that's right justified here in this field
as well as the price, which is right justified. And I've got the two decimal digits displaying.
Okay. So that's it. Good luck with this one.
Again, this project is -- this little challenge is more than just formatting.
I really want you to understand the structures and how you access
those structures using the vectors within the structures.
It's really simple once you get it.
And it's really powerful to be able to use those structures that have those vectors of structures
within them.
Very powerful, very easy to use. You saw how easy it was to declare the data.
Okay. So that's it. Good luck with the challenge.
I'll see you on the other side in the solution.
Don't rush through this. Take your time.
Once this challenge is over, we'll start working with files.